Symfony logging tutorial

Symfony logs can be a very helpful tool when you are developing and/or testing.

Writing to a symfony log


The dev log file is written to whenever any code in Symfony writes using the Monolog service (in the dev environment):

$this->get('logger')->info("hello world");

Type the following command in a terminal, and it will watch the dev log file, spitting out the contents of the log file as the contents are written.

tail -f PATH_TO_YOUR_APP/logs/dev.log


Things to know about the Monolog Service


There are some behaviours with logging using the Monolog service you should be aware of.

  • when in production, logger->info() messages are simply discarded (for performance)
  • when in production, logger->err() messages can trigger emails, indicating the message being logged
  • when in production, if logger->err() is called, monolog *will* write any logger->info() messages to the prod.log file, that were leading up to when the logger->err() was called
  • when *not* in production, logger->err() and logger->info() write the error into the corresponding test.log or dev.log file
  • logging with monolog is like writing a trace of activity in the application (take a look at the existing log files, and you will see what I mean)
  • use it when a critical error occurs, and you need to be notified immediately
    $this->get('logger')->err("the database is not responding");

For information on configuring Monolog to email production errors: http://symfony.com/doc/2.0/cookbook/logging/monolog_email.html

For information on Monolog and Symfony: http://symfony.com/doc/2.0/cookbook/logging/monolog.html

No comments:

Post a Comment