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 :
sfContext::getInstance()->getEventDispatcher()->getListeners(‘application.log‘)
Array
(
[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 :
prod:
logger:
class: sfAggregateLogger
param:
level: debug
loggers:
sf_file_debug:
class: sfFileLogger
param:
level: debug
file: %SF_LOG_DIR%/%SF_APP%_%SF_ENVIRONMENT%.log
sf_file_info:
class: sfFileLogger
param:
level: debug
file: %SF_LOG_DIR%/anotherlogfile.log
One Reply
[...] Logging in Symfony 1.1 [...]