PageRenderTime 43ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 0ms

/htdocs/kohana/3.1.3.1/system/classes/kohana/log.php

http://github.com/pmjones/php-framework-benchmarks
PHP | 205 lines | 90 code | 26 blank | 89 comment | 7 complexity | 310ac8908e4f8f65a6ba411f574f1035 MD5 | raw file
Possible License(s): LGPL-3.0, Apache-2.0, BSD-3-Clause, ISC, AGPL-3.0, LGPL-2.1
  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. /**
  24. * @var string timestamp format for log entries
  25. */
  26. public static $timestamp = 'Y-m-d H:i:s';
  27. /**
  28. * @var string timezone for log entries
  29. */
  30. public static $timezone;
  31. /**
  32. * @var boolean immediately write when logs are added
  33. */
  34. public static $write_on_add = FALSE;
  35. /**
  36. * @var Log Singleton instance container
  37. */
  38. protected static $_instance;
  39. /**
  40. * Get the singleton instance of this class and enable writing at shutdown.
  41. *
  42. * $log = Log::instance();
  43. *
  44. * @return Log
  45. */
  46. public static function instance()
  47. {
  48. if (Log::$_instance === NULL)
  49. {
  50. // Create a new instance
  51. Log::$_instance = new Log;
  52. // Write the logs at shutdown
  53. register_shutdown_function(array(Log::$_instance, 'write'));
  54. }
  55. return Log::$_instance;
  56. }
  57. /**
  58. * @var array list of added messages
  59. */
  60. protected $_messages = array();
  61. /**
  62. * @var array list of log writers
  63. */
  64. protected $_writers = array();
  65. /**
  66. * Attaches a log writer, and optionally limits the levels of messages that
  67. * will be written by the writer.
  68. *
  69. * $log->attach($writer);
  70. *
  71. * @param object Log_Writer instance
  72. * @param mixed array of messages levels to write OR max level to write
  73. * @param integer min level to write IF $levels is not an array
  74. * @return Log
  75. */
  76. public function attach(Log_Writer $writer, $levels = array(), $min_level = 0)
  77. {
  78. if ( ! is_array($levels))
  79. {
  80. $levels = range($min_level, $levels);
  81. }
  82. $this->_writers["{$writer}"] = array
  83. (
  84. 'object' => $writer,
  85. 'levels' => $levels
  86. );
  87. return $this;
  88. }
  89. /**
  90. * Detaches a log writer. The same writer object must be used.
  91. *
  92. * $log->detach($writer);
  93. *
  94. * @param object Log_Writer instance
  95. * @return Log
  96. */
  97. public function detach(Log_Writer $writer)
  98. {
  99. // Remove the writer
  100. unset($this->_writers["{$writer}"]);
  101. return $this;
  102. }
  103. /**
  104. * Adds a message to the log. Replacement values must be passed in to be
  105. * replaced using [strtr](http://php.net/strtr).
  106. *
  107. * $log->add(Log::ERROR, 'Could not locate user: :user', array(
  108. * ':user' => $username,
  109. * ));
  110. *
  111. * @param string level of message
  112. * @param string message body
  113. * @param array values to replace in the message
  114. * @return Log
  115. */
  116. public function add($level, $message, array $values = NULL)
  117. {
  118. if ($values)
  119. {
  120. // Insert the values into the message
  121. $message = strtr($message, $values);
  122. }
  123. // Create a new message and timestamp it
  124. $this->_messages[] = array
  125. (
  126. 'time' => Date::formatted_time('now', Log::$timestamp, Log::$timezone),
  127. 'level' => $level,
  128. 'body' => $message,
  129. );
  130. if (Log::$write_on_add)
  131. {
  132. // Write logs as they are added
  133. $this->write();
  134. }
  135. return $this;
  136. }
  137. /**
  138. * Write and clear all of the messages.
  139. *
  140. * $log->write();
  141. *
  142. * @return void
  143. */
  144. public function write()
  145. {
  146. if (empty($this->_messages))
  147. {
  148. // There is nothing to write, move along
  149. return;
  150. }
  151. // Import all messages locally
  152. $messages = $this->_messages;
  153. // Reset the messages array
  154. $this->_messages = array();
  155. foreach ($this->_writers as $writer)
  156. {
  157. if (empty($writer['levels']))
  158. {
  159. // Write all of the messages
  160. $writer['object']->write($messages);
  161. }
  162. else
  163. {
  164. // Filtered messages
  165. $filtered = array();
  166. foreach ($messages as $message)
  167. {
  168. if (in_array($message['level'], $writer['levels']))
  169. {
  170. // Writer accepts this kind of message
  171. $filtered[] = $message;
  172. }
  173. }
  174. // Write the filtered messages
  175. $writer['object']->write($filtered);
  176. }
  177. }
  178. }
  179. } // End Kohana_Log