8000 [Logger] [Monolog] Add ConfigBuilders for Monolog by Nyholm · Pull Request #15278 · symfony/symfony-docs · GitHub
[go: up one dir, main page]

Skip to content
8000

[Logger] [Monolog] Add ConfigBuilders for Monolog #15278

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Apr 23, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 5 additions & 3 deletions email.rst
Original file line number Diff line number Diff line change
Expand Up @@ -394,9 +394,11 @@ the report with details of the sent emails.
.. code-block:: php

// config/packages/dev/web_profiler.php
$container->loadFromExtension('web_profiler', [
'intercept_redirects' => 'true',
]);
use Symfony\Config\WebProfilerConfig;

return static function (WebProfilerConfig $webProfiler) {
$webProfiler->interceptRedirects(true);
};

.. tip::

Expand Down
102 changes: 50 additions & 52 deletions logging.rst
Original file line number Diff line number Diff line change
Expand Up @@ -138,23 +138,22 @@ to write logs using the :phpfunction:`syslog` function:
.. code-block:: php

// config/packages/prod/monolog.php
$container->loadFromExtension('monolog', [
'handlers' => [
// this "file_log" key could be anything
'file_log' => [
'type' => 'stream',
// log to var/logs/(environment).log
'path' => '%kernel.logs_dir%/%kernel.environment%.log',
// log *all* messages (debug is lowest level)
'level' => 'debug',
],
'syslog_handler' => [
'type' => 'syslog',
// log error-level messages and higher
'level' => 'error',
],
],
]);
use Symfony\Config\MonologConfig;

return static function (MonologConfig $monolog) {
// this "file_log" key could be anything
$monolog->handler('file_log')
->type('stream')
// log to var/logs/(environment).log
->path('%kernel.logs_dir%/%kernel.environment%.log')
// log *all* messages (debug is lowest level)
->level('debug');

$monolog->handler('syslog_handler')
->type('syslog')
// log error-level messages and higher
->level('error');
};

This defines a *stack* of handlers and each handler is called in the order that it's
defined.
Expand Down Expand Up @@ -236,29 +235,29 @@ one of the messages reaches an ``action_level``. Take this example:
.. code-block:: php

// config/packages/prod/monolog.php
$container->loadFromExtension('monolog', [
'handlers' => [
'filter_for_errors' => [
'type' => 'fingers_crossed',
// if *one* log is error or higher, pass *all* to file_log
'action_level' => 'error',
'handler' => 'file_log',
],

// now passed *all* logs, but only if one log is error or higher
'file_log' => [
'type' => 'stream',
'path' => '%kernel.logs_dir%/%kernel.environment%.log',
'level' => 'debug',
],

// still passed *all* logs, and still only logs error or higher
'syslog_handler' => [
'type' => 'syslog',
'level' => 'error',
],
],
]);
use Symfony\Config\MonologConfig;

return static function (MonologConfig $monolog) {
$monolog->handler('filter_for_errors')
->type('fingers_crossed')
// if *one* log is error or higher, pass *all* to file_log
->actionLevel('error')
->handler('file_log')
;

// now passed *all* logs, but only if one log is error or higher
$monolog->handler('file_log')
->type('stream')
->path('%kernel.logs_dir%/%kernel.environment%.log')
->level('debug')
;

// still passed *all* logs, and still only logs error or higher
$monolog->handler('syslog_handler')
->type('syslog')
->level('error')
;
};

Now, if even one log entry has an ``error`` level or higher, then *all* log entries
for that request are saved to a file via the ``file_log`` handler. That means that
Expand Down Expand Up @@ -331,18 +330,17 @@ option of your handler to ``rotating_file``:
.. code-block:: php

// config/packages/prod/monolog.php
$container->loadFromExtension('monolog', [
'handlers' => [
'main' => [
'type' => 'rotating_file',
'path' => '%kernel.logs_dir%/%kernel.environment%.log',
'level' => 'debug',
// max number of log files to keep
// defaults to zero, which means infinite files
'max_files' => 10,
],
],
]);
use Symfony\Config\MonologConfig;

return static function (MonologConfig $monolog) {
$monolog->handler('main')
->type('rotating_file')
->path('%kernel.logs_dir%/%kernel.environment%.log')
->level('debug')
// max number of log files to keep
// defaults to zero, which means infinite files
->maxFiles(10);
};

