PageRenderTime 78ms CodeModel.GetById 27ms RepoModel.GetById 1ms app.codeStats 0ms

/system/classes/Kohana/Log.php

https://bitbucket.org/sklyarov_ivan/trap
PHP | 228 lines | 116 code | 26 blank | 86 comment | 11 complexity | 516422060d6c2cb3d85a976e5ffbc6a2 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-2012 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. /**
  24. * @var boolean immediately write when logs are added
  25. */
  26. public static $write_on_add = FALSE;
  27. /**
  28. * @var Log Singleton instance container
  29. */
  30. protected static $_instance;
  31. /**
  32. * Get the singleton instance of this class and enable writing at shutdown.
  33. *
  34. * $log = Log::instance();
  35. *
  36. * @return Log
  37. */
  38. public static function instance()
  39. {
  40. if (Log::$_instance === NULL)
  41. {
  42. // Create a new instance
  43. Log::$_instance = new Log;
  44. // Write the logs at shutdown
  45. register_shutdown_function(array(Log::$_instance, 'write'));
  46. }
  47. return Log::$_instance;
  48. }
  49. /**
  50. * @var array list of added messages
  51. */
  52. protected $_messages = array();
  53. /**
  54. * @var array list of log writers
  55. */
  56. protected $_writers = array();
  57. /**
  58. * Attaches a log writer, and optionally limits the levels of messages that
  59. * will be written by the writer.
  60. *
  61. * $log->attach($writer);
  62. *
  63. * @param Log_Writer $writer instance
  64. * @param mixed $levels array of messages levels to write OR max level to write
  65. * @param integer $min_level min level to write IF $levels is not an array
  66. * @return Log
  67. */
  68. public function attach(Log_Writer $writer, $levels = array(), $min_level = 0)
  69. {
  70. if ( ! is_array($levels))
  71. {
  72. $levels = range($min_level, $levels);
  73. }
  74. $this->_writers["{$writer}"] = array
  75. (
  76. 'object' => $writer,
  77. 'levels' => $levels
  78. );
  79. return $this;
  80. }
  81. /**
  82. * Detaches a log writer. The same writer object must be used.
  83. *
  84. * $log->detach($writer);
  85. *
  86. * @param Log_Writer $writer instance
  87. * @return Log
  88. */
  89. public function detach(Log_Writer $writer)
  90. {
  91. // Remove the writer
  92. unset($this->_writers["{$writer}"]);
  93. return $this;
  94. }
  95. /**
  96. * Adds a message to the log. Replacement values must be passed in to be
  97. * replaced using [strtr](http://php.net/strtr).
  98. *
  99. * $log->add(Log::ERROR, 'Could not locate user: :user', array(
  100. * ':user' => $username,
  101. * ));
  102. *
  103. * @param string $level level of message
  104. * @param string $message message body
  105. * @param array $values values to replace in the message
  106. * @param array $additional additional custom parameters to supply to the log writer
  107. * @return Log
  108. */
  109. public function add($level, $message, array $values = NULL, array $additional = NULL)
  110. {
  111. if ($values)
  112. {
  113. // Insert the values into the message
  114. $message = strtr($message, $values);
  115. }
  116. // Grab a copy of the trace
  117. if (isset($additional['exception']))
  118. {
  119. $trace = $additional['exception']->getTrace();
  120. }
  121. else
  122. {
  123. // Older php version don't have 'DEBUG_BACKTRACE_IGNORE_ARGS', so manually remove the args from the backtrace
  124. if ( ! defined('DEBUG_BACKTRACE_IGNORE_ARGS'))
  125. {
  126. $trace = array_map(function ($item) {
  127. unset($item['args']);
  128. return $item;
  129. }, array_slice(debug_backtrace(FALSE), 1));
  130. }
  131. else
  132. {
  133. $trace = array_slice(debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS), 1);
  134. }
  135. }
  136. if ($additional == NULL)
  137. {
  138. $additional = array();
  139. }
  140. // Create a new message
  141. $this->_messages[] = array
  142. (
  143. 'time' => time(),
  144. 'level' => $level,
  145. 'body' => $message,
  146. 'trace' => $trace,
  147. 'file' => isset($trace[0]['file']) ? $trace[0]['file'] : NULL,
  148. 'line' => isset($trace[0]['line']) ? $trace[0]['line'] : NULL,
  149. 'class' => isset($trace[0]['class']) ? $trace[0]['class'] : NULL,
  150. 'function' => isset($trace[0]['function']) ? $trace[0]['function'] : NULL,
  151. 'additional' => $additional,
  152. );
  153. if (Log::$write_on_add)
  154. {
  155. // Write logs as they are added
  156. $this->write();
  157. }
  158. return $this;
  159. }
  160. /**
  161. * Write and clear all of the messages.
  162. *
  163. * $log->write();
  164. *
  165. * @return void
  166. */
  167. public function write()
  168. {
  169. if (empty($this->_messages))
  170. {
  171. // There is nothing to write, move along
  172. return;
  173. }
  174. // Import all messages locally
  175. $messages = $this->_messages;
  176. // Reset the messages array
  177. $this->_messages = array();
  178. foreach ($this->_writers as $writer)
  179. {
  180. if (empty($writer['levels']))
  181. {
  182. // Write all of the messages
  183. $writer['object']->write($messages);
  184. }
  185. else
  186. {
  187. // Filtered messages
  188. $filtered = array();
  189. foreach ($messages as $message)
  190. {
  191. if (in_array($message['level'], $writer['levels']))
  192. {
  193. // Writer accepts this kind of message
  194. $filtered[] = $message;
  195. }
  196. }
  197. // Write the filtered messages
  198. $writer['object']->write($filtered);
  199. }
  200. }
  201. }
  202. } // End Kohana_Log