PHPackages                             yapro/monolog-ext - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Logging &amp; Monitoring](/categories/logging)
4. /
5. yapro/monolog-ext

ActiveLibrary[Logging &amp; Monitoring](/categories/logging)

yapro/monolog-ext
=================

Very useful extensions for Monolog

v2.0.1(4mo ago)2973↓50%21proprietaryPHPPHP &gt;=8.0CI passing

Since May 1Pushed 4mo ago1 watchersCompare

[ Source](https://github.com/yapro/monolog-ext)[ Packagist](https://packagist.org/packages/yapro/monolog-ext)[ RSS](/packages/yapro-monolog-ext/feed)WikiDiscussions master Synced 1mo ago

READMEChangelog (10)Dependencies (3)Versions (16)Used By (1)

MonologExt
==========

[](#monologext)

The really useful Monolog`s extensions.

[![lib tests](https://github.com/yapro/monolog-ext/actions/workflows/main.yml/badge.svg)](https://github.com/yapro/monolog-ext/actions/workflows/main.yml/badge.svg)

Installation
------------

[](#installation)

Add as a requirement in your `composer.json` file or run

```
composer require yapro/monolog-ext dev-master
```

Configuration of Symfony &gt;= 2.x
----------------------------------

[](#configuration-of-symfony--2x)

You can use the best way to handle your logs because it's the easiest way:

```
monolog:
    channels:
        - deprecation # Deprecations are logged in the dedicated "deprecation" channel when it exists
    handlers:
        main:
            type: service
            id: YaPro\MonologExt\Handler\WiseHandler
```

and don't forget to register the handler as a service:

```
services:
  YaPro\MonologExt\Handler\WiseHandler: ~
```

You will get the features:

- writing logs to stderr (  )
- json representation
- ignoring logs when the client sends an invalid http request (4xx)
- log records of all levels in the application code (src dir, not in vendor dir)
- log records of the NOTICE level and higher in libraries (vendor dir)
- smart a record reducing (removing keys from the record context when the record size exceeds 8192 bytes)

and other features such as development mode.

You can also configure WiseHandler:

env var namedefault valueexampledescriptionEH\_DEV\_MODE\_PHP\_FPM01print out important debugging information and stop the execution (comfortable development in php-fpm)EH\_DEV\_MODE\_PHP\_CLI01print out important debugging information and stop the execution (comfortable development in php-cli)EH\_MAX\_DUMP\_LEVEL53the nesting level of the objects to be serialized (3 is less detailed level than 5)EH\_IGNORE\_RECORD\_LEVEL\_BELOW0250errors with a level below the specified one will be ignored (250 is less then NOTICE, 0 == all records)EH\_STOP\_WHEN\_RECORD\_LEVEL\_ABOVE500400an error with a level higher than the specified one will stop the http request with the 500 status (Logger::CRITICAL)You can also use the collection of monolog processors, that gives you the opportunity to handle and log different errors.

Add needed for you app to your config.yml

```
services:
  # Adds exception`s information to a log record
  YaPro\MonologExt\Processor\AddInformationAboutExceptionProcessor:
    class: YaPro\MonologExt\Processor\AddInformationAboutExceptionProcessor
    tags:
      - { name: monolog.processor, handler: main }

  # Adds a call stack of the log-record location
  YaPro\MonologExt\Processor\AddStackTraceOfCallPlaceProcessor:
    class: YaPro\MonologExt\Processor\AddStackTraceOfCallPlaceProcessor
    tags:
      - { name: monolog.processor, handler: main }

  # Stop execution when problems occur (very useful in tests)
  YaPro\MonologExt\Processor\StopExecutionWhenProblemProcessor:
    class: YaPro\MonologExt\Processor\StopExecutionWhenProblemProcessor
    tags:
      - { name: monolog.processor, handler: main }

  # Moves the contents of the content field to the field specified in the processor constructor + removes the context field
  YaPro\MonologExt\Processor\RenameContextProcessor:
    class: YaPro\MonologExt\Processor\RenameContextProcessor
    tags:
      - { name: monolog.processor, handler: main, priority: -1 }

  # Moves the contents of the content field to the location specified in the record field + removes the context field
  YaPro\MonologExt\Processor\MoveContextProcessor:
    class: YaPro\MonologExt\Processor\MoveContextProcessor
    tags:
      - { name: monolog.processor, handler: main, priority: -1 }

  # Adds a request as curl command to a log record
  # Old version - https://github.com/yapro/monolog-ext/blob/php5/src/Monolog/Processor/RequestAsCurl.php
  monolog.processor.request_as_curl:
    class: Debug\Monolog\Processor\RequestAsCurl
    arguments: [ "@request_stack" ]
    tags:
      - { name: monolog.processor, handler: main }

  # not implemented yet. Old version - https://github.com/yapro/monolog-ext/blob/php5/src/Monolog/Processor/Guzzle.php
  monolog.processor.guzzle:
    class: Debug\Monolog\Processor\Guzzle
    tags:
      - { name: monolog.processor, handler: main }
  # странная особенность - если не объявить, то возникает ошибка: Cannot autowire service, no such service exists. You
  # should maybe alias this class to one of these existing services: "monolog.formatter.json", "monolog.formatter.loggly".
  # создал вопрос: https://github.com/symfony/symfony/issues/36527
  Monolog\Formatter\JsonFormatter:
    class: Monolog\Formatter\JsonFormatter
```

then use logger, examples:

```
$logger->info('I just got the logger');
$logger->error('An error occurred');
```

Look up, variable $e will be transformed to string (Monolog`s functionality), and you will get: Message of Exception + Stack trace

```
$logger->warning('My warning', array(
   'my' => 'data',
   'exception' => $e,// now you can see the above written custom stack trace as a string
));
$logger->warning('My second warning', array($e));// the short variant of version which you can see the above
```

By default, \\YaPro\\MonologExt\\VarHelper extract an extra data into string by standard depth's level which is equal to two. But, you can use any depth's level, example is equal a five:

```
$logger->error('An error occurred', [ 'my mixed type var' => (new VarHelper)->dump($myVar, 5) ] );
```

What is ExtraException
----------------------

[](#what-is-extraexception)

ExtraException is exception which you can to create as object, to add the extra data and throw away. After throwing the Monolog ExceptionProcessor will catches this exception and saves extra data to logs. Examples:

```
throw (new ExtraException())->setExtra('mixed data');
```

Recommendation
--------------

[](#recommendation)

Add service json\_formatter to file app/config/config.yml It will help you to format error in the json, and then you can use  for aggregate all errors.

```
services:
    json_formatter:
        class: Monolog\Formatter\JsonFormatter
```

And don`t forget to add a monolog formatter:

```
monolog:
    handlers:
        main:
            formatter: json_formatter
```

If you wish to collect some data of http request, you can add WebProcessor:

```
services:
    monolog.processor.web:
        class: Monolog\Processor\WebProcessor
        tags:
            - { name: monolog.processor, handler: main }
```

The configuration without Symfony framework.
--------------------------------------------

[](#the-configuration-without-symfony-framework)

---

Here is a configuration sample:

```