Using a Logger inside a Service
-------------------------------
Expand Down
41 changes: 18 additions & 23 deletions logging/channels_handlers.rst
Original file line number Diff line number Diff line change
Expand Up @@ -78,23 +78,19 @@ can do it in any (or all) environments:
.. code-block:: php

// config/packages/prod/monolog.php
$container->loadFromExtension('monolog', [
'handlers' => [
'security' => [
'type' => 'stream',
'path' => '%kernel.logs_dir%/security.log',
'channels' => [
'security',
],
],
'main' => [
// ...
'channels' => [
'!security',
],
],
],
]);
use Symfony\Config\MonologConfig;

return static function (MonologConfig $monolog) {
$monolog->handler('security')
->type('stream')
->path('%kernel.logs_dir%/security.log')
->channels()->elements(['security']);

$monolog->handler('main')
// ...

->channels()->elements(['!security']);
};

.. caution::

Expand Down Expand Up @@ -163,12 +159,11 @@ You can also configure additional channels without the need to tag your services
.. code-block:: php

// config/packages/prod/monolog.php
$container->loadFromExtension('monolog', [
'channels' => [
'foo',
'bar',
],
]);
use Symfony\Config\MonologConfig;

return static function (MonologConfig $monolog) {
$monolog->channels(['foo', 'bar']);
};

Symfony automatically registers one service per channel (in this example, the
channel ``foo`` creates a service called ``monolog.logger.foo``). In order to
Expand Down
24 changes: 10 additions & 14 deletions logging/formatter.rst
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ configure your handler to use it:

.. code-block:: xml

<!-- config/services.xml -->
<!-- config/packages/prod/monolog.xml (and/or config/packages/dev/monolog.xml) -->
<?xml version="1.0" encoding="UTF-8" ?>
<container xmlns="http://symfony.com/schema/dic/services"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
Expand All @@ -33,7 +33,6 @@ configure your handler to use it:
http://symfony.com/schema/dic/monolog
https://symfony.com/schema/dic/monolog/monolog-1.0.xsd">

<!-- config/packages/prod/monolog.xml (and/or config/packages/dev/monolog.xml) -->
<monolog:config>
<monolog:handler
name="file"
Expand All @@ -46,16 +45,13 @@ configure your handler to use it:

.. code-block:: php

// config/services.php
use Monolog\Formatter\JsonFormatter;

// config/packages/prod/monolog.php (and/or config/packages/dev/monolog.php)
$container->loadFromExtension('monolog', [
'handlers' => [
'file' => [
'type' => 'stream',
'level' => 'debug',
'formatter' => 'monolog.formatter.json',
],
],
]);
use Symfony\Config\MonologConfig;

return static function (MonologConfig $monolog) {
$monolog->handler('file')
->type('stream')
->level('debug')
->formatter('monolog.formatter.json')
;
};
17 changes: 8 additions & 9 deletions logging/handlers.rst
Original file line number Diff line number Diff line change
Expand Up @@ -88,14 +88,13 @@ Then reference it in the Monolog configuration:

// config/packages/prod/monolog.php
use Symfony\Bridge\Monolog\Handler\ElasticsearchLogstashHandler;

$container->loadFromExtension('monolog', [
'handlers' => [
'es' => [
'type' => 'service',
'id' => ElasticsearchLogstashHandler::class,
],
],
]);
use Symfony\Config\MonologConfig;

return static function (MonologConfig $monolog) {
$monolog->handler('es')
->type('service')
->id(ElasticsearchLogstashHandler::class)
;
};

.. _`ELK stack`: https://www.elastic.co/what-is/elk-stack
18 changes: 9 additions & 9 deletions logging/monolog_console.rst
Original file line number Diff line number Diff line change
Expand Up @@ -122,15 +122,15 @@ The Monolog console handler is enabled by default:
.. code-block:: php

// config/packages/dev/monolog.php
$container->loadFromExtension('monolog', [
'handlers' => [
'console' => [
'type' => 'console',
'process_psr_3_messages' => false,
'channels' => ['!event', '!doctrine', '!console'],
],
],
]);
use Symfony\Config\MonologConfig;

return static function (MonologConfig $monolog) {
$monolog->handler('console')
->type('console')
->processPsr3Messages(false)
->channels()->elements(['!event', '!doctrine', '!console'])
;
};

Now, log messages will be shown on the console based on the log levels and verbosity.
By default (normal verbosity level), warnings and higher will be shown. But in
Expand Down
Loading
0