diff --git a/email.rst b/email.rst index 60e630abc38..d2f52a21fd6 100644 --- a/email.rst +++ b/email.rst @@ -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:: diff --git a/logging.rst b/logging.rst index 2651247d5c5..b18c6479bf1 100644 --- a/logging.rst +++ b/logging.rst @@ -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. @@ -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 @@ -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 ------------------------------- diff --git a/logging/channels_handlers.rst b/logging/channels_handlers.rst index 8f6e9aed98a..5c06b13d794 100644 --- a/logging/channels_handlers.rst +++ b/logging/channels_handlers.rst @@ -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:: @@ -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 diff --git a/logging/formatter.rst b/logging/formatter.rst index b41cd7ad06e..737b0b86a5f 100644 --- a/logging/formatter.rst +++ b/logging/formatter.rst @@ -23,7 +23,7 @@ configure your handler to use it: .. code-block:: xml - + - 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') + ; + }; diff --git a/logging/handlers.rst b/logging/handlers.rst index 9f5b903cfa9..4e37e01f622 100644 --- a/logging/handlers.rst +++ b/logging/handlers.rst @@ -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 diff --git a/logging/monolog_console.rst b/logging/monolog_console.rst index 21c67d705d8..13789a35b25 100644 --- a/logging/monolog_console.rst +++ b/logging/monolog_console.rst @@ -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 diff --git a/logging/monolog_email.rst b/logging/monolog_email.rst index 22ed4d08928..77a27cea9cd 100644 --- a/logging/monolog_email.rst +++ b/logging/monolog_email.rst @@ -99,36 +99,36 @@ it is broken down. .. code-block:: php // config/packages/prod/monolog.php - $container->loadFromExtension('monolog', [ - 'handlers' => [ - 'main' => [ - 'type' => 'fingers_crossed', - // 500 errors are logged at the critical level - 'action_level' => 'critical', - // to also log 400 level errors (but not 404's): - // 'action_level' => 'error', - // 'excluded_404s' => [ - // '^/', - // ], - 'handler' => 'deduplicated', - ], - 'deduplicated' => [ - 'type' => 'deduplication', - 'handler' => 'symfony_mailer', - ], - 'symfony_mailer' => [ - 'type' => 'symfony_mailer', - 'from_email' => 'error@example.com', - 'to_email' => 'error@example.com', - // or a list of recipients - // 'to_email' => ['dev1@example.com', 'dev2@example.com', ...], - 'subject' => 'An Error Occurred! %%message%%', - 'level' => 'debug', - 'formatter' => 'monolog.formatter.html', - 'content_type' => 'text/html', - ], - ], - ]); + use Symfony\Config\MonologConfig; + + return static function (MonologConfig $monolog) { + $monolog->handler('main') + ->type('fingers_crossed') + // 500 errors are logged at the critical level + ->actionLevel('critical') + // to also log 400 level errors (but not 404's): + // ->actionLevel('error') + // ->excluded404s(['^/']) + + ->handler('deduplicated') + ; + + $monolog->handler('deduplicated') + ->type('deduplicated') + ->handler('symfony_mailer'); + + $monolog->handler('symfony_mailer') + ->type('symfony_mailer') + ->fromEmail('error@example.com') + ->toEmail(['error@example.com']) + // or a list of recipients + // ->toEmail(['dev1@example.com', 'dev2@example.com', ...]) + ->subject('An Error Occurred! %%message%%') + ->level('debug') + ->formatter('monolog.formatter.html') + ->contentType('text/html') + ; + }; The ``main`` handler is a ``fingers_crossed`` handler which means that it is only triggered when the action level, in this case ``critical`` is reached. @@ -177,17 +177,18 @@ You can adjust the time period using the ``time`` option: .. code-block:: php // config/packages/prod/monolog.php - $container->loadFromExtension('monolog', [ - 'handlers' => [ - // ... - 'deduplicated' => [ - 'type' => 'deduplication', - // the time in seconds during which duplicate entries are discarded (default: 60) - 'time' => 10, - 'handler' => 'symfony_mailer', - ], - ], - ]); + use Symfony\Config\MonologConfig; + + return static function (MonologConfig $monolog) { + // ... + + $monolog->handler('deduplicated') + ->type('deduplicated') + // the time in seconds during which duplicate entries are discarded (default: 60) + ->time(10) + ->handler('symfony_mailer') + ; + }; The messages are then passed to the ``symfony_mailer`` handler. This is the handler that actually deals with emailing you the error. The settings for this are @@ -285,39 +286,43 @@ get logged on the server as well as the emails being sent: .. code-block:: php // config/packages/prod/monolog.php - $container->loadFromExtension('monolog', [ - 'handlers' => [ - 'main' => [ - 'type' => 'fingers_crossed', - 'action_level' => 'critical', - 'handler' => 'grouped', - ], - 'grouped' => [ - 'type' => 'group', - 'members' => ['streamed', 'deduplicated'], - ], - 'streamed' => [ - 'type' => 'stream', - 'path' => '%kernel.logs_dir%/%kernel.environment%.log', - 'level' => 'debug', - ], - 'deduplicated' => [ - 'type' => 'deduplication', - 'handler' => 'symfony_mailer', - ], - 'symfony_mailer' => [ - 'type' => 'symfony_mailer', - 'from_email' => 'error@example.com', - 'to_email' => 'error@example.com', - // or a list of recipients - // 'to_email' => ['dev1@example.com', 'dev2@example.com', ...], - 'subject' => 'An Error Occurred! %%message%%', - 'level' => 'debug', - 'formatter' => 'monolog.formatter.html', - 'content_type' => 'text/html', - ], - ], - ]); + use Symfony\Config\MonologConfig; + + return static function (MonologConfig $monolog) { + $monolog->handler('main') + ->type('fingers_crossed') + ->actionLevel('critical') + ->handler('grouped') + ; + + $monolog->handler('group') + ->members(['streamed', 'deduplicated']) + ; + + $monolog->handler('streamed') + ->type('stream') + ->path('%kernel.logs_dir%/%kernel.environment%.log') + ->level('debug') + ; + + $monolog->handler('deduplicated') + ->type('deduplicated') + ->handler('symfony_mailer') + ; + + // still passed *all* logs, and still only logs error or higher + $monolog->handler('symfony_mailer') + ->type('symfony_mailer') + ->fromEmail('error@example.com') + ->toEmail(['error@example.com']) + // or a list of recipients + // ->toEmail(['dev1@example.com', 'dev2@example.com', ...]) + ->subject('An Error Occurred! %%message%%') + ->level('debug') + ->formatter('monolog.formatter.html') + ->contentType('text/html') + ; + }; This uses the ``group`` handler to send the messages to the two group members, the ``deduplicated`` and the ``stream`` handlers. The messages will diff --git a/logging/monolog_exclude_http_codes.rst b/logging/monolog_exclude_http_codes.rst index 9c1bd81bdcc..4382b818437 100644 --- a/logging/monolog_exclude_http_codes.rst +++ b/logging/monolog_exclude_http_codes.rst @@ -49,16 +49,16 @@ logging these HTTP codes based on the MonologBundle configuration: .. code-block:: php // config/packages/prod/monolog.php - $container->loadFromExtension('monolog', [ - 'handlers' => [ - 'main' => [ - // ... - 'type' => 'fingers_crossed', - 'handler' => ..., - 'excluded_http_codes' => [403, 404], - ], - ], - ]); + use Symfony\Config\MonologConfig; + + return static function (MonologConfig $monolog) { + $monolog->handler('main') + // ... + ->type('fingers_crossed') + ->handler(...) + ->excludedHttpCode([403, 404]) + ; + }; .. caution:: diff --git a/logging/monolog_regex_based_excludes.rst b/logging/monolog_regex_based_excludes.rst index be4a6ee8b7e..b5186e13b60 100644 --- a/logging/monolog_regex_based_excludes.rst +++ b/logging/monolog_regex_based_excludes.rst @@ -54,18 +54,16 @@ configuration: .. code-block:: php // config/packages/prod/monolog.php - $container->loadFromExtension('monolog', [ - 'handlers' => [ - 'main' => [ - // ... - 'type' => 'fingers_crossed', - 'handler' => ..., - 'excluded_404s' => [ - '^/phpmyadmin', - ], - ], - ], - ]); + use Symfony\Config\MonologConfig; + + return static function (MonologConfig $monolog) { + $monolog->handler('main') + // ... + ->type('fingers_crossed') + ->handler(...) + ->excluded404s(['^/phpmyadmin']) + ; + }; .. caution:: diff --git a/logging/processors.rst b/logging/processors.rst index c0a3eb33bbf..3c4faacda5f 100644 --- a/logging/processors.rst +++ b/logging/processors.rst @@ -149,16 +149,16 @@ Finally, set the formatter to be used on whatever handler you want: .. code-block:: php // config/packages/prod/monolog.php - $container->loadFromExtension('monolog', [ - 'handlers' => [ - 'main' => [ - 'type' => 'stream', - 'path' => '%kernel.logs_dir%/%kernel.environment%.log', - 'level' => 'debug', - 'formatter' => 'monolog.formatter.session_request', - ], - ], - ]); + use Symfony\Config\MonologConfig; + + return static function (MonologConfig $monolog) { + $monolog->handler('main') + ->type('stream') + ->path('%kernel.logs_dir%/%kernel.environment%.log') + ->level('debug') + ->formatter('monolog.formatter.session_request') + ; + }; If you use several handlers, you can also register a processor at the handler level or at the channel level instead of registering it globally