File log writer. Writes out messages and stores them in a YYYY/MM directory.
Class declared in SYSPATH/classes/Log/File.php on line 3.
int
$strace_levelLevel to use for stack traces
integer 7
string
$timestamptimestamp format for log entries.
Defaults to Date::$timestamp_format
NULL
string
$timezonetimezone for log entries
Defaults to Date::$timezone, which defaults to date_default_timezone_get()
NULL
string
$_directoryDirectory to place log files in
NULL
array
$_log_levelsNumeric log level to string lookup table.
array(8) ( 0 => string(9) "EMERGENCY" 1 => string(5) "ALERT" 2 => string(8) "CRITICAL" 3 => string(5) "ERROR" 4 => string(7) "WARNING" 5 => string(6) "NOTICE" 6 => string(4) "INFO" 7 => string(5) "DEBUG" )
Creates a new file logger. Checks that the directory exists and is writable.
$writer = new Log_File($directory);
string
$directory
required - Log directory void
public function __construct($directory)
{
if (!is_dir($directory) OR ! is_writable($directory)) {
throw new Kohana_Exception('Directory :dir must be writable', [':dir' => Debug::path($directory)]);
}
// Determine the directory path
$this->_directory = realpath($directory) . DIRECTORY_SEPARATOR;
}
Writes each of the messages into the log file. The log file will be
appended to the YYYY/MM/DD.log.php
file, where YYYY is the current
year, MM is the current month, and DD is the current day.
$writer->write($messages);
array
$messages
required - $messages void
public function write(array $messages)
{
// Set the yearly directory name
$directory = $this->_directory . date('Y');
if (!is_dir($directory)) {
// Create the yearly directory
mkdir($directory, 02777);
// Set permissions (must be manually set to fix umask issues)
chmod($directory, 02777);
}
// Add the month to the directory
$directory .= DIRECTORY_SEPARATOR . date('m');
if (!is_dir($directory)) {
// Create the monthly directory
mkdir($directory, 02777);
// Set permissions (must be manually set to fix umask issues)
chmod($directory, 02777);
}
// Set the name of the log file
$filename = $directory . DIRECTORY_SEPARATOR . date('d') . EXT;
if (!file_exists($filename)) {
// Create the log file
file_put_contents($filename, Kohana::FILE_SECURITY . ' ?>' . PHP_EOL);
// Allow anyone to write to log files
chmod($filename, 0666);
}
foreach ($messages as $message) {
// Write each message into the log file
file_put_contents($filename, PHP_EOL . $this->format_message($message), FILE_APPEND);
}
}
Allows the writer to have a unique key when stored.
echo $writer;
string
final public function __toString()
{
return spl_object_hash($this);
}
Formats a log entry.
array
$message
required - $message string
$format
= string(33) "time --- level: body in file:line" - $format string
public function format_message(array $message, $format = "time --- level: body in file:line")
{
$message['time'] = Date::formatted_time('@' . $message['time'], Log_Writer::$timestamp, Log_Writer::$timezone, true);
$message['level'] = $this->_log_levels[$message['level']];
$string = strtr($format, array_filter($message, 'is_scalar'));
if (isset($message['additional']['exception'])) {
// Re-use as much as possible, just resetting the body to the trace
$message['body'] = $message['additional']['exception']->getTraceAsString();
$message['level'] = $this->_log_levels[Log_Writer::$strace_level];
$string .= PHP_EOL . strtr($format, array_filter($message, 'is_scalar'));
}
return $string;
}