Logging

Symfony comes with a minimalist PSR-3 logger: Logger.In conformance with the twelve-factor app methodology, it sends messages starting from theWARNING level to stderr).

The minimal log level can be changed by setting the SHELL_VERBOSITY environment variable:

SHELL_VERBOSITY valueMinimum log level
-1ERROR
1NOTICE
2INFO
3DEBUG

The minimum log level, the default output and the log format can also be changed bypassing the appropriate arguments to the constructor of Logger.To do so, override the "logger" service definition.

Logging a Message

To log a message, inject the default logger in your controller:

  1. use Psr\Log\LoggerInterface;
  2.  
  3. public function index(LoggerInterface $logger)
  4. {
  5. $logger->info('I just got the logger');
  6. $logger->error('An error occurred');
  7.  
  8. $logger->critical('I left the oven on!', [
  9. // include extra "context" info in your logs
  10. 'cause' => 'in_hurry',
  11. ]);
  12.  
  13. // ...
  14. }

The logger service has different methods for different logging levels/priorities.See LoggerInterface for a list of all of the methods on the logger.

Monolog

Symfony integrates seamlessly with Monolog, the most popular PHP logginglibrary, to create and store log messages in a variety of different placesand trigger various actions.

For instance, using Monolog you can configure the logger to do different things based on thelevel of a message (e.g. send an email when an error occurs).

Run this command to install the Monolog based logger before using it:

  1. $ composer require symfony/monolog-bundle

The following sections assume that Monolog is installed.

Where Logs are Stored

By default, log entries are written to the var/log/dev.log file when you're inthe 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(i.e. error() , critical(), alert() or emergency()).

To control this, you'll configure different handlers that handle log entries, sometimesmodify them, and ultimately store them.

Handlers: Writing Logs to different Locations

The logger has a stack of handlers, and each can be used to write the log entriesto different locations (e.g. files, database, Slack, etc).

Tip

You can also configure logging "channels", which are like categories. Eachchannel can have its own handlers, which means you can store different logmessages in different places. See How to Log Messages to different Files.

Symfony pre-configures some basic handlers in the default monolog.yamlconfig files. Check these out for some real-world examples.

This example uses two handlers: stream (to write to a file) and syslogto write logs using the syslog function:

  • YAML
  1. # config/packages/prod/monolog.yaml
  2. monolog:
  3. handlers:
  4. # this "file_log" key could be anything
  5. file_log:
  6. type: stream
  7. # log to var/log/(environment).log
  8. path: "%kernel.logs_dir%/%kernel.environment%.log"
  9. # log *all* messages (debug is lowest level)
  10. level: debug
  11.  
  12. syslog_handler:
  13. type: syslog
  14. # log error-level messages and higher
  15. level: error
  • XML
  1. <!-- config/packages/prod/monolog.xml -->
  2. <?xml version="1.0" encoding="UTF-8" ?>
  3. <container xmlns="http://symfony.com/schema/dic/services"
  4. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  5. xmlns:monolog="http://symfony.com/schema/dic/monolog"
  6. xsi:schemaLocation="http://symfony.com/schema/dic/services
  7. https://symfony.com/schema/dic/services/services-1.0.xsd
  8. http://symfony.com/schema/dic/monolog
  9. https://symfony.com/schema/dic/monolog/monolog-1.0.xsd">
  10.  
  11. <monolog:config>
  12. <monolog:handler
  13. name="file_log"
  14. type="stream"
  15. path="%kernel.logs_dir%/%kernel.environment%.log"
  16. level="debug"
  17. />
  18. <monolog:handler
  19. name="syslog_handler"
  20. type="syslog"
  21. level="error"
  22. />
  23. </monolog:config>
  24. </container>
  • PHP
  1. // config/packages/prod/monolog.php
  2. $container->loadFromExtension('monolog', [
  3. 'handlers' => [
  4. 'file_log' => [
  5. 'type' => 'stream',
  6. 'path' => '%kernel.logs_dir%/%kernel.environment%.log',
  7. 'level' => 'debug',
  8. ],
  9. 'syslog_handler' => [
  10. 'type' => 'syslog',
  11. 'level' => 'error',
  12. ],
  13. ],
  14. ]);

This defines a stack of handlers and each handler is called in the order that it'sdefined.

Handlers that Modify Log Entries

