Warning! This page has the following tags:
- This page is deprecated as of Agavi 0.11.0, but might contain useful information.
- This page needs to be either fixed and then merged with docbook documentation, or totally rewritten in docbook documentation.
Setting up logging
Setting up logging can be a bit tricky with Agavi if you haven't done it before. This document attempts to explain via examples how to easily set up a logging system that will log messages to a file.
The LoggerManager is defined in contexts.ini. In the defined sections you will see lines saying logger_manager = "LoggerManager". You can create your own LoggerManager class if you wish and change the above line to point to your own class, although this isn't necessary at all.
Editing settings.ini
Open up config/settings.ini. in the [.settings] section, you'll see USE_LOGGING = "Off". Change this setting to "On". If the setting isn't there, just add it.
Editing logging.ini
Define a section in the file called [logging]. In this section we define names for loggers and set the sections that describe those loggers.
[logging]
applogger = AppLogger
securitylogger = SecurityLogger
Next, we add a section called MyLogger and specify the class handling the logging, the appenders used and the priority at which the logger logs. We'll also add a SecurityLogger section that defines a logger that we use to log security-related things, like for instance (failed) logins, logouts, possible attempts at SQL injection or XSS.
[AppLogger]
class = Logger
appenders = AppFileAppender
priority = Logger::INFO
[SecurityLogger]
class = Logger
appenders = SecurityFileAppender
priority = Logger::INFO
Now we also have to configure the appender class. We have to define the class that handles the appending, a layout for the log format and, since we in this example log to files, tell the logger which file to log to.
[AppFileAppender]
class = FileAppender
layout = MyLayout
param.file = "%AG_WEBAPP_DIR%/logs/application.log"
[SecurityFileAppender]
class = FileAppender
layout = MyLayout
param.file = "%AG_WEBAPP_DIR%/logs/security.log"
Lastly we need to define the layout. Currently Agavi comes with only one layout, PassthruLayout. This layout, like the name indicates, doesn't reformat the message in any way, it just "passes through".
[MyLayout]
class = PassthruLayout
That's it for the configuration part, we are now ready to start logging!
Using the logger
In an Action, View or Model (or anything that has access to the Context), we do
// Fetch our logger
$logger = $this->getContext()->getLoggerManager()->getLogger('applogger');
// Log a message with priority INFO
$logger->log(new Message('Foobar', Logger::INFO));
The above snippet will write the line "Foobar" into the file defined in logging.ini. We can also do things like
$loggerManager = $this->getContext()->getLoggerManager();
$applogger = $loggerManager->getLogger('applogger');
$securitylogger = $loggerManager->getLogger('securitylogger');
// [...]
// we've discovered a possible security breach
// and have information about it in $message
$msg = new Message("Possible security breach detected: $message", Logger::INFO);
$securitylogger->log($msg);
You can easily also make your own layouts that reformat the messages.
<?php
class DateLayout extends Layout
{
public function &format($message)
{
// Yes, the returned string has to be assigned to a variable,
// because Agavi isn't completely E_STRICT compatible yet
$msg = sprintf("[%s] %s", date('Y-m-d H:i:s'), $message->__toString());
return $msg;
}
}
?>
Then simply use add the above class to autoload.ini and set class to DateLayout in the [MyLayout] section above.

