PageRenderTime 48ms CodeModel.GetById 25ms RepoModel.GetById 0ms app.codeStats 0ms

/system/classes/kohana/log.php

https://bitbucket.org/alvinpd/monsterninja
PHP | 194 lines | 88 code | 25 blank | 81 comment | 6 complexity | c275d0c22e991e57f533ca91a7b635c0 MD5 | raw file
  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-2009 Kohana Team
  11. * @license http://kohanaphp.com/license
  12. */
  13. class Kohana_Log {
  14. /**
  15. * @var string timestamp format
  16. */
  17. public static $timestamp = 'Y-m-d H:i:s';
  18. /**
  19. * @var string timezone for dates logged
  20. */
  21. public static $timezone;
  22. // Singleton static instance
  23. private static $_instance;
  24. /**
  25. * Get the singleton instance of this class and enable writing at shutdown.
  26. *
  27. * $log = Kohana_Log::instance();
  28. *
  29. * @return Kohana_Log
  30. */
  31. public static function instance()
  32. {
  33. if (self::$_instance === NULL)
  34. {
  35. // Create a new instance
  36. self::$_instance = new self;
  37. // Write the logs at shutdown
  38. register_shutdown_function(array(self::$_instance, 'write'));
  39. }
  40. return self::$_instance;
  41. }
  42. // List of added messages
  43. private $_messages = array();
  44. // List of log writers
  45. private $_writers = array();
  46. /**
  47. * Attaches a log writer, and optionally limits the types of messages that
  48. * will be written by the writer.
  49. *
  50. * $log->attach($writer);
  51. *
  52. * @param object Kohana_Log_Writer instance
  53. * @param array messages types to write
  54. * @return $this
  55. */
  56. public function attach(Kohana_Log_Writer $writer, array $types = NULL)
  57. {
  58. $this->_writers["{$writer}"] = array
  59. (
  60. 'object' => $writer,
  61. 'types' => $types
  62. );
  63. return $this;
  64. }
  65. /**
  66. * Detaches a log writer. The same writer object must be used.
  67. *
  68. * $log->detach($writer);
  69. *
  70. * @param object Kohana_Log_Writer instance
  71. * @return $this
  72. */
  73. public function detach(Kohana_Log_Writer $writer)
  74. {
  75. // Remove the writer
  76. unset($this->_writers["{$writer}"]);
  77. return $this;
  78. }
  79. /**
  80. * Adds a message to the log. Replacement values must be passed in to be
  81. * replaced using [strtr](http://php.net/strtr).
  82. *
  83. * $log->add('error', 'Could not locate user: :user', array(
  84. * ':user' => $username,
  85. * ));
  86. *
  87. * @param string type of message
  88. * @param string message body
  89. * @param array values to replace in the message
  90. * @return $this
  91. */
  92. public function add($type, $message, array $values = NULL)
  93. {
  94. if (self::$timezone)
  95. {
  96. // Display the time according to the given timezone
  97. $time = new DateTime('now', new DateTimeZone(self::$timezone));
  98. $time = $time->format(self::$timestamp);
  99. }
  100. else
  101. {
  102. // Display the time in the current locale timezone
  103. $time = date(self::$timestamp);
  104. }
  105. if ($values)
  106. {
  107. // Insert the values into the message
  108. $message = strtr($message, $values);
  109. }
  110. // Create a new message and timestamp it
  111. $this->_messages[] = array
  112. (
  113. 'time' => $time,
  114. 'type' => $type,
  115. 'body' => $message,
  116. );
  117. return $this;
  118. }
  119. /**
  120. * Write and clear all of the messages.
  121. *
  122. * $log->write();
  123. *
  124. * @return void
  125. */
  126. public function write()
  127. {
  128. if (empty($this->_messages))
  129. {
  130. // There is nothing to write, move along
  131. return;
  132. }
  133. // Import all messages locally
  134. $messages = $this->_messages;
  135. // Reset the messages array
  136. $this->_messages = array();
  137. foreach ($this->_writers as $writer)
  138. {
  139. if (empty($writer['types']))
  140. {
  141. // Write all of the messages
  142. $writer['object']->write($messages);
  143. }
  144. else
  145. {
  146. // Filtered messages
  147. $filtered = array();
  148. foreach ($messages as $message)
  149. {
  150. if (in_array($message['type'], $writer['types']))
  151. {
  152. // Writer accepts this kind of message
  153. $filtered[] = $message;
  154. }
  155. }
  156. // Write the filtered messages
  157. $writer['object']->write($filtered);
  158. }
  159. }
  160. }
  161. final private function __construct()
  162. {
  163. // Enforce singleton behavior
  164. }
  165. private function __clone()
  166. {
  167. // Enforce singleton behavior
  168. }
  169. } // End Kohana_Log