Instead of writing log files somewhere, some handlers are used to filter or modifylog entries before sending them to other handlers. One powerful, built-in handlercalled fingerscrossed is used in the prod environment by default. It stores_all log messages during a request but only passes them to a second handler ifone of the messages reaches an action_level. Take this example:

  • YAML
  1. # config/packages/prod/monolog.yaml
  2. monolog:
  3. handlers:
  4. filter_for_errors:
  5. type: fingers_crossed
  6. # if *one* log is error or higher, pass *all* to file_log
  7. action_level: error
  8. handler: file_log
  9.  
  10. # now passed *all* logs, but only if one log is error or higher
  11. file_log:
  12. type: stream
  13. path: "%kernel.logs_dir%/%kernel.environment%.log"
  14.  
  15. # still passed *all* logs, and still only logs error or higher
  16. syslog_handler:
  17. type: syslog
  18. level: error
  • XML
  1. <!-- config/packages/prod/monolog.xml -->
  2. <?xml version="1.0" encoding="UTF-8" ?>
  3. <container xmlns="http://symfony.com/schema/dic/services"
  4. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  5. xmlns:monolog="http://symfony.com/schema/dic/monolog"
  6. xsi:schemaLocation="http://symfony.com/schema/dic/services
  7. https://symfony.com/schema/dic/services/services-1.0.xsd
  8. http://symfony.com/schema/dic/monolog
  9. https://symfony.com/schema/dic/monolog/monolog-1.0.xsd">
  10.  
  11. <monolog:config>
  12. <monolog:handler
  13. name="filter_for_errors"
  14. type="fingers_crossed"
  15. action-level="error"
  16. handler="file_log"
  17. />
  18. <monolog:handler
  19. name="file_log"
  20. type="stream"
  21. path="%kernel.logs_dir%/%kernel.environment%.log"
  22. level="debug"
  23. />
  24. <monolog:handler
  25. name="syslog_handler"
  26. type="syslog"
  27. level="error"
  28. />
  29. </monolog:config>
  30. </container>
  • PHP
  1. // config/packages/prod/monolog.php
  2. $container->loadFromExtension('monolog', [
  3. 'handlers' => [
  4. 'filter_for_errors' => [
  5. 'type' => 'fingers_crossed',
  6. 'action_level' => 'error',
  7. 'handler' => 'file_log',
  8. ],
  9. 'file_log' => [
  10. 'type' => 'stream',
  11. 'path' => '%kernel.logs_dir%/%kernel.environment%.log',
  12. 'level' => 'debug',
  13. ],
  14. 'syslog_handler' => [
  15. 'type' => 'syslog',
  16. 'level' => 'error',
  17. ],
  18. ],
  19. ]);

Now, if even one log entry has an error level or higher, then all log entriesfor that request are saved to a file via the filelog handler. That means thatyour log file will contain _all the details about the problematic request - makingdebugging much easier!

Tip

The handler named "file_log" will not be included in the stack itself asit is used as a nested handler of the fingers_crossed handler.

Note

If you want to override the monolog configuration via another configfile, you will need to redefine the entire handlers stack. The configurationfrom the two files cannot be merged because the order matters and a merge doesnot allow to control the order.

All Built-in Handlers

Monolog comes with many built-in handlers for emailing logs, sending them to Loggly,or notifying you in Slack. These are documented inside of MonologBundle itself. Fora full list, see Monolog Configuration.

How to Rotate your Log Files

Over time, log files can grow to be huge, both while developing and onproduction. One best-practice solution is to use a tool like the logrotateLinux command to rotate log files before they become too large.

Another option is to have Monolog rotate the files for you by using therotating_file handler. This handler creates a new log file every dayand can also remove old files automatically. To use it, set the typeoption of your handler to rotating_file:

  • YAML
  1. # config/packages/prod/monolog.yaml
  2. monolog:
  3. handlers:
  4. main:
  5. type: rotating_file
  6. path: '%kernel.logs_dir%/%kernel.environment%.log'
  7. level: debug
  8. # max number of log files to keep
  9. # defaults to zero, which means infinite files
  10. max_files: 10
  • XML
  1. <!-- config/packages/prod/monolog.xml -->
  2. <?xml version="1.0" encoding="UTF-8" ?>
  3. <container xmlns="http://symfony.com/schema/dic/services"
  4. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  5. xmlns:monolog="http://symfony.com/schema/dic/monolog"
  6. xsi:schemaLocation="http://symfony.com/schema/dic/services
  7. https://symfony.com/schema/dic/services/services-1.0.xsd
  8. http://symfony.com/schema/dic/monolog
  9. https://symfony.com/schema/dic/monolog/monolog-1.0.xsd">
  10.  
  11. <monolog:config>
  12. <!-- "max-files": max number of log files to keep
  13. defaults to zero, which means infinite files -->
  14. <monolog:handler name="main"
  15. type="rotating_file"
  16. path="%kernel.logs_dir%/%kernel.environment%.log"
  17. level="debug"
  18. max-files="10"
  19. />
  20. </monolog:config>
  21. </container>
  • PHP
  1. // config/packages/prod/monolog.php
  2. $container->loadFromExtension('monolog', [
  3. 'handlers' => [
  4. 'main' => [
  5. 'type' => 'rotating_file',
  6. 'path' => '%kernel.logs_dir%/%kernel.environment%.log',
  7. 'level' => 'debug',
  8. // max number of log files to keep
  9. // defaults to zero, which means infinite files
  10. 'max_files' => 10,
  11. ],
  12. ],
  13. ]);

Using a Logger inside a Service

If your application uses service autoconfiguration,any service whose class implements Psr\Log\LoggerAwareInterface willreceive a call to its method setLogger() with the default logger servicepassed as a service.

New in version 4.2: The automatic call to setLogger() when implementing LoggerAwareInterfacewas introduced in Symfony 4.2.

If you want to use in your own services a pre-configured logger which uses aspecific channel (app by default), you can either autowire monolog channelsor use the monolog.logger tag with the channel property as explained in theDependency Injection reference.

Adding extra Data to each Log (e.g. a unique request token)

Monolog also supports processors: functions that can dynamically add extrainformation to your log entries.

See How to Add extra Data to Log Messages via a Processor for details.

Learn more