PageRenderTime 49ms CodeModel.GetById 23ms RepoModel.GetById 1ms app.codeStats 0ms

/system/classes/kohana/log.php

https://bitbucket.org/sudak/rating
PHP | 206 lines | 91 code | 26 blank | 89 comment | 7 complexity | 9651e2c9aa8e39b2f22ad6a26b52f669 MD5 | raw file
Possible License(s): BSD-3-Clause
  1. <?php defined('SYSPATH') or die('No direct script access.');
  2. /**
  3. * Message logging with observer-based log writing.
  4. *
  5. * [!!] This class does not support extensions, only additional writers.
  6. *
  7. * @package Kohana
  8. * @category Logging
  9. * @author Kohana Team
  10. * @copyright (c) 2008-2011 Kohana Team
  11. * @license http://kohanaframework.org/license
  12. */
  13. class Kohana_Log {
  14. // Log message levels - Windows users see PHP Bug #18090
  15. const EMERGENCY = LOG_EMERG; // 0
  16. const ALERT = LOG_ALERT; // 1
  17. const CRITICAL = LOG_CRIT; // 2
  18. const ERROR = LOG_ERR; // 3
  19. const WARNING = LOG_WARNING; // 4
  20. const NOTICE = LOG_NOTICE; // 5
  21. const INFO = LOG_INFO; // 6
  22. const DEBUG = LOG_DEBUG; // 7
  23. const STRACE = 8;
  24. /**
  25. * @var string timestamp format for log entries
  26. */
  27. public static $timestamp = 'Y-m-d H:i:s';
  28. /**
  29. * @var string timezone for log entries
  30. */
  31. public static $timezone;
  32. /**
  33. * @var boolean immediately write when logs are added
  34. */
  35. public static $write_on_add = FALSE;
  36. /**
  37. * @var Log Singleton instance container
  38. */
  39. protected static $_instance;
  40. /**
  41. * Get the singleton instance of this class and enable writing at shutdown.
  42. *
  43. * $log = Log::instance();
  44. *
  45. * @return Log
  46. */
  47. public static function instance()
  48. {
  49. if (Log::$_instance === NULL)
  50. {
  51. // Create a new instance
  52. Log::$_instance = new Log;
  53. // Write the logs at shutdown
  54. register_shutdown_function(array(Log::$_instance, 'write'));
  55. }
  56. return Log::$_instance;
  57. }
  58. /**
  59. * @var array list of added messages
  60. */
  61. protected $_messages = array();
  62. /**
  63. * @var array list of log writers
  64. */
  65. protected $_writers = array();
  66. /**
  67. * Attaches a log writer, and optionally limits the levels of messages that
  68. * will be written by the writer.
  69. *
  70. * $log->attach($writer);
  71. *
  72. * @param object Log_Writer instance
  73. * @param mixed array of messages levels to write OR max level to write
  74. * @param integer min level to write IF $levels is not an array
  75. * @return Log
  76. */
  77. public function attach(Log_Writer $writer, $levels = array(), $min_level = 0)
  78. {
  79. if ( ! is_array($levels))
  80. {
  81. $levels = range($min_level, $levels);
  82. }
  83. $this->_writers["{$writer}"] = array
  84. (
  85. 'object' => $writer,
  86. 'levels' => $levels
  87. );
  88. return $this;
  89. }
  90. /**
  91. * Detaches a log writer. The same writer object must be used.
  92. *
  93. * $log->detach($writer);
  94. *
  95. * @param object Log_Writer instance
  96. * @return Log
  97. */
  98. public function detach(Log_Writer $writer)
  99. {
  100. // Remove the writer
  101. unset($this->_writers["{$writer}"]);
  102. return $this;
  103. }
  104. /**
  105. * Adds a message to the log. Replacement values must be passed in to be
  106. * replaced using [strtr](http://php.net/strtr).
  107. *
  108. * $log->add(Log::ERROR, 'Could not locate user: :user', array(
  109. * ':user' => $username,
  110. * ));
  111. *
  112. * @param string level of message
  113. * @param string message body
  114. * @param array values to replace in the message
  115. * @return Log
  116. */
  117. public function add($level, $message, array $values = NULL)
  118. {
  119. if ($values)
  120. {
  121. // Insert the values into the message
  122. $message = strtr($message, $values);
  123. }
  124. // Create a new message and timestamp it
  125. $this->_messages[] = array
  126. (
  127. 'time' => Date::formatted_time('now', Log::$timestamp, Log::$timezone),
  128. 'level' => $level,
  129. 'body' => $message,
  130. );
  131. if (Log::$write_on_add)
  132. {
  133. // Write logs as they are added
  134. $this->write();
  135. }
  136. return $this;
  137. }
  138. /**
  139. * Write and clear all of the messages.
  140. *
  141. * $log->write();
  142. *
  143. * @return void
  144. */
  145. public function write()
  146. {
  147. if (empty($this->_messages))
  148. {
  149. // There is nothing to write, move along
  150. return;
  151. }
  152. // Import all messages locally
  153. $messages = $this->_messages;
  154. // Reset the messages array
  155. $this->_messages = array();
  156. foreach ($this->_writers as $writer)
  157. {
  158. if (empty($writer['levels']))
  159. {
  160. // Write all of the messages
  161. $writer['object']->write($messages);
  162. }
  163. else
  164. {
  165. // Filtered messages
  166. $filtered = array();
  167. foreach ($messages as $message)
  168. {
  169. if (in_array($message['level'], $writer['levels']))
  170. {
  171. // Writer accepts this kind of message
  172. $filtered[] = $message;
  173. }
  174. }
  175. // Write the filtered messages
  176. $writer['object']->write($filtered);
  177. }
  178. }
  179. }
  180. } // End Kohana_Log