Logging in Symfony 1.1

As described in the book, the logging system on Symfony 1.1 is based on the new Event system.

If you are not familiar with events, I recommand that you read very carefully that part to understand the mechanisms.

Litteraly, logging something means sending a logging notification to the logging listener. It is done by passing an instance of the sfEvent class to the sfEventDispatcher of the application context.

  • 1.0 way of logging :
$this->getContext()->getLogger()->info({sfController} dispatch request‘);
  • 1.1 way of logging :
$this->dispatcher->notify(new sfEvent($this, ‘application.log‘, array(‘Initialization‘)));

What is application.log ? It is not the actual log file, as you may think. It is in fact the name of event defined in the sfLogger class :

$dispatcher->connect(‘application.log‘, array($this, ‘listenToLogEvent‘));

application.log is one of Symfony built-in events. Here are the default listeners defined in a Symfony application’s event dispatcher :

  • routing.load_configuration
  • application.log
  • view.cache.filter_content
  • response.filter_content
  • user.change_culture
  • controller.change_action
  • request.filter_parameters
  • context.load_factories
  • template.filter_parameters

application.log is the main listener of what’s happening in your Symfony application since almost every class of the framework notify it when it does something :

  • controller initialization,
  • view decoration,
  • actions calls,
  • routing connections,
  • response sending,
  • user credentials/flash handling,
  • cache saving and discarding

Here is the default implementation of the application.log listener :


  [0] => Array
      [0] => sfAggregateLogger Object
          [dispatcher:protected] => sfEventDispatcher Object ()
          [loggers:protected] => Array
              [0] => sfFileLogger Object
                  [type:protected] => symfony
                  [format:protected] => %time% %type% [%priority%] %message%%EOL%
                  [timeFormat:protected] => %b %d %H:%M:%S
                  [fp:protected] => Resource id #45
                  [level:protected] => 6
          [level:protected] => 7
        [1] => listenToLogEvent

When notified, it will trigger the sfAggregateLogger::listenToLogEvent() method.

The sfAggregateLogger is a logger holder that contains several loggers objects. When triggered, the sfAggregateLogger will call all its loggers. It’s pretty convenient if you want to log the same informations but in different ways (on a file and sending an email simultanously for example).

By default in your application, it contains one sfFileLogger object that will log into a file (sf_root_dir/log/<application>_<environnement>.log).

application.log is defined in the logger section of your application’s factories.yml, so you can easily append another logger, depending or not on the context :

    class:   sfAggregateLogger
    level:   debug
        class: sfFileLogger
        level: debug
        file: %SF_LOG_DIR%/%SF_APP%_%SF_ENVIRONMENT%.log
       class: sfFileLogger   
       level: debug
       file: %SF_LOG_DIR%/anotherlogfile.log

  1. 1 rpsblog.com » A week of symfony #85 (11-&gt;17 august 2008)

    […] Logging in Symfony 1.1 […]

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: