Skip to content

Updated logging/* articles to Symfony 4 #8683

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 2 commits into from
Nov 27, 2017
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
51 changes: 24 additions & 27 deletions logging.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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 <service-container-services-load-example>`,
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',
Expand All @@ -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 </logging/monolog_email>`).

Expand All @@ -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/environments>` 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
Expand All @@ -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:
Expand All @@ -71,7 +64,7 @@ to write logs using the :phpfunction:`syslog` function:

.. code-block:: yaml

# app/config/config.yml
# config/packages/monolog.yaml
monolog:
handlers:
# this "file_log" key could be anything
Expand All @@ -89,7 +82,7 @@ to write logs using the :phpfunction:`syslog` function:

.. code-block:: xml

<!-- app/config/config.xml -->
<!-- config/packages/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 @@ -116,7 +109,7 @@ to write logs using the :phpfunction:`syslog` function:

.. code-block:: php

// app/config/config.php
// config/packages/monolog.php
$container->loadFromExtension('monolog', array(
'handlers' => array(
'file_log' => array(
Expand Down Expand Up @@ -147,7 +140,7 @@ one of the messages reaches an ``action_level``. Take this example:

.. code-block:: yaml

# app/config/config.yml
# config/packages/monolog.yaml
monolog:
handlers:
filter_for_errors:
Expand All @@ -168,7 +161,7 @@ one of the messages reaches an ``action_level``. Take this example:

.. code-block:: xml

<!-- app/config/config.xml -->
<!-- config/packages/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 Down Expand Up @@ -201,7 +194,7 @@ one of the messages reaches an ``action_level``. Take this example:

.. code-block:: php

// app/config/config.php
// config/packages/monolog.php
$container->loadFromExtension('monolog', array(
'handlers' => array(
'filter_for_errors' => array(
Expand Down Expand Up @@ -261,7 +254,7 @@ option of your handler to ``rotating_file``:

.. code-block:: yaml

# app/config/config_dev.yml
# config/packages/dev/monolog.yaml
monolog:
handlers:
main:
Expand All @@ -274,7 +267,7 @@ option of your handler to ``rotating_file``:

.. code-block:: xml

<!-- app/config/config_dev.xml -->
<!-- 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 @@ -298,7 +291,7 @@ option of your handler to ``rotating_file``:

.. code-block:: php

// app/config/config_dev.php
// config/packages/dev/monolog.php
$container->loadFromExtension('monolog', array(
'handlers' => array(
'main' => array(
Expand All @@ -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 <dic_tags-monolog>`.
Expand All @@ -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
Expand Down
15 changes: 7 additions & 8 deletions logging/channels_handlers.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -49,7 +48,7 @@ in all environments, or just ``config_prod.yml`` to happen only in ``prod``:

.. code-block:: xml

<!-- app/config/config.xml -->
<!-- config/packages/monolog.xml-->
<container xmlns="http://symfony.com/schema/dic/services"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:monolog="http://symfony.com/schema/dic/monolog"
Expand All @@ -76,7 +75,7 @@ in all environments, or just ``config_prod.yml`` to happen only in ``prod``:

.. code-block:: php

// app/config/config.php
// config/packages/monolog.php
$container->loadFromExtension('monolog', array(
'handlers' => array(
'security' => array(
Expand Down Expand Up @@ -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

<!-- app/config/config.xml -->
<!-- config/packages/monolog.xml -->
<container xmlns="http://symfony.com/schema/dic/services"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:monolog="http://symfony.com/schema/dic/monolog"
Expand All @@ -161,7 +160,7 @@ You can also configure additional channels without the need to tag your services

.. code-block:: php

// app/config/config.php
// config/packages/monolog.php
$container->loadFromExtension('monolog', array(
'channels' => array(
'foo',
Expand Down
27 changes: 6 additions & 21 deletions logging/disable_microsecond_precision.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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

<!-- app/config/config.xml -->
<!-- config/packages/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,25 +29,14 @@ log generation. This is recommended for systems that generate a large number of
http://symfony.com/schema/dic/monolog/monolog-1.0.xsd">

<monolog:config use-microseconds="false">
<monolog:handler
name="applog"
type="stream"
path="/var/log/symfony.log"
level="error"
/>
<!-- ... -->
</monolog:config>
</container>

.. 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',
),
),
// ...
));
9 changes: 4 additions & 5 deletions logging/formatter.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -44,7 +44,7 @@ configure your handler to use it:
<service id="Monolog\Formatter\JsonFormatter" />
</services>

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

.. code-block:: php

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

// config/services.php
$container->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(
Expand Down
25 changes: 15 additions & 10 deletions logging/monolog_console.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is becoming quite unreadable. Can we please at the class definition (class .... { }) lines?


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
Expand All @@ -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:
# ...
Expand All @@ -75,7 +80,7 @@ example, in ``config_dev.yml``:

.. code-block:: xml

<!-- app/config/config.xml -->
<!-- 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 @@ -98,7 +103,7 @@ example, in ``config_dev.yml``:

.. code-block:: php

// app/config/config.php
// config/packages/dev/monolog.php
$container->loadFromExtension('monolog', array(
'handlers' => array(
'console' => array(
Expand Down
Loading