PageRenderTime 44ms CodeModel.GetById 21ms RepoModel.GetById 0ms app.codeStats 0ms

/framework/dev/Log.php

https://bitbucket.org/mwuits/mockup_nachz
PHP | 183 lines | 71 code | 14 blank | 98 comment | 3 complexity | 282bce1cb5d446b8b4d4199999c4c472 MD5 | raw file
Possible License(s): MIT, BSD-3-Clause, GPL-2.0, AGPL-1.0, LGPL-2.1, LGPL-2.0, LGPL-3.0, GPL-3.0
  1. <?php
  2. require_once 'Zend/Log.php';
  3. /**
  4. * Wrapper class for a logging handler like {@link Zend_Log}
  5. * which takes a message (or a map of context variables) and
  6. * sends it to one or more {@link Zend_Log_Writer_Abstract}
  7. * subclasses for output.
  8. *
  9. * These priorities are currently supported:
  10. * - SS_Log::ERR
  11. * - SS_Log::WARN
  12. * - SS_Log::NOTICE
  13. *
  14. * You can add an error writer by calling {@link SS_Log::add_writer()}
  15. *
  16. * Example usage of logging errors by email notification:
  17. * <code>
  18. * SS_Log::add_writer(new SS_LogEmailWriter('my@email.com'), SS_Log::ERR);
  19. * </code>
  20. *
  21. * Example usage of logging errors by file:
  22. * <code>
  23. * SS_Log::add_writer(new SS_LogFileWriter('/var/log/silverstripe/errors.log'), SS_Log::ERR);
  24. * </code>
  25. *
  26. * Example usage of logging at warnings and errors by setting the priority to '<=':
  27. * <code>
  28. * SS_Log::add_writer(new SS_LogEmailWriter('my@email.com'), SS_Log::WARN, '<=');
  29. * </code>
  30. *
  31. * Each writer object can be assigned a formatter. The formatter is
  32. * responsible for formatting the message before giving it to the writer.
  33. * {@link SS_LogErrorEmailFormatter} is such an example that formats errors
  34. * into HTML for human readability in an email client.
  35. *
  36. * Formatters are added to writers like this:
  37. * <code>
  38. * $logEmailWriter = new SS_LogEmailWriter('my@email.com');
  39. * $myEmailFormatter = new MyLogEmailFormatter();
  40. * $logEmailWriter->setFormatter($myEmailFormatter);
  41. * </code>
  42. *
  43. * @package framework
  44. * @subpackage dev
  45. */
  46. class SS_Log {
  47. const ERR = Zend_Log::ERR;
  48. const WARN = Zend_Log::WARN;
  49. const NOTICE = Zend_Log::NOTICE;
  50. /**
  51. * Logger class to use.
  52. * @see SS_Log::get_logger()
  53. * @var string
  54. */
  55. public static $logger_class = 'SS_ZendLog';
  56. /**
  57. * @see SS_Log::get_logger()
  58. * @var object
  59. */
  60. protected static $logger;
  61. /**
  62. * @var array Logs additional context from PHP's superglobals.
  63. * Caution: Depends on logger implementation (mainly targeted at {@link SS_LogEmailWriter}).
  64. * @see http://framework.zend.com/manual/en/zend.log.overview.html#zend.log.overview.understanding-fields
  65. */
  66. static $log_globals = array(
  67. '_SERVER' => array(
  68. 'HTTP_ACCEPT',
  69. 'HTTP_ACCEPT_CHARSET',
  70. 'HTTP_ACCEPT_ENCODING',
  71. 'HTTP_ACCEPT_LANGUAGE',
  72. 'HTTP_REFERRER',
  73. 'HTTP_USER_AGENT',
  74. 'HTTPS',
  75. 'REMOTE_ADDR',
  76. ),
  77. );
  78. /**
  79. * Get the logger currently in use, or create a new one if it doesn't exist.
  80. *
  81. * @return object
  82. */
  83. public static function get_logger() {
  84. if(!self::$logger) {
  85. // Create default logger
  86. self::$logger = new self::$logger_class;
  87. // Add default context (shouldn't change until the actual log event happens)
  88. foreach(self::$log_globals as $globalName => $keys) {
  89. foreach($keys as $key) {
  90. $val = @$GLOBALS[$globalName][$key];
  91. self::$logger->setEventItem(sprintf('$%s[\'%s\']', $globalName, $key), $val);
  92. }
  93. }
  94. }
  95. return self::$logger;
  96. }
  97. /**
  98. * Get all writers in use by the logger.
  99. * @return array Collection of Zend_Log_Writer_Abstract instances
  100. */
  101. public static function get_writers() {
  102. return self::get_logger()->getWriters();
  103. }
  104. /**
  105. * Remove all writers currently in use.
  106. */
  107. public static function clear_writers() {
  108. self::get_logger()->clearWriters();
  109. }
  110. /**
  111. * Remove a writer instance from the logger.
  112. * @param object $writer Zend_Log_Writer_Abstract instance
  113. */
  114. public static function remove_writer($writer) {
  115. self::get_logger()->removeWriter($writer);
  116. }
  117. /**
  118. * Add a writer instance to the logger.
  119. * @param object $writer Zend_Log_Writer_Abstract instance
  120. * @param const $priority Priority. Possible values: SS_Log::ERR, SS_Log::WARN or SS_Log::NOTICE
  121. * @param $comparison Priority comparison operator. Acts on the integer values of the error
  122. * levels, where more serious errors are lower numbers. By default this is "=", which means only
  123. * the given priority will be logged. Set to "<=" if you want to track errors of *at least*
  124. * the given priority.
  125. */
  126. public static function add_writer($writer, $priority = null, $comparison = '=') {
  127. if($priority) $writer->addFilter(new Zend_Log_Filter_Priority($priority, $comparison));
  128. self::get_logger()->addWriter($writer);
  129. }
  130. /**
  131. * Dispatch a message by priority level.
  132. *
  133. * The message parameter can be either a string (a simple error
  134. * message), or an array of variables. The latter is useful for passing
  135. * along a list of debug information for the writer to handle, such as
  136. * error code, error line, error context (backtrace).
  137. *
  138. * @param mixed $message Exception object or array of error context variables
  139. * @param const $priority Priority. Possible values: SS_Log::ERR, SS_Log::WARN or SS_Log::NOTICE
  140. * @param mixed $extras Extra information to log in event
  141. */
  142. public static function log($message, $priority, $extras = null) {
  143. if($message instanceof Exception) {
  144. $message = array(
  145. 'errno' => '',
  146. 'errstr' => $message->getMessage(),
  147. 'errfile' => $message->getFile(),
  148. 'errline' => $message->getLine(),
  149. 'errcontext' => $message->getTrace()
  150. );
  151. } elseif(is_string($message)) {
  152. $trace = SS_Backtrace::filtered_backtrace();
  153. $lastTrace = $trace[0];
  154. $message = array(
  155. 'errno' => '',
  156. 'errstr' => $message,
  157. 'errfile' => @$lastTrace['file'],
  158. 'errline' => @$lastTrace['line'],
  159. 'errcontext' => $trace
  160. );
  161. }
  162. try {
  163. self::get_logger()->log($message, $priority, $extras);
  164. } catch(Exception $e) {
  165. // @todo How do we handle exceptions thrown from Zend_Log?
  166. // For example, an exception is thrown if no writers are added
  167. }
  168. }
  169. }