模块

Log

带有基于观测器的日志写入功能的消息日志记录。 [!!] 这个类不支持扩展,只是额外的写入器。

package
BootPHP
category
日志
author
Tinsh
copyright
© 2005-2016 Kilofox Studio

该类在 SYSPATH/classes/log.php 第 13 行声明。

常量

EMERGENCY

integer 0

ALERT

integer 1

CRITICAL

integer 2

ERROR

integer 3

WARNING

integer 4

NOTICE

integer 5

INFO

integer 6

DEBUG

integer 7

STRACE

integer 8

属性

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

protected static Log $_instance

Singleton instance container

object Log()

protected array $_messages

list of added messages

protected array $_writers

list of log writers

方法

public add( string $level , string $message [, array $values = NULL ] ) (在 Log 中定义)

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

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

参数

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

返回值

  • Log

源代码

public function add($level, $message, array $values = NULL)
{
	if ($values)
	{
		// Insert the values into the message
		$message = strtr($message, $values);
	}
	// Create a new message && timestamp it
	$this->_messages[] = array
		(
		'time' => Date::formatted_time('now', self::$timestamp, self::$timezone),
		'level' => $level,
		'body' => $message,
	);
	if (self::$write_on_add)
	{
		// Write logs as they are added
		$this->write();
	}
	return $this;
}

public attach( object $writer [, mixed $levels = array(0) , integer $min_level = integer 0 ] ) (在 Log 中定义)

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

$log->attach($writer);

参数

  • object $writer required - Log_Writer instance
  • mixed $levels = array(0) - Array of messages levels to write || max level to write
  • integer $min_level = integer 0 - Min level to write IF $levels is not an array

返回值

  • Log

源代码

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,
		'levels' => $levels
	);
	return $this;
}

public detach( object $writer ) (在 Log 中定义)

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

$log->detach($writer);

参数

  • object $writer required - Log_Writer instance

返回值

  • Log

源代码

public function detach(Log_Writer $writer)
{
	// Remove the writer
	unset($this->_writers["{$writer}"]);
	return $this;
}

public static instance( ) (在 Log 中定义)

Get the singleton instance of this class && enable writing at shutdown.

$log = self::instance();

返回值

  • Log

源代码

public static function instance()
{
	if (self::$_instance === NULL)
	{
		// Create a new instance
		self::$_instance = new Log;
		// Write the logs at shutdown
		register_shutdown_function(array(self::$_instance, 'write'));
	}
	return self::$_instance;
}

public write( ) (在 Log 中定义)

Write && clear all of the messages.

$log->write();

返回值

  • void

源代码

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['levels']))
		{
			// Write all of the messages
			$writer['object']->write($messages);
		}
		else
		{
			// Filtered messages
			$filtered = array();
			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);
		}
	}
}