All Projects → tomphp → php-context-logger

tomphp / php-context-logger

Licence: MIT license
A PSR-3 compliant logger decorator which allows context metadata to be built up.

Programming Languages

PHP
23972 projects - #3 most used programming language

Projects that are alternatives of or similar to php-context-logger

monolog-http
A collection of monolog handlers that use a PSR-18 HTTP Client to send your logs
Stars: ✭ 34 (+183.33%)
Mutual labels:  psr3

Context Logger

Build Status Latest Stable Version Total Downloads Latest Unstable Version License

A PSR-3 compliant logger decorator which allows context metadata to be built up.

Installation

$ composer require tomphp/context-logger

Usage

<?php

use Monolog\Logger;
use Monolog\Handler\StreamHandler;
use TomPHP\ContextLogger;

$monolog = new Logger('name');
$monolog->pushHandler(new StreamHandler('path/to/your.log', Logger::WARNING));

$log = new ContextLogger($monolog);

$log->addContext('correlation_id', uniqid());

$log->error('There was an error');

Setting the Context

An original context can be set by providing an array as the second argument to the constructor:

$log = new ContextLogger($monolog, ['correlation_id' => uniqid()]);

The context can be added to or modified by the addContext(string $name, $value) method.

The context can also be added to/modified by providing an array to the $context parameter of any of the PSR-3 LoggerInterface methods.

Removing Context

You can remove a item from the context by using the removeContext(string $name) method.

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].