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

/fuel/packages/log/classes/log.php

https://bitbucket.org/codeyash/bootstrap
PHP | 212 lines | 105 code | 27 blank | 80 comment | 11 complexity | 4fa15846efb2bd776dee88642ab570ea MD5 | raw file
Possible License(s): MIT, Apache-2.0
  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. $old = umask(0);
  50. mkdir($filepath, \Config::get('file.chmod.folders', 0777), true);
  51. umask($old);
  52. }
  53. $filename = $filepath.date('d').'.php';
  54. if ( ! file_exists($filename))
  55. {
  56. file_put_contents($filename, "<"."?php defined('COREPATH') or exit('No direct script access allowed'); ?".">".PHP_EOL.PHP_EOL);
  57. }
  58. // create the monolog instance
  59. static::$monolog = new \Monolog\Logger('fuelphp');
  60. // create the streamhandler, and activate the handler
  61. $stream = new \Monolog\Handler\StreamHandler($filename, \Monolog\Logger::DEBUG);
  62. $formatter = new \Monolog\Formatter\LineFormatter("%level_name% - %datetime% --> %message%".PHP_EOL, "Y-m-d H:i:s");
  63. $stream->setFormatter($formatter);
  64. static::$monolog->pushHandler($stream);
  65. }
  66. /**
  67. * Return the monolog instance
  68. */
  69. public static function instance()
  70. {
  71. return static::$monolog;
  72. }
  73. /**
  74. * Logs a message with the Info Log Level
  75. *
  76. * @param string $msg The log message
  77. * @param string $method The method that logged
  78. * @return bool If it was successfully logged
  79. */
  80. public static function info($msg, $method = null)
  81. {
  82. return static::write(\Fuel::L_INFO, $msg, $method);
  83. }
  84. /**
  85. * Logs a message with the Debug Log Level
  86. *
  87. * @param string $msg The log message
  88. * @param string $method The method that logged
  89. * @return bool If it was successfully logged
  90. */
  91. public static function debug($msg, $method = null)
  92. {
  93. return static::write(\Fuel::L_DEBUG, $msg, $method);
  94. }
  95. /**
  96. * Logs a message with the Warning Log Level
  97. *
  98. * @param string $msg The log message
  99. * @param string $method The method that logged
  100. * @return bool If it was successfully logged
  101. */
  102. public static function warning($msg, $method = null)
  103. {
  104. return static::write(\Fuel::L_WARNING, $msg, $method);
  105. }
  106. /**
  107. * Logs a message with the Error Log Level
  108. *
  109. * @param string $msg The log message
  110. * @param string $method The method that logged
  111. * @return bool If it was successfully logged
  112. */
  113. public static function error($msg, $method = null)
  114. {
  115. return static::write(\Fuel::L_ERROR, $msg, $method);
  116. }
  117. /**
  118. * Write Log File
  119. *
  120. * Generally this function will be called using the global log_message() function
  121. *
  122. * @access public
  123. * @param int|string the error level
  124. * @param string the error message
  125. * @param string information about the method
  126. * @return bool
  127. */
  128. public static function write($level, $msg, $method = null)
  129. {
  130. // defined default error labels
  131. static $oldlabels = array(
  132. 1 => 'Error',
  133. 2 => 'Warning',
  134. 3 => 'Debug',
  135. 4 => 'Info',
  136. );
  137. // get the levels defined to be logged
  138. $loglabels = \Config::get('log_threshold');
  139. // bail out if we don't need logging at all
  140. if ($loglabels == \Fuel::L_NONE)
  141. {
  142. return false;
  143. }
  144. // if it's not an array, assume it's an "up to" level
  145. if ( ! is_array($loglabels))
  146. {
  147. $a = array();
  148. foreach (static::$levels as $l => $label)
  149. {
  150. $l >= $loglabels and $a[] = $l;
  151. }
  152. $loglabels = $a;
  153. }
  154. // if profiling is active log the message to the profile
  155. if (\Config::get('profiling'))
  156. {
  157. \Console::log($method.' - '.$msg);
  158. }
  159. // convert the level to monolog standards if needed
  160. if (is_int($level) and isset($oldlabels[$level]))
  161. {
  162. $level = strtoupper($oldlabels[$level]);
  163. }
  164. if (is_string($level))
  165. {
  166. if ( ! $level = array_search($level, static::$levels))
  167. {
  168. $level = 250; // can't map it, convert it to a NOTICE
  169. }
  170. }
  171. // make sure $level has the correct value
  172. if ((is_int($level) and ! isset(static::$levels[$level])) or (is_string($level) and ! array_search(strtoupper($level), static::$levels)))
  173. {
  174. throw new \FuelException('Invalid level "'.$level.'" passed to logger()');
  175. }
  176. // do we need to log the message with this level?
  177. if ( ! in_array($level, $loglabels))
  178. {
  179. return false;
  180. }
  181. // log the message
  182. static::$monolog->log($level, (empty($method) ? '' : $method.' - ').$msg);
  183. return true;
  184. }
  185. }