oat-sa/lib-correlation-ids-monolog

OAT Correlation Ids Monolog Library

0.3.0 2022-01-13 12:57 UTC

This package is auto-updated.

Last update: 2024-11-17 14:35:52 UTC


README

PHP library for correlation ids monolog processor based on the correlation ids library.

Table of contents

Installation

$ composer require oat-sa/lib-correlation-ids-monolog

Principles

This library provides a ready to use monolog processor that will extend the logs context with the correlation ids fetched from the correlation ids registry.

Usage

You need to push the CorrelationIdsMonologProcessor to your logger as follow:

<?php declare(strict_types=1);

use OAT\Library\CorrelationIds\Registry\CorrelationIdsRegistry;
use OAT\Library\CorrelationIds\Registry\CorrelationIdsRegistryInterface;
use OAT\Library\CorrelationIdsMonolog\Processor\CorrelationIdsMonologProcessor;
use Monolog\Logger;

/** @var CorrelationIdsRegistryInterface $registry */
$registry = new CorrelationIdsRegistry(...);

$logger = new Logger('channel-name');
$logger->pushProcessor(new CorrelationIdsMonologProcessor($registry));

...

$logger->info('log message'); // this log entry context will be automatically populated with the correlation ids.

Note: you can customize the log context key names by providing you own CorrelationIdsHeaderNamesProviderInterface implementation and pass it to the CorrelationIdsMonologProcessor constructor.

Tests

To run tests:

$ vendor/bin/phpunit

Note: see phpunit.xml.dist for available test suites.