Framework:sb Logger

From Surebert wiki

Contents

Overview

Logging is essential to any application. Classes extending sb_Logger are made to standardize logging interface within a surebert framework application. This makes it easy to switch out logging methods without changing your code.

Creating an Instance

All classes which extend sb_Logger http://surebert.com/svn/sbf/trunk/sb/Logger.php

Creating a logger instance is easy. You could also assign the instance to App::$logger so that it is globally accessible throughout your application.

php code

$logger = new sb_Logger_FileSystem();

Setting the Agent

Everytime the logger logs it saves the datestamp and an agent string along with each message. The default agent string is the IP of the request. You can also specify a more detailed agent string by using the $logger->set_agent_string() method. The one below specifies the user's username and the IP. You could pass any additional information you wish, browser type, etc. Arguments should be separated by a tab.

php code

App::$logger->set_agent_string($uname."\t".Gateway::$remote_addr);

Writng a Message

Once this is set, you can call any of the log types passed to the constructor as methods on the $logger instance and pass it a message to log.

php code

$logger->security('Dictionary hack attempt');

sb_Logger_Filesystem

The default type of logging with the surebert framework is to log inside the projects /private/log function using sb_Logger_Filesystem. This is sufficient for application which do not require an external logging server or database backend. SVN http://surebert.com/svn/sbf/trunk/sb/Logger/FileSystem.php

The following would create a three new directories inside of /private/logs: search, details and security in . MAKE SURE THAT /private/log is writeable by apache. Any day there is a message a new file for that day would be created in the respective folder. When a new day begins, any new messages are written into the new days log.

php code

$logger = new sb_Logger_FileSystem();
$logger->set_agent_string($uname."\t".Gateway::$remote_addr);
$logger->security('Dictionary hack attempt');

If the above code was run on 02/04/09, it would make a file /private/log/security/2009_02_04.log and write the following text inside.

text code

2009/02/04 19:14:56	visco   127.0.0.1
Dictionary hack attempt