phrity/monolog

Extensions for Monolog logger framework; Context normalizer, persister, interpolator.

dev-main 2025-04-17 08:58 UTC

This package is auto-updated.

Last update: 2025-04-17 08:59:44 UTC


README

Build Status Coverage Status

Introduction

Extensions to Monolog logging framework. Adds additional Context related Processors.

Installation

Install with Composer;

composer require phrity/monolog

Processors

List of Monolog Processors included in this library.

ContextInterpolator

Advanced interpolator that attempts to interpolate context entity to string.

$logger->pushProcessor(new ContextInterpolator());
$logger->info('Test {stringable}', [
    'stringable' => new MyStringableClass(),
]);

Reference can also use . to access properties in objects and arrays.

$logger->pushProcessor(new ContextInterpolator());
$logger->info('Test {myClass.myProperty}', [
    'myClass' => new Mylass(myProperty: 1234),
]);

Optionally the interpolator can use Symfony Serializer to normalize context data used for interpolation. The DefaultSerializer provides a standard normalization setup, but you can use any class that implements Symfony NormalizerInterface.

$logger->pushProcessor(new ContextInterpolator(new DefaultSerializer()));
$logger->info('Test {dateTime}', [
    'dateTime' => new DateTime(),
]);

ContextNormalizer

Wrapper for Symfony Serializer that normalize context data.

$logger->pushProcessor(new ContextNormalizer());
$logger->info('Test', [
    'dateTime' => new DateTime(),
]);

By default it uses DefaultSerializer, a standard normalization setup, but you can use any class that implements Symfony NormalizerInterface.

$logger->pushProcessor(new ContextInterpolator(new MyNormalizer()));
$logger->info('Test', [
    'dateTime' => new DateTime(),
]);

ContextPersister

The Persister keeps context data that will be used on all log actions.

$logger->pushProcessor(new ContextPersister(['initial' => 'Will be added to all log actions']));
$logger->info('Test');

By keeping the reference of the Persister, persisted context can be changed at any point.

$persister = new ContextPersister(['initial' => 'Will be added to all log actions.']);
$logger->pushProcessor($persister);
$persister->add(['added' => 'Will be added to all subsequent log actions']);
$logger->info('Test');
$persister->set(['replaced' => 'Will replace existing on subsequent log actions']);
$logger->info('Test');
$persister->reset();
$logger->info('Test');

Versions

Version PHP
1.0 ^8.1 phrity/monolog v1.0