Modules

Kohana_Log

Message logging with observer-based log writing.

This class does not support extensions, only additional writers.

package
Kohana
category
Logging
author
Kohana Team
copyright
© 2008-2010 Kohana Team
license
http://kohanaframework.org/license

Class declared in SYSPATH/classes/kohana/log.php on line 13.

Properties

public static string $timestamp

timestamp format for log entries

string(11) "Y-m-d H:i:s"

public static string $timezone

timezone for log entries

NULL

public static boolean $write_on_add

immediately write when logs are added

bool FALSE

private static Kohana_Log $_instance

Singleton instance container

object Kohana_Log()

private array $_messages

list of added messages

private array $_writers

list of log writers

Methods

public add( string $type , string $message [, array $values = NULL ] ) (defined in Kohana_Log)

Adds a message to the log. Replacement values must be passed in to be replaced using strtr.

$log->add('error', 'Could not locate user: :user', array(
    ':user' => $username,
));

Parameters

  • string $type required - Type of message
  • string $message required - Message body
  • array $values = NULL - Values to replace in the message

Return Values

  • Kohana_Log

Source Code

public function add($type, $message, array $values = NULL)
{
	if ($values)
	{
		// Insert the values into the message
		$message = strtr($message, $values);
	}

	// Create a new message and timestamp it
	$this->_messages[] = array
	(
		'time' => Date::formatted_time('now', self::$timestamp, self::$timezone),
		'type' => $type,
		'body' => $message,
	);

	if (self::$write_on_add)
	{
		// Write logs as they are added
		$this->write();
	}

	return $this;
}

public attach( object $writer [, array $types = NULL ] ) (defined in Kohana_Log)

Attaches a log writer, and optionally limits the types of messages that will be written by the writer.

$log->attach($writer);

Parameters

  • object $writer required - Kohana_Log_Writer instance
  • array $types = NULL - Messages types to write

Return Values

  • Kohana_Log

Source Code

public function attach(Kohana_Log_Writer $writer, array $types = NULL)
{
	$this->_writers["{$writer}"] = array
	(
		'object' => $writer,
		'types' => $types
	);

	return $this;
}

public detach( object $writer ) (defined in Kohana_Log)

Detaches a log writer. The same writer object must be used.

$log->detach($writer);

Parameters

  • object $writer required - Kohana_Log_Writer instance

Return Values

  • Kohana_Log

Source Code

public function detach(Kohana_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 = Kohana_Log::instance();

Return Values

  • Kohana_Log

Source Code

public static function instance()
{
	if (self::$_instance === NULL)
	{
		// Create a new instance
		self::$_instance = new self;

		// Write the logs at shutdown
		register_shutdown_function(array(self::$_instance, 'write'));
	}

	return self::$_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 = array();

	foreach ($this->_writers as $writer)
	{
		if (empty($writer['types']))
		{
			// Write all of the messages
			$writer['object']->write($messages);
		}
		else
		{
			// Filtered messages
			$filtered = array();

			foreach ($messages as $message)
			{
				if (in_array($message['type'], $writer['types']))
				{
					// Writer accepts this kind of message
					$filtered[] = $message;
				}
			}

			// Write the filtered messages
			$writer['object']->write($filtered);
		}
	}
}