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

/_app/core/log.php

https://bitbucket.org/sirestudios/fortis-wellness
PHP | 212 lines | 100 code | 34 blank | 78 comment | 5 complexity | 2e280166d972b4e11fe85557c86fb178 MD5 | raw file
Possible License(s): JSON
  1. <?php
  2. /**
  3. * Statamic Log
  4. * API for logging messages to the site's error log
  5. *
  6. * @author Fred LeBlanc
  7. */
  8. class Log
  9. {
  10. const DEBUG = \Slim\Log::DEBUG;
  11. const INFO = \Slim\Log::INFO;
  12. const WARN = \Slim\Log::WARN;
  13. const ERROR = \Slim\Log::ERROR;
  14. const FATAL = \Slim\Log::FATAL;
  15. protected static $types = array(
  16. \Slim\Log::DEBUG => 'DEBUG',
  17. \Slim\Log::INFO => 'INFO',
  18. \Slim\Log::WARN => 'WARN',
  19. \Slim\Log::ERROR => 'ERROR',
  20. \Slim\Log::FATAL => 'FATAL'
  21. );
  22. /**
  23. * debug
  24. * Log a debug message
  25. *
  26. * @param string $message Message to log
  27. * @param string $aspect Aspect for context
  28. * @param string $instance Instance for context
  29. */
  30. public static function debug($message, $aspect, $instance=NULL) {
  31. self::write($message, \Slim\Log::DEBUG, $aspect, $instance);
  32. }
  33. /**
  34. * info
  35. * Log an info message
  36. *
  37. * @param string $message Message to log
  38. * @param string $aspect Aspect for context
  39. * @param string $instance Instance for context
  40. */
  41. public static function info($message, $aspect, $instance=NULL) {
  42. self::write($message, \Slim\Log::INFO, $aspect, $instance);
  43. }
  44. /**
  45. * warn
  46. * Log a warn message
  47. *
  48. * @param string $message Message to log
  49. * @param string $aspect Aspect for context
  50. * @param string $instance Instance for context
  51. */
  52. public static function warn($message, $aspect, $instance=NULL) {
  53. self::write($message, \Slim\Log::WARN, $aspect, $instance);
  54. }
  55. /**
  56. * error
  57. * Log an error message
  58. *
  59. * @param string $message Message to log
  60. * @param string $aspect Aspect for context
  61. * @param string $instance Instance for context
  62. */
  63. public static function error($message, $aspect, $instance=NULL) {
  64. self::write($message, \Slim\Log::ERROR, $aspect, $instance);
  65. }
  66. /**
  67. * fatal
  68. * Log a fatal message
  69. *
  70. * @param string $message Message to log
  71. * @param string $aspect Aspect for context
  72. * @param string $instance Instance for context
  73. */
  74. public static function fatal($message, $aspect, $instance=NULL) {
  75. self::write($message, \Slim\Log::FATAL, $aspect, $instance);
  76. }
  77. /**
  78. * convert_log_level
  79. * Converts a given $log_level string to its corresponding integer value
  80. *
  81. * @param $log_level string Log level to convert
  82. * @return int
  83. */
  84. public static function convert_log_level($log_level) {
  85. switch(strtolower($log_level)) {
  86. case 'debug':
  87. return \Slim\Log::DEBUG;
  88. break;
  89. case 'info':
  90. return \Slim\Log::INFO;
  91. break;
  92. case 'warn':
  93. return \Slim\Log::WARN;
  94. break;
  95. case 'error':
  96. return \Slim\Log::ERROR;
  97. break;
  98. default:
  99. return \Slim\Log::FATAL;
  100. break;
  101. }
  102. }
  103. /**
  104. * log_slim_exception
  105. * Parses and logs slim exceptions for Statamic's logger
  106. *
  107. * @param object $exception Slim Exception
  108. * @return void
  109. */
  110. public static function log_slim_exception($exception) {
  111. $filename = $exception->getFile();
  112. $message = ($exception->getLine() && !preg_match("/line [\d]+/i", $exception->getMessage())) ? $exception->getMessage() . " on line " . $exception->getLine() . "." : $exception->getMessage();
  113. $aspect = 'unknown';
  114. $instance = 'unknown';
  115. if (strstr($filename, "/") !== FALSE) {
  116. preg_match("#/vendor/([\w\d\-_]+)/#i", $filename, $matches);
  117. $path_info = pathinfo($filename);
  118. $instance = $path_info['filename'];
  119. $aspect = (isset($matches[1])) ? $matches[1] : "core";
  120. }
  121. // log this as a fatal message
  122. self::fatal($message, $aspect, $instance);
  123. }
  124. /**
  125. * is_logging_enabled
  126. * Is logging currently enabled for this site?
  127. *
  128. * @return boolean
  129. */
  130. protected static function is_logging_enabled() {
  131. $app = \Slim\Slim::getInstance();
  132. return (isset($app->config['_log_enabled']) && $app->config['_log_enabled']);
  133. }
  134. /**
  135. * log
  136. * If logging is enabled for this site, logs message to log
  137. *
  138. * @param string $message Message to log
  139. * @param int $level Level of error to log
  140. * @param string $context Context of message
  141. * @param string $instance Specific instance
  142. * @return void
  143. */
  144. protected static function write($message, $level, $context, $instance=NULL) {
  145. // check that logging is enabled
  146. if (!self::is_logging_enabled()) {
  147. return;
  148. }
  149. $app = \Slim\Slim::getInstance();
  150. $log = $app->getLog();
  151. $entry = self::$types[$level] . "|";
  152. $entry .= Date::format("r") . "|";
  153. $entry .= $context . "|";
  154. $entry .= $instance . "|";
  155. $entry .= $_SERVER["REQUEST_URI"] . "|";
  156. $entry .= $_SERVER["REMOTE_ADDR"] . "|";
  157. $entry .= (isset($app->config['environment']) && $app->config['environment']) ? $app->config['environment'] . "|" : "|";
  158. $entry .= $message;
  159. switch($level) {
  160. case \Slim\Log::DEBUG:
  161. return $log->debug($entry);
  162. break;
  163. case \Slim\Log::INFO:
  164. return $log->info($entry);
  165. break;
  166. case \Slim\Log::WARN:
  167. return $log->warn($entry);
  168. break;
  169. case \Slim\Log::ERROR:
  170. return $log->error($entry);
  171. break;
  172. default:
  173. return $log->fatal($entry);
  174. break;
  175. }
  176. }
  177. }
  178. ?>