AdvancedLogger

Please note, that the AdvancedLogger is marked as deprecated since 1.17. Please use the Logger as of release 1.17 in combination with an appropriate LogWriter implementation.

Due to the fact, that the Logger is not able to address multiple log targets, the 1.8 series now features an extended logger. The AdvancedLogger has different target formats (file, database, stdout) and supports different log formats.

1. Configuration

In order to use the logger, the component must first be configured. For this reason, the configuration file
Code
/config/core/logging/{CONTEXT}/{ENVIRONMENT}_logconfig.ini
must be provided. Thereby, each section contains one definition of a concrete instance of the AdvancedLoggers.


1.1. File logging

In case of file logging, the config file must contain a section as described in the followind code box:
APF configuration
[FileLogger] LogTarget = "file" LogDir = "/path/to/log/dir" LogFileName = "log_file_name" LogType = "true|false" LogTimestamp = "true|false"
The LogTarget defines, that the output of the logger should be stored in a file. The directives LogDir and LogFileName specify the path and name of the log file. Please note, that the log directory must be created prior to use. If LogType is set to true, the log type (severity) is included in the log message, LogTimestamp indicates, if the log message is prefixed with a timestamp.


1.2. Database logging

In case of database logging, the configuration sectio must look like this:
APF configuration
[DatabaseLogger] LogTarget = "database" LogDatabase = "connection_key" LogTable = "log_table_name"
The LogTarget defines, that the output of the logger should be stored in a database. The directives LogDatabase and LogTable specify the target database and table. Thereby, LogDatabase must contain a valid database connection key (see ConnectionManager for more details) and the LogTable contains the log table. In order to use database logging, the target database must contain a log table, that has the following structure:
SQL statement
CREATE TABLE IF NOT EXISTS `<log_table_name>` ( `<primary_key_name>` int(5) NOT NULL auto_increment, `Timestamp` timestamp NOT NULL default CURRENT_TIMESTAMP, `Type` varchar(100) NOT NULL default '', `Message` text NOT NULL, PRIMARY KEY (`<primary_key_name>`) );

1.3. Stdout logging

If you decided to flush the logger's content to the stdout (directly printed to the client in most cases) the configuration file should contain a section like this:
APF configuration
[StdoutLogger] LogTarget = "stdout" LogType = "true|false" LogTimestamp = "true|false"
The LogTarget defines, that the output of the logger should be printed to stdout. If LogType is set to true, the log type (severity) is included in the log message, LogTimestamp indicates, if the log message is prefixed with a timestamp.


2. Usage

To be able to use multiple logger instances within one application, a dedicated AdvancedLogger must be created using the AdvancedLoggerFactory. The factory's challenge is to create and control the logger instances.

In a real life application, there is a need for three different logger with different targets. To achieve this, the configuration file presented below could be used:
APF configuration
[FileLogger] LogTarget = "file" LogDir = "/path/to/my/advanced/logger/log/dir" LogFileName = "special_log" LogType = "false" LogTimestamp = "true" [StdoutLogger] LogTarget = "stdout" LogType = "false" LogTimestamp = "false" [DatabaseLogger] LogTarget = "database" LogDatabase = "AdvancedLogger" LogTable = "log"
Now, the predefined loggers can be used like this within your application and on various places:
PHP code
// get logger factory $logFactory = &$this->getServiceObject('core::logging','AdvancedLoggerFactory'); // create file logger and append message $log1 = &$logFactory->getAdvancedLogger('FileLogger'); $log1->logEntry('user typed studpid things into the given text area!'); // create stdout logger and append message $log2 = &$logFactory->getAdvancedLogger('StdoutLogger'); $log2->logEntry('the remote server didn\'t answer within the timout!'); // create database logger and append message $log3 = &$logFactory->getAdvancedLogger('DatabaseLogger'); $log3->logEntry('current application is doing nonsense!');

Comments

Do you want to add a comment to the article above, or do you want to post additional hints? So please click here. Comments already posted can be found below.
There are no comments belonging to this article.