PHP code example of manomano-tech / correlation-ids-monolog

1. Go to this page and download the library: Download manomano-tech/correlation-ids-monolog library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

manomano-tech / correlation-ids-monolog example snippets


use ManoManoTech\CorrelationId\CorrelationEntryName;
use ManoManoTech\CorrelationId\Factory\HeaderCorrelationIdContainerFactory;
use ManoManoTech\CorrelationId\Generator\RamseyUuidGenerator;
use ManoManoTech\CorrelationIdMonolog\CorrelationIdProcessor;
use Monolog\Logger;

// We specify which generator will be responsible for generating the
// identification of the current process
$generator = new RamseyUuidGenerator();

// We define what are the http header names to look for
// this is optional. We show the default values here.
$correlationEntryNames = new CorrelationEntryName(
    'current-correlation-id',
    'parent-correlation-id',
    'root-correlation-id'
);

$factory = new HeaderCorrelationIdContainerFactory(
    $generator,
    $correlationEntryNames
);
$correlationIdContainer = $factory->create(getallheaders());

// now you can create your monolog processor

$processor = new CorrelationIdProcessor($correlationIdContainer);

$logger = new Logger('channel-name');
$logger->pushProcessor([$processor]);

$logger->addInfo('message');

$record = [
    'extra' => [
        'current' => '6a051d24-aa5b-4c57-bcb4-bbbb7eda1c16',
        'parent' => '3fc044d9-90fa-4b50-b6d9-3423f567155f',
        'root' => '3b5263fa-1644-4750-8f11-aaf61e58cd9e',
    ],
];

use ManoManoTech\CorrelationId\CorrelationEntryName;
use ManoManoTech\CorrelationIdMonolog\CorrelationIdProcessor;
use Monolog\Logger;

$correlationEntryName = new CorrelationEntryName(
    'current-id',
    'parent-id',
    'root-id'
);

$processor = new CorrelationIdProcessor(
    $correlationIdContainer,
    $correlationEntryName
);

$logger = new Logger('channel-name');
$logger->pushProcessor([$processor]);

$logger->addInfo('message');

$record = [
    'extra' => [
        'current-id' => '6a051d24-aa5b-4c57-bcb4-bbbb7eda1c16',
        'parent-id' => '3fc044d9-90fa-4b50-b6d9-3423f567155f',
        'root-id' => '3b5263fa-1644-4750-8f11-aaf61e58cd9e',
    ],
];

use ManoManoTech\CorrelationIdMonolog\CorrelationIdProcessor;
use Monolog\Logger;

$processor = new CorrelationIdProcessor($correlationIdContainer);
$processor->groupCorrelationIdsInOneArrayWithKey('correlation');

$logger = new Logger('channel-name');
$logger->pushProcessor([$processor]);

$logger->addInfo('message');

$record = [
    'extra' => [
        'correlation' => [
            'current' => '6a051d24-aa5b-4c57-bcb4-bbbb7eda1c16',
            'parent' => '3fc044d9-90fa-4b50-b6d9-3423f567155f',
            'root' => '3b5263fa-1644-4750-8f11-aaf61e58cd9e',
        ],
    ],
];

$record = [
    'extra' => [
        'current' => '6a051d24-aa5b-4c57-bcb4-bbbb7eda1c16',
        'parent' => null,
        'root' => null,
    ],
];

use ManoManoTech\CorrelationIdMonolog\CorrelationIdProcessor;
use Monolog\Logger;

$processor = new CorrelationIdProcessor($CorrelationIdContainer);
$processor->skipEmptyValues();

$logger = new Logger('channel-name');
$logger->pushProcessor([$processor]);

$logger->addInfo('message');

$record = [
    'extra' => [
        'current' => '6a051d24-aa5b-4c57-bcb4-bbbb7eda1c16',
    ],
];