Skip to content

Commit

Permalink
Merge branch 'feature/3464-Move-Log-Constants' into 3.1.x
Browse files Browse the repository at this point in the history
  • Loading branch information
kiall committed Dec 7, 2010
2 parents d2ce692 + 0a8186e commit 2238b46
Show file tree
Hide file tree
Showing 7 changed files with 72 additions and 37 deletions.
8 changes: 0 additions & 8 deletions classes/kohana/core.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,6 @@ class Kohana_Core {
const VERSION = '3.1.0';
const CODENAME = 'merle';

// Log message types
const ERROR = 'ERROR';
const DEBUG = 'DEBUG';
const INFO = 'INFO';
const CRITICAL = 'CRITICAL';
const STRACE = 'STRACE';
const ALERT = 'ALERT';

// Common environment type constants for consistency and convenience
const PRODUCTION = 1;
const STAGING = 2;
Expand Down
2 changes: 1 addition & 1 deletion classes/kohana/exception.php
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ public static function handler(Exception $e)
if (is_object(Kohana::$log))
{
// Add this exception to the log
Kohana::$log->add(Kohana::ERROR, $error);
Kohana::$log->add(Log::ERROR, $error);

// Make sure the logs are written
Kohana::$log->write();
Expand Down
42 changes: 29 additions & 13 deletions classes/kohana/log.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,16 @@
*/
class Kohana_Log {

// Log message levels
const EMERGENCY = LOG_EMERG; // 0
const ALERT = LOG_ALERT; // 1
const CRITICAL = LOG_CRIT; // 2
const ERROR = LOG_ERR; // 3
const WARNING = LOG_WARNING; // 4
const NOTICE = LOG_NOTICE; // 5
const INFO = LOG_INFO; // 6
const DEBUG = LOG_DEBUG; // 7

/**
* @var string timestamp format for log entries
*/
Expand Down Expand Up @@ -60,21 +70,27 @@ public static function instance()
protected $_writers = array();

/**
* Attaches a log writer, and optionally limits the types of messages that
* Attaches a log writer, and optionally limits the levels of messages that
* will be written by the writer.
*
* $log->attach($writer);
*
* @param object Log_Writer instance
* @param array messages types to write
* @param object Log_Writer instance
* @param mixed array of messages levels to write OR max level to write
* @param integer min level to write IF $levels is not an array
* @return $this
*/
public function attach(Log_Writer $writer, array $types = NULL)
public function attach(Log_Writer $writer, $levels = array(), $min_level = 0)
{
if ( ! is_array($levels))
{
$levels = range($min_level, $levels);
}

$this->_writers["{$writer}"] = array
(
'object' => $writer,
'types' => $types
'levels' => $levels
);

return $this;
Expand All @@ -100,16 +116,16 @@ public function detach(Log_Writer $writer)
* Adds a message to the log. Replacement values must be passed in to be
* replaced using [strtr](http://php.net/strtr).
*
* $log->add('error', 'Could not locate user: :user', array(
* $log->add(Log::ERROR, 'Could not locate user: :user', array(
* ':user' => $username,
* ));
*
* @param string type of message
* @param string level of message
* @param string message body
* @param array values to replace in the message
* @return $this
*/
public function add($type, $message, array $values = NULL)
public function add($level, $message, array $values = NULL)
{
if ($values)
{
Expand All @@ -120,9 +136,9 @@ public function add($type, $message, array $values = NULL)
// Create a new message and timestamp it
$this->_messages[] = array
(
'time' => Date::formatted_time('now', Log::$timestamp, Log::$timezone),
'type' => $type,
'body' => $message,
'time' => Date::formatted_time('now', Log::$timestamp, Log::$timezone),
'level' => $level,
'body' => $message,
);

if (Log::$write_on_add)
Expand Down Expand Up @@ -157,7 +173,7 @@ public function write()

foreach ($this->_writers as $writer)
{
if (empty($writer['types']))
if (empty($writer['levels']))
{
// Write all of the messages
$writer['object']->write($messages);
Expand All @@ -169,7 +185,7 @@ public function write()

foreach ($messages as $message)
{
if (in_array($message['type'], $writer['types']))
if (in_array($message['level'], $writer['levels']))
{
// Writer accepts this kind of message
$filtered[] = $message;
Expand Down
6 changes: 2 additions & 4 deletions classes/kohana/log/file.php
Original file line number Diff line number Diff line change
Expand Up @@ -82,13 +82,11 @@ public function write(array $messages)
chmod($filename, 0666);
}

// Set the log line format
$format = 'time --- type: body';

foreach ($messages as $message)
{
// Write each message into the log file
file_put_contents($filename, PHP_EOL.strtr($format, $message), FILE_APPEND);
// Format: time --- level: body
file_put_contents($filename, PHP_EOL.$message['time'].' --- '.$this->_log_levels[$message['level']].': '.$message['body'], FILE_APPEND);
}
}

Expand Down
9 changes: 1 addition & 8 deletions classes/kohana/log/syslog.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,6 @@ class Kohana_Log_Syslog extends Log_Writer {
// The syslog identifier
protected $_ident;

protected $_syslog_levels = array('ERROR' => LOG_ERR,
'CRITICAL' => LOG_CRIT,
'STRACE' => LOG_ALERT,
'ALERT' => LOG_WARNING,
'INFO' => LOG_INFO,
'DEBUG' => LOG_DEBUG);

/**
* Creates a new syslog logger.
*
Expand Down Expand Up @@ -47,7 +40,7 @@ public function write(array $messages)
{
foreach ($messages as $message)
{
syslog($this->_syslog_levels[$message['type']], $message['body']);
syslog($message['level'], $message['body']);
}
}

Expand Down
14 changes: 14 additions & 0 deletions classes/kohana/log/writer.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,20 @@
*/
abstract class Kohana_Log_Writer {

/**
* Numeric log level to string lookup table.
* @var array
*/
protected $_log_levels = array(
LOG_EMERG => 'EMERGENCY',
LOG_CRIT => 'CRITICAL',
LOG_ERR => 'ERROR',
LOG_WARNING => 'WARNING',
LOG_NOTICE => 'NOTICE',
LOG_INFO => 'INFO',
LOG_DEBUG => 'DEBUG',
);

/**
* Write an array of messages.
*
Expand Down
28 changes: 25 additions & 3 deletions tests/kohana/LogTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
* @copyright (c) 2008-2010 Kohana Team
* @license http://kohanaframework.org/license
*/
Class Kohana_LogTest extends Unittest_TestCase
class Kohana_LogTest extends Unittest_TestCase
{

/**
Expand Down Expand Up @@ -44,7 +44,7 @@ public function test_writers_is_initially_empty()
}

/**
* Test that attaching a log writer adds it to the array of log writers
* Test that attaching a log writer using an array of levels adds it to the array of log writers
*
* @TODO Is this test too specific?
*
Expand All @@ -59,7 +59,29 @@ public function test_attach_attaches_log_writer_and_returns_this()
$this->assertSame($logger, $logger->attach($writer));

$this->assertAttributeSame(
array(spl_object_hash($writer) => array('object' => $writer, 'types' => NULL)),
array(spl_object_hash($writer) => array('object' => $writer, 'levels' => array())),
'_writers',
$logger
);
}

/**
* Test that attaching a log writer using a min/max level adds it to the array of log writers
*
* @TODO Is this test too specific?
*
* @test
* @covers Log::attach
*/
public function test_attach_attaches_log_writer_min_max_and_returns_this()
{
$logger = new Log;
$writer = $this->getMockForAbstractClass('Log_Writer');

$this->assertSame($logger, $logger->attach($writer, Log::NOTICE, Log::CRITICAL));

$this->assertAttributeSame(
array(spl_object_hash($writer) => array('object' => $writer, 'levels' => array(Log::CRITICAL, Log::ERROR, Log::WARNING, Log::NOTICE))),
'_writers',
$logger
);
Expand Down

0 comments on commit 2238b46

Please sign in to comment.