Giter Club home page Giter Club logo

monolog-processor-collection's Introduction

Monolog Processor Collection

Minimum PHP Version CI Latest Stable Version License

Welcome to the Monolog Processor Collection (MPC) - the ultimate suite of processors designed to enhance your logging with the renowned Monolog library. This toolkit is meticulously crafted to integrate seamlessly with PHP 8.1+, ensuring your logging captures the comprehensive details you need with minimal overhead.

MPC is engineered for developers who demand more from their logs. Whether you're tracking down elusive bugs or monitoring live production environments, processors enrich your log entries with invaluable context, turning ordinary logs into a rich, actionable dataset.

MPC is compatible with worker mode of web servers, as relevant processors implement the ResettableInterface.

Available Processors

The package provides the following processors:

  • BacktraceProcessor adds the backtrace to the log record
  • ClientIpProcessor adds the client IP address to the log record
  • EnvVarProcessor adds the value of one or more environment variables to the log record
  • HighResolutionTimestampProcessor adds the high resolution time to the log record
  • IsHttpsProcessor adds a boolean value indicating whether the request is a secured HTTP request to the log record
  • PhpIniValueProcessor adds the value of one or more PHP ini settings to the log record
  • ProtocolVersionProcessor adds the HTTP protocol version to the log record
  • RequestSizeProcessor adds the size of the request to the log record, headers included, in bytes
  • ResourceUsagesProcessor adds the resource usage to the log record as returned by getrusage()
  • SapiNameProcessor adds the name of the SAPI to the log record
  • SessionIdProcessor adds the session ID to the log record, or null if no session is active
  • UuidProcessor adds a UUID v7 to the log record to track records triggered during the same request

Installation

The recommended way to install MPC is through Composer:

composer require alexandre-daubois/monolog-processor-collection

Usage

All processor can be used in the same way as any other Monolog processor. For example:

use Monolog\Logger;

$logger = new Logger('name');
$logger->pushProcessor(new BacktraceProcessor());

Some processors, like EnvVarProcessor and PhpIniValueProcessor, require you to specify more arguments. For example:

use Monolog\Logger;

$logger = new Logger('name');
$logger->pushProcessor(new EnvVarProcessor(['APP_ENV', 'APP_DEBUG']));

Integration with Symfony and MonologBundle

You can register those processors to be used with Symfony and MonologBundle by adding the following configuration to your config/packages/monolog.php file:

use Monolog\Processor\ProcessorInterface;
use MonologProcessorCollection\BacktraceProcessor;
use MonologProcessorCollection\EnvVarProcessor;
use MonologProcessorCollection\ProtocolVersionProcessor;
use MonologProcessorCollection\EnvVarProcessor;
use MonologProcessorCollection\SapiNameProcessor;

return static function (ContainerConfigurator $configurator): void {
    // ...

    // register as many processors as you like, but keep in mind that
    // each processor is called for each log record
    $services = $configurator->services();
    $services
        ->set(BacktraceProcessor::class)
        ->set(EnvVarProcessor::class)->args(['APP_ENV'])
        ->set(ProtocolVersionProcessor::class)
        ->set(SapiNameProcessor::class);

    // ...
};

If you don't use autoconfigure, you need to tag the processors with monolog.processor:

return static function (ContainerConfigurator $configurator): void {
    // ...

    $services = $configurator->services();
    $services
        ->set(BacktraceProcessorAlias::class)
            ->tag('monolog.processor', ['handler' => 'main'])
        ->set(EnvVarProcessor::class)->args(['APP_ENV'])
            ->tag('monolog.processor', ['handler' => 'main']);

    // ...
};

You can achieve the same configuration with YAML:

# config/packages/monolog.yaml
services:
    MonologProcessorCollection\BacktraceProcessor:
        tags:
            - { name: monolog.processor, handler: main }
    MonologProcessorCollection\EnvVarProcessor:
        arguments:
            - APP_ENV
        tags:
            - { name: monolog.processor, handler: main }

Or XML:

<!-- config/packages/monolog.xml -->

<!-- ... -->

<service id="MonologProcessorCollection\BacktraceProcessor" public="false">
    <tag name="monolog.processor" handler="main" />
</service>
<service id="MonologProcessorCollection\EnvVarProcessor" public="false">
    <argument>APP_ENV</argument>
    <tag name="monolog.processor" handler="main" />
</service>

monolog-processor-collection's People

Contributors

alexandre-daubois avatar toshy avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar

Forkers

toshy

monolog-processor-collection's Issues

Few questions

Hey @alexandre-daubois :), hope you're doing well!

I have a few questions:

  • Is there any advantage for BacktraceProcessor vs monolog includeStacktraces feature?
  • Is there any advantage for UuidProcessor vs monolog's UidProcessor?
  • ProtocolVersionProcessor/IsHttpsProcessor: maybe that could make a good addition for monolog's WebProcessor, wdyt?
  • Not really a question, but I think dealing with log level within the processor is wrong, log level is a concern for handlers within monolog, so the processors won't be called if the handler it is attached to does not handle due to log level

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.