PageRenderTime 106ms CodeModel.GetById 39ms app.highlight 12ms RepoModel.GetById 49ms app.codeStats 0ms

/system/classes/Kohana/Log/Writer.php

https://bitbucket.org/chrispiechowicz/zepto
PHP | 95 lines | 34 code | 12 blank | 49 comment | 1 complexity | cd347545dec0e2818f71a85405db8e40 MD5 | raw file
 1<?php defined('SYSPATH') OR die('No direct script access.');
 2/**
 3 * Log writer abstract class. All [Log] writers must extend this class.
 4 *
 5 * @package    Kohana
 6 * @category   Logging
 7 * @author     Kohana Team
 8 * @copyright  (c) 2008-2012 Kohana Team
 9 * @license    http://kohanaframework.org/license
10 */
11abstract class Kohana_Log_Writer {
12
13	/**
14	 * @var  string  timestamp format for log entries.
15	 * 
16	 * Defaults to Date::$timestamp_format
17	 */
18	public static $timestamp;
19
20	/**
21	 * @var  string  timezone for log entries
22	 * 
23	 * Defaults to Date::$timezone, which defaults to date_default_timezone_get()
24	 */
25	public static $timezone;
26
27	/**
28	 * Numeric log level to string lookup table.
29	 * @var array 
30	 */
31	protected $_log_levels = array(
32		LOG_EMERG   => 'EMERGENCY',
33		LOG_ALERT   => 'ALERT',
34		LOG_CRIT    => 'CRITICAL',
35		LOG_ERR     => 'ERROR',
36		LOG_WARNING => 'WARNING',
37		LOG_NOTICE  => 'NOTICE',
38		LOG_INFO    => 'INFO',
39		LOG_DEBUG   => 'DEBUG',
40	);
41
42	/**
43	 * @var  int  Level to use for stack traces
44	 */
45	public static $strace_level = LOG_DEBUG;
46
47	/**
48	 * Write an array of messages.
49	 *
50	 *     $writer->write($messages);
51	 *
52	 * @param   array   $messages
53	 * @return  void
54	 */
55	abstract public function write(array $messages);
56
57	/**
58	 * Allows the writer to have a unique key when stored.
59	 *
60	 *     echo $writer;
61	 *
62	 * @return  string
63	 */
64	final public function __toString()
65	{
66		return spl_object_hash($this);
67	}
68
69	/**
70	 * Formats a log entry.
71	 * 
72	 * @param   array   $message
73	 * @param   string  $format
74	 * @return  string
75	 */
76	public function format_message(array $message, $format = "time --- level: body in file:line")
77	{
78		$message['time'] = Date::formatted_time('@'.$message['time'], Log_Writer::$timestamp, Log_Writer::$timezone, TRUE);
79		$message['level'] = $this->_log_levels[$message['level']];
80
81		$string = strtr($format, $message);
82
83		if (isset($message['additional']['exception']))
84		{
85			// Re-use as much as possible, just resetting the body to the trace
86			$message['body'] = $message['additional']['exception']->getTraceAsString();
87			$message['level'] = $this->_log_levels[Log_Writer::$strace_level];
88
89			$string .= PHP_EOL.strtr($format, $message);
90		}
91
92		return $string;
93	}
94
95} // End Kohana_Log_Writer