Kohana_Log
Message logging with observer-based log writing.
This class does not support extensions, only additional writers.
Properties
Methods
Constants
-
EMERGENCY
integer 1
-
ALERT
integer 1
-
CRITICAL
integer 1
-
ERROR
integer 4
-
WARNING
integer 5
-
NOTICE
integer 6
-
INFO
integer 6
-
DEBUG
integer 6
Properties
-
public static boolean $write_on_add
-
immediately write when logs are added
-
bool FALSE
-
protected static Log $_instance
-
Singleton instance container
-
NULL
-
protected array $_messages
-
list of added messages
-
Default value:
array(0)
-
protected array $_writers
-
list of log writers
-
Default value:
array(0)
Methods
public add(string $level , string $message [, array $values = NULL , array $additional = NULL ] ) (defined in Kohana_Log)
Adds a message to the log. Replacement values must be passed in to be replaced using strtr.
$log->add(Log::ERROR, 'Could not locate user: :user', array(
':user' => $username,
));
Parameters
- string $level required - Level of message
- string $message required - Message body
- array $values = NULL - Values to replace in the message
- array $additional = NULL - Additional custom parameters to supply to the log writer
Return Values
- Log
Source Code
public function add($level, $message, array $values = NULL, array $additional = NULL)
{
if ($values)
{
// Insert the values into the message
$message = strtr($message, $values);
}
// Grab a copy of the trace
if (isset($additional['exception']))
{
$trace = $additional['exception']->getTrace();
}
else
{
// Older php version don't have 'DEBUG_BACKTRACE_IGNORE_ARGS', so manually remove the args from the backtrace
if ( ! defined('DEBUG_BACKTRACE_IGNORE_ARGS'))
{
$trace = array_map(function ($item) {
unset($item['args']);
return $item;
}, array_slice(debug_backtrace(FALSE), 1));
}
else
{
$trace = array_slice(debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS), 1);
}
}
if ($additional == NULL)
{
$additional = [];
}
// Create a new message
$this->_messages[] = [
'time' => time(),
'level' => $level,
'body' => $message,
'trace' => $trace,
'file' => isset($trace[0]['file']) ? $trace[0]['file'] : NULL,
'line' => isset($trace[0]['line']) ? $trace[0]['line'] : NULL,
'class' => isset($trace[0]['class']) ? $trace[0]['class'] : NULL,
'function' => isset($trace[0]['function']) ? $trace[0]['function'] : NULL,
'additional' => $additional,
];
if (Log::$write_on_add)
{
// Write logs as they are added
$this->write();
}
return $this;
}
public attach(Log_Writer $writer [, mixed $levels = array(0) , integer $min_level = integer 0 ] ) (defined in Kohana_Log)
Attaches a log writer, and optionally limits the levels of messages that will be written by the writer.
$log->attach($writer);
Parameters
- Log_Writer $writer required - Instance
- mixed $levels = array(0) - Array of messages levels to write OR max level to write
- integer $min_level = integer 0 - Min level to write IF $levels is not an array
Return Values
- Log
Source Code
public function attach(Log_Writer $writer, $levels = [], $min_level = 0)
{
if ( ! is_array($levels))
{
$levels = range($min_level, $levels);
}
$this->_writers["{$writer}"] = [
'object' => $writer,
'levels' => $levels
];
return $this;
}
public detach(Log_Writer $writer ) (defined in Kohana_Log)
Detaches a log writer. The same writer object must be used.
$log->detach($writer);
Parameters
- Log_Writer $writer required - Instance
Return Values
- Log
Source Code
public function detach(Log_Writer $writer)
{
// Remove the writer
unset($this->_writers["{$writer}"]);
return $this;
}
public static instance() (defined in Kohana_Log)
Get the singleton instance of this class and enable writing at shutdown.
$log = Log::instance();
Return Values
- Log
Source Code
public static function instance()
{
if (Log::$_instance === NULL)
{
// Create a new instance
Log::$_instance = new Log;
// Write the logs at shutdown
register_shutdown_function([Log::$_instance, 'write']);
}
return Log::$_instance;
}
public write() (defined in Kohana_Log)
Write and clear all of the messages.
$log->write();
Return Values
- void
Source Code
public function write()
{
if (empty($this->_messages))
{
// There is nothing to write, move along
return;
}
// Import all messages locally
$messages = $this->_messages;
// Reset the messages array
$this->_messages = [];
foreach ($this->_writers as $writer)
{
if (empty($writer['levels']))
{
// Write all of the messages
$writer['object']->write($messages);
}
else
{
// Filtered messages
$filtered = [];
foreach ($messages as $message)
{
if (in_array($message['level'], $writer['levels']))
{
// Writer accepts this kind of message
$filtered[] = $message;
}
}
// Write the filtered messages
$writer['object']->write($filtered);
}
}
}