PHPTools‎ > ‎

Logger

Logger is a simple PHP class that allows logging various types of messages to a database table. In order to use Logger you'll need to create the database table for it. In order to do this you need to import the SQL in phptools/logger/logger.sql into a database of your choosing this will create the necessary table structure. Then it needs to have access to the database object, this must be a an instance of the DB class that's included with phptools. In order to do this you must set a callback on the Logger object you can see an example of this below.

$callback = function(){
    return DB::connect('mysql://user:password@db_server/database');
};
Logger::setDbObjectCallback($callback);
Logger::setDbTableName('logger');

Once Logger has a database object then it can do it's thing. There are four types of log messages debug, info, warn, and error. Each of these has it's own method which are described below.

Methods

debug($message)

    Logs the output of debug_backtrace() for debugging purposes. In order to prevent from logging debug messages in higher environments debug will not log anything unless you set the debug flag by calling setDebug(true), you can turn it off by calling the same method with false as the parameter. $message takes a simple string to describe the backtrace.

error($message, $detail = null)

    Used to log error messages, $message is a string used to describe the message and the optional $detail parameter is used to log variables. If $detail is an array or an object it's passed through Dump so we can output the entire structure of the variable.

info($message, $detail = null)

    Used to log informational messages, $message is a string used to describe the message and the optional $detail parameter is used to log variables. If $detail is an array or an object it's passed through Dump so we can output the entire structure of the variable.

setDbObjectCallback($callback)

    Set the $callback that Logger uses to get access to our database object.

setDebug($value)

    Turn debug mode on or off. It gets set to the boolean conversion of $value.

warn($message, $detail = null)

    Used to log warning messages, $message is a string used to describe the message and the optional $detail parameter is used to log variables. If $detail is an array or an object it's passed through Dump so we can output the entire structure of the variable.
Comments