log.php 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  1. <?php
  2. /**
  3. * Fuel is a fast, lightweight, community driven PHP5 framework.
  4. *
  5. * @package Fuel
  6. * @version 1.0
  7. * @author Fuel Development Team
  8. * @license MIT License
  9. * @copyright 2010 - 2013 Fuel Development Team
  10. * @link http://fuelphp.com
  11. */
  12. namespace Log;
  13. /**
  14. * Log core class replacement
  15. *
  16. * This class will provide the interface between the Fuel v1.x class API
  17. * and the Monolog package, in preparation for FuelPHP v2.0
  18. */
  19. class Log
  20. {
  21. /**
  22. * container for the Monolog instance
  23. */
  24. protected static $monolog = null;
  25. /**
  26. * Copy of the Monolog log levels
  27. */
  28. protected static $levels = array(
  29. 100 => 'DEBUG',
  30. 200 => 'INFO',
  31. 250 => 'NOTICE',
  32. 300 => 'WARNING',
  33. 400 => 'ERROR',
  34. 500 => 'CRITICAL',
  35. 550 => 'ALERT',
  36. 600 => 'EMERGENCY',
  37. );
  38. /**
  39. * Initialize the class
  40. */
  41. public static function _init()
  42. {
  43. // load the file config
  44. \Config::load('file', true);
  45. // determine the name and location of the logfile
  46. $filepath = \Config::get('log_path').date('Y/m').'/';
  47. if ( ! is_dir($filepath))
  48. {
  49. @mkdir($filepath, \Config::get('file.chmod.folders', 0777), true);
  50. }
  51. $filename = $filepath.date('d').'.php';
  52. if ( ! $handle = @fopen($filename, 'a'))
  53. {
  54. die('Fatal error: could not create or access the log file ('.$filename.')<br />check your file system permissions!');
  55. }
  56. if ( ! filesize($filename))
  57. {
  58. fwrite($handle, "<?php defined('COREPATH') or exit('No direct script access allowed'); ?>".PHP_EOL.PHP_EOL);
  59. chmod($filename, \Config::get('file.chmod.files', 0666));
  60. }
  61. fclose($handle);
  62. // create the monolog instance
  63. static::$monolog = new \Monolog\Logger('fuelphp');
  64. // create the streamhandler, and activate the handler
  65. $stream = new \Monolog\Handler\StreamHandler($filename, \Monolog\Logger::DEBUG);
  66. $formatter = new \Monolog\Formatter\LineFormatter("%level_name% - %datetime% --> %message%".PHP_EOL, "Y-m-d H:i:s");
  67. $stream->setFormatter($formatter);
  68. static::$monolog->pushHandler($stream);
  69. }
  70. /**
  71. * Return the monolog instance
  72. */
  73. public static function instance()
  74. {
  75. return static::$monolog;
  76. }
  77. /**
  78. * Logs a message with the Info Log Level
  79. *
  80. * @param string $msg The log message
  81. * @param string $method The method that logged
  82. * @return bool If it was successfully logged
  83. */
  84. public static function info($msg, $method = null)
  85. {
  86. return static::write(\Fuel::L_INFO, $msg, $method);
  87. }
  88. /**
  89. * Logs a message with the Debug Log Level
  90. *
  91. * @param string $msg The log message
  92. * @param string $method The method that logged
  93. * @return bool If it was successfully logged
  94. */
  95. public static function debug($msg, $method = null)
  96. {
  97. return static::write(\Fuel::L_DEBUG, $msg, $method);
  98. }
  99. /**
  100. * Logs a message with the Warning Log Level
  101. *
  102. * @param string $msg The log message
  103. * @param string $method The method that logged
  104. * @return bool If it was successfully logged
  105. */
  106. public static function warning($msg, $method = null)
  107. {
  108. return static::write(\Fuel::L_WARNING, $msg, $method);
  109. }
  110. /**
  111. * Logs a message with the Error Log Level
  112. *
  113. * @param string $msg The log message
  114. * @param string $method The method that logged
  115. * @return bool If it was successfully logged
  116. */
  117. public static function error($msg, $method = null)
  118. {
  119. return static::write(\Fuel::L_ERROR, $msg, $method);
  120. }
  121. /**
  122. * Write Log File
  123. *
  124. * Generally this function will be called using the global log_message() function
  125. *
  126. * @access public
  127. * @param int|string the error level
  128. * @param string the error message
  129. * @param string information about the method
  130. * @return bool
  131. */
  132. public static function write($level, $msg, $method = null)
  133. {
  134. // defined default error labels
  135. static $oldlabels = array(
  136. 1 => 'Error',
  137. 2 => 'Warning',
  138. 3 => 'Debug',
  139. 4 => 'Info',
  140. );
  141. // get the levels defined to be logged
  142. $loglabels = \Config::get('log_threshold');
  143. // bail out if we don't need logging at all
  144. if ($loglabels == \Fuel::L_NONE)
  145. {
  146. return false;
  147. }
  148. // if it's not an array, assume it's an "up to" level
  149. if ( ! is_array($loglabels))
  150. {
  151. $a = array();
  152. foreach (static::$levels as $l => $label)
  153. {
  154. $l >= $loglabels and $a[] = $l;
  155. }
  156. $loglabels = $a;
  157. }
  158. // if profiling is active log the message to the profile
  159. if (\Config::get('profiling'))
  160. {
  161. \Console::log($method.' - '.$msg);
  162. }
  163. // convert the level to monolog standards if needed
  164. if (is_int($level) and isset($oldlabels[$level]))
  165. {
  166. $level = strtoupper($oldlabels[$level]);
  167. }
  168. if (is_string($level))
  169. {
  170. if ( ! $level = array_search($level, static::$levels))
  171. {
  172. $level = 250; // can't map it, convert it to a NOTICE
  173. }
  174. }
  175. // make sure $level has the correct value
  176. if ((is_int($level) and ! isset(static::$levels[$level])) or (is_string($level) and ! array_search(strtoupper($level), static::$levels)))
  177. {
  178. throw new \FuelException('Invalid level "'.$level.'" passed to logger()');
  179. }
  180. // do we need to log the message with this level?
  181. if ( ! in_array($level, $loglabels))
  182. {
  183. return false;
  184. }
  185. // log the message
  186. static::$monolog->log($level, (empty($method) ? '' : $method.' - ').$msg);
  187. return true;
  188. }
  189. }