This repository has been archived on 2024-04-08. You can view files and clone it, but cannot push or open issues or pull requests.
khosb/includes/kohana/system/classes/Kohana/Log/File.php

94 lines
2.3 KiB
PHP
Raw Normal View History

2012-11-09 12:18:50 +00:00
<?php defined('SYSPATH') OR die('No direct script access.');
2010-08-21 04:43:03 +00:00
/**
* File log writer. Writes out messages and stores them in a YYYY/MM directory.
*
* @package Kohana
* @category Logging
* @author Kohana Team
2012-11-09 12:18:50 +00:00
* @copyright (c) 2008-2012 Kohana Team
2011-05-13 06:00:25 +00:00
* @license http://kohanaframework.org/license
2010-08-21 04:43:03 +00:00
*/
2011-05-13 06:00:25 +00:00
class Kohana_Log_File extends Log_Writer {
2010-08-21 04:43:03 +00:00
2011-05-13 06:00:25 +00:00
/**
* @var string Directory to place log files in
*/
2010-08-21 04:43:03 +00:00
protected $_directory;
/**
* Creates a new file logger. Checks that the directory exists and
* is writable.
*
2011-05-13 06:00:25 +00:00
* $writer = new Log_File($directory);
2010-08-21 04:43:03 +00:00
*
2012-11-09 12:18:50 +00:00
* @param string $directory log directory
2010-08-21 04:43:03 +00:00
* @return void
*/
public function __construct($directory)
{
if ( ! is_dir($directory) OR ! is_writable($directory))
{
throw new Kohana_Exception('Directory :dir must be writable',
2011-05-13 06:00:25 +00:00
array(':dir' => Debug::path($directory)));
2010-08-21 04:43:03 +00:00
}
// 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);
*
2012-11-09 12:18:50 +00:00
* @param array $messages
2010-08-21 04:43:03 +00:00
* @return void
*/
public function write(array $messages)
{
// Set the yearly directory name
2011-05-13 06:00:25 +00:00
$directory = $this->_directory.date('Y');
2010-08-21 04:43:03 +00:00
if ( ! is_dir($directory))
{
// Create the yearly directory
2011-05-13 06:00:25 +00:00
mkdir($directory, 02777);
2010-08-21 04:43:03 +00:00
// Set permissions (must be manually set to fix umask issues)
2011-05-13 06:00:25 +00:00
chmod($directory, 02777);
2010-08-21 04:43:03 +00:00
}
// Add the month to the directory
2011-05-13 06:00:25 +00:00
$directory .= DIRECTORY_SEPARATOR.date('m');
2010-08-21 04:43:03 +00:00
if ( ! is_dir($directory))
{
2012-11-09 12:18:50 +00:00
// Create the monthly directory
2011-05-13 06:00:25 +00:00
mkdir($directory, 02777);
2010-08-21 04:43:03 +00:00
// Set permissions (must be manually set to fix umask issues)
2011-05-13 06:00:25 +00:00
chmod($directory, 02777);
2010-08-21 04:43:03 +00:00
}
// Set the name of the log file
2011-05-13 06:00:25 +00:00
$filename = $directory.DIRECTORY_SEPARATOR.date('d').EXT;
2010-08-21 04:43:03 +00:00
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
2012-11-09 12:18:50 +00:00
file_put_contents($filename, PHP_EOL.$this->format_message($message), FILE_APPEND);
2010-08-21 04:43:03 +00:00
}
}
} // End Kohana_Log_File