From eee0a11f815738be357a43cac97ada1828ec2dc4 Mon Sep 17 00:00:00 2001 From: Javier Eguiluz Date: Thu, 16 Nov 2017 17:21:33 +0100 Subject: [PATCH 1/2] Updated logging/* articles to Symfony 4 --- logging.rst | 51 +++++++++++------------ logging/channels_handlers.rst | 15 ++++--- logging/disable_microsecond_precision.rst | 27 +++--------- logging/formatter.rst | 9 ++-- logging/monolog_console.rst | 25 ++++++----- logging/monolog_email.rst | 18 ++++---- logging/monolog_regex_based_excludes.rst | 6 +-- logging/processors.rst | 33 +++++++-------- 8 files changed, 83 insertions(+), 101 deletions(-) diff --git a/logging.rst b/logging.rst index 8b1e2b6b111..8f81925243f 100644 --- a/logging.rst +++ b/logging.rst @@ -7,19 +7,16 @@ logs that can be stored in a variety of different places. Logging a Message ----------------- -To log a message, fetch the ``logger`` service from the container in -your controller:: +If the application uses the :ref:`default services.yaml configuration `, +you can get the logger service injecting the ``LoggerInterface`` class:: use Psr\Log\LoggerInterface; - public function indexAction(LoggerInterface $logger) + public function index(LoggerInterface $logger) { - // alternative way of getting the logger - // $logger = $this->get('logger'); - $logger->info('I just got the logger'); $logger->error('An error occurred'); - + $logger->critical('I left the oven on!', array( // include extra "context" info in your logs 'cause' => 'in_hurry', @@ -28,7 +25,7 @@ your controller:: // ... } -The ``logger`` service has different methods for different logging levels/priorities. +The logger service has different methods for different logging levels/priorities. You can configure the logger to do different things based on the *level* of a message (e.g. :doc:`send an email when an error occurs `). @@ -37,10 +34,6 @@ See LoggerInterface_ for a list of all of the methods on the logger. Where Logs are Stored --------------------- -The configuration for *where* logs are stored lives in the specific -:doc:`environment ` configuration files: ``config_dev.yml`` -and ``config_prod.yml``. - By default, log entries are written to the ``var/log/dev.log`` file when you're in the ``dev`` environment. In the ``prod`` environment, logs are written to ``var/log/prod.log``, but *only* during a request where an error or high-priority log entry was made @@ -61,8 +54,8 @@ to different locations (e.g. files, database, Slack, etc). channel can have its *own* handlers, which means you can store different log messages in different places. See :doc:`/logging/channels_handlers`. -Symfony pre-configures some basic handlers in the ``config_dev.yml`` and ``config_prod.yml`` -files. Check these out for some real-world examples. +Symfony pre-configures some basic handlers in the default ``monolog.yaml`` +config files. Check these out for some real-world examples. This example uses *two* handlers: ``stream`` (to write to a file) and ``syslog`` to write logs using the :phpfunction:`syslog` function: @@ -71,7 +64,7 @@ to write logs using the :phpfunction:`syslog` function: .. code-block:: yaml - # app/config/config.yml + # config/packcages/monolog.yaml monolog: handlers: # this "file_log" key could be anything @@ -89,7 +82,7 @@ to write logs using the :phpfunction:`syslog` function: .. code-block:: xml - + loadFromExtension('monolog', array( 'handlers' => array( 'file_log' => array( @@ -147,7 +140,7 @@ one of the messages reaches an ``action_level``. Take this example: .. code-block:: yaml - # app/config/config.yml + # config/packcages/monolog.yaml monolog: handlers: filter_for_errors: @@ -168,7 +161,7 @@ one of the messages reaches an ``action_level``. Take this example: .. code-block:: xml - + loadFromExtension('monolog', array( 'handlers' => array( 'filter_for_errors' => array( @@ -261,7 +254,7 @@ option of your handler to ``rotating_file``: .. code-block:: yaml - # app/config/config_dev.yml + # config/packcages/dev/monolog.yaml monolog: handlers: main: @@ -274,7 +267,7 @@ option of your handler to ``rotating_file``: .. code-block:: xml - + loadFromExtension('monolog', array( 'handlers' => array( 'main' => array( @@ -315,8 +308,7 @@ option of your handler to ``rotating_file``: Using a Logger inside a Service ------------------------------- -To use a logger in your own services, add the ``@logger`` service as an argument -of those services. If you want to use a pre-configured logger which uses a +If you want to use in your own services a pre-configured logger which uses a specific channel (``app`` by default), use the ``monolog.logger`` tag with the ``channel`` property as explained in the :ref:`Dependency Injection reference `. @@ -334,9 +326,14 @@ Learn more .. toctree:: :maxdepth: 1 - :glob: - logging/* + logging/monolog_regex_based_excludes + logging/monolog_email + logging/channels_handlers + logging/monolog_console + logging/disable_microsecond_precision + logging/formatter + logging/processors .. _Monolog: https://github.com/Seldaek/monolog .. _LoggerInterface: https://github.com/php-fig/log/blob/master/Psr/Log/LoggerInterface.php diff --git a/logging/channels_handlers.rst b/logging/channels_handlers.rst index 48a11f405be..d0b05d57333 100644 --- a/logging/channels_handlers.rst +++ b/logging/channels_handlers.rst @@ -25,14 +25,13 @@ Switching a Channel to a different Handler Now, suppose you want to log the ``security`` channel to a different file. To do this, just create a new handler and configure it to log only messages -from the ``security`` channel. You might add this in ``config.yml`` to log -in all environments, or just ``config_prod.yml`` to happen only in ``prod``: +from the ``security`` channel: .. configuration-block:: .. code-block:: yaml - # app/config/config.yml + # config/packages/monolog.yaml monolog: handlers: security: @@ -49,7 +48,7 @@ in all environments, or just ``config_prod.yml`` to happen only in ``prod``: .. code-block:: xml - + loadFromExtension('monolog', array( 'handlers' => array( 'security' => array( @@ -138,13 +137,13 @@ You can also configure additional channels without the need to tag your services .. code-block:: yaml - # app/config/config.yml + # config/packages/monolog.yaml monolog: channels: ['foo', 'bar'] .. code-block:: xml - + loadFromExtension('monolog', array( 'channels' => array( 'foo', diff --git a/logging/disable_microsecond_precision.rst b/logging/disable_microsecond_precision.rst index 5e090b45072..d7b922d7cca 100644 --- a/logging/disable_microsecond_precision.rst +++ b/logging/disable_microsecond_precision.rst @@ -11,18 +11,14 @@ log generation. This is recommended for systems that generate a large number of .. code-block:: yaml - # app/config/config.yml + # config/packages/monolog.yaml monolog: use_microseconds: false - handlers: - applog: - type: stream - path: /var/log/symfony.log - level: error + # ... .. code-block:: xml - + - + .. code-block:: php - // app/config/config.php + // config/packages/monolog.php $container->loadFromExtension('monolog', array( 'use_microseconds' => false, - 'handlers' => array( - 'applog' => array( - 'type' => 'stream', - 'path' => '/var/log/symfony.log', - 'level' => 'error', - ), - ), + // ... )); diff --git a/logging/formatter.rst b/logging/formatter.rst index 3131b8c79f1..e5ce61af0bb 100644 --- a/logging/formatter.rst +++ b/logging/formatter.rst @@ -20,7 +20,7 @@ configure your handler to use it: Monolog\Formatter\JsonFormatter: ~ - # app/config/config_prod.yml (and/or config_dev.yml) + # config/packages/prod/monolog.yaml (and/or config/packages/dev/monolog.yaml) monolog: handlers: file: @@ -44,7 +44,7 @@ configure your handler to use it: - + register(JsonFormatter::class); - // app/config/config_prod.php (or config_dev.php) + // config/packages/prod/monolog.php (and/or config/packages/dev/monolog.php) $container->loadFromExtension('monolog', array( 'handlers' => array( 'file' => array( diff --git a/logging/monolog_console.rst b/logging/monolog_console.rst index b0f0e40760f..435a933bb1f 100644 --- a/logging/monolog_console.rst +++ b/logging/monolog_console.rst @@ -35,16 +35,22 @@ current log level and the console verbosity. The example above could then be rewritten as:: + use Psr\Log\LoggerInterface; use Symfony\Component\Console\Input\InputInterface; use Symfony\Component\Console\Output\OutputInterface; - protected function execute(InputInterface $input, OutputInterface $output) + private $logger; + + public function __constructor(LoggerInterface $logger) { - // assuming the Command extends ContainerAwareCommand... - $logger = $this->getContainer()->get('logger'); - $logger->debug('Some info'); + $this->logger = $logger; + } - $logger->notice('Some more info'); + protected function execute(InputInterface $input, OutputInterface $output) + { + $this->logger->debug('Some info'); + // ... + $this->logger->notice('Some more info'); } Depending on the verbosity level that the command is run in and the user's @@ -53,14 +59,13 @@ the console. If they are displayed, they are timestamped and colored appropriate Additionally, error logs are written to the error output (php://stderr). There is no need to conditionally handle the verbosity settings anymore. -The Monolog console handler is enabled by default in the Symfony Framework. For -example, in ``config_dev.yml``: +The Monolog console handler is enabled by default: .. configuration-block:: .. code-block:: yaml - # app/config/config_dev.yml + # config/packages/dev/monolog.yaml monolog: handlers: # ... @@ -75,7 +80,7 @@ example, in ``config_dev.yml``: .. code-block:: xml - + loadFromExtension('monolog', array( 'handlers' => array( 'console' => array( diff --git a/logging/monolog_email.rst b/logging/monolog_email.rst index 59eb032d739..4ff3ff4dc58 100644 --- a/logging/monolog_email.rst +++ b/logging/monolog_email.rst @@ -14,7 +14,7 @@ it is broken down. .. code-block:: yaml - # app/config/config_prod.yml + # config/packages/prod/monolog.yaml monolog: handlers: mail: @@ -42,7 +42,7 @@ it is broken down. .. code-block:: xml - + loadFromExtension('monolog', array( 'handlers' => array( 'mail' => array( @@ -149,7 +149,7 @@ You can adjust the time period using the ``time`` option: .. code-block:: yaml - # app/config/config_prod.yml + # config/packages/prod/monolog.yaml monolog: handlers: # ... @@ -161,7 +161,7 @@ You can adjust the time period using the ``time`` option: .. code-block:: xml - + loadFromExtension('monolog', array( 'handlers' => array( // ... @@ -194,7 +194,7 @@ get logged on the server as well as the emails being sent: .. code-block:: yaml - # app/config/config_prod.yml + # config/packages/prod/monolog.yaml monolog: handlers: main: @@ -222,7 +222,7 @@ get logged on the server as well as the emails being sent: .. code-block:: xml - + loadFromExtension('monolog', array( 'handlers' => array( 'main' => array( diff --git a/logging/monolog_regex_based_excludes.rst b/logging/monolog_regex_based_excludes.rst index 51df5adf716..2537565c945 100644 --- a/logging/monolog_regex_based_excludes.rst +++ b/logging/monolog_regex_based_excludes.rst @@ -16,7 +16,7 @@ configuration: .. code-block:: yaml - # app/config/config.yml + # config/packages/monolog.yaml monolog: handlers: main: @@ -28,7 +28,7 @@ configuration: .. code-block:: xml - + loadFromExtension('monolog', array( 'handlers' => array( 'main' => array( diff --git a/logging/processors.rst b/logging/processors.rst index 309115aa068..ce63343d7ae 100644 --- a/logging/processors.rst +++ b/logging/processors.rst @@ -63,7 +63,6 @@ information: - "[%%datetime%%] [%%extra.token%%] %%channel%%.%%level_name%%: %%message%% %%context%% %%extra%%\n" App\Logger\SessionRequestProcessor: - autowire: true tags: - { name: monolog.processor, method: processRecord } @@ -86,7 +85,7 @@ information: [%%datetime%%] [%%extra.token%%] %%channel%%.%%level_name%%: %%message%% %%context%% %%extra%% - + @@ -103,7 +102,7 @@ information: ->addArgument('[%%datetime%%] [%%extra.token%%] %%channel%%.%%level_name%%: %%message%% %%context%% %%extra%%\n'); $container - ->autowire(SessionRequestProcessor::class) + ->register(SessionRequestProcessor::class) ->addTag('monolog.processor', array('method' => 'processRecord')); Finally, set the formatter to be used on whatever handler you want: @@ -112,7 +111,7 @@ Finally, set the formatter to be used on whatever handler you want: .. code-block:: yaml - # app/config/config.yml + # config/packages/monolog.yaml monolog: handlers: main: @@ -123,7 +122,7 @@ Finally, set the formatter to be used on whatever handler you want: .. code-block:: xml - + loadFromExtension('monolog', array( 'handlers' => array( 'main' => array( @@ -172,16 +171,15 @@ the ``monolog.processor`` tag: .. code-block:: yaml - # app/config/config.yml + # config/services.yaml services: App\Logger\SessionRequestProcessor: - autowire: true tags: - { name: monolog.processor, method: processRecord, handler: main } .. code-block:: xml - + - + @@ -200,11 +198,11 @@ the ``monolog.processor`` tag: .. code-block:: php - // app/config/config.php + // config/services.php // ... $container - ->autowire(SessionRequestProcessor::class) + ->register(SessionRequestProcessor::class) ->addTag('monolog.processor', array('method' => 'processRecord', 'handler' => 'main')); Registering Processors per Channel @@ -217,16 +215,15 @@ the ``monolog.processor`` tag: .. code-block:: yaml - # app/config/config.yml + # config/services.yaml services: App\Logger\SessionRequestProcessor: - autowire: true tags: - { name: monolog.processor, method: processRecord, channel: main } .. code-block:: xml - + - + @@ -245,9 +242,9 @@ the ``monolog.processor`` tag: .. code-block:: php - // app/config/config.php + // config/services.php // ... $container - ->autowire(SessionRequestProcessor::class) + ->register(SessionRequestProcessor::class) ->addTag('monolog.processor', array('method' => 'processRecord', 'channel' => 'main')); From 96ce11e7566ad88d20088c671152f923d01fc161 Mon Sep 17 00:00:00 2001 From: Ryan Weaver Date: Fri, 17 Nov 2017 14:03:35 +0200 Subject: [PATCH 2/2] fixing typo --- logging.rst | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/logging.rst b/logging.rst index 8f81925243f..f4bb0f7ef56 100644 --- a/logging.rst +++ b/logging.rst @@ -64,7 +64,7 @@ to write logs using the :phpfunction:`syslog` function: .. code-block:: yaml - # config/packcages/monolog.yaml + # config/packages/monolog.yaml monolog: handlers: # this "file_log" key could be anything @@ -82,7 +82,7 @@ to write logs using the :phpfunction:`syslog` function: .. code-block:: xml - + loadFromExtension('monolog', array( 'handlers' => array( 'file_log' => array( @@ -140,7 +140,7 @@ one of the messages reaches an ``action_level``. Take this example: .. code-block:: yaml - # config/packcages/monolog.yaml + # config/packages/monolog.yaml monolog: handlers: filter_for_errors: @@ -161,7 +161,7 @@ one of the messages reaches an ``action_level``. Take this example: .. code-block:: xml - + loadFromExtension('monolog', array( 'handlers' => array( 'filter_for_errors' => array( @@ -254,7 +254,7 @@ option of your handler to ``rotating_file``: .. code-block:: yaml - # config/packcages/dev/monolog.yaml + # config/packages/dev/monolog.yaml monolog: handlers: main: @@ -267,7 +267,7 @@ option of your handler to ``rotating_file``: .. code-block:: xml - + loadFromExtension('monolog', array( 'handlers' => array( 'main' => array(