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

/classes/log.php

http://github.com/fuel/core
PHP | 325 lines | 165 code | 41 blank | 119 comment | 17 complexity | 7900591d8bb81ce0a1054776e8ea994c MD5 | raw file
Possible License(s): BSD-3-Clause
  1. <?php
  2. /**
  3. * Fuel is a fast, lightweight, community driven PHP 5.4+ framework.
  4. *
  5. * @package Fuel
  6. * @version 1.9-dev
  7. * @author Fuel Development Team
  8. * @license MIT License
  9. * @copyright 2010 - 2019 Fuel Development Team
  10. * @link https://fuelphp.com
  11. */
  12. namespace Fuel\Core;
  13. /**
  14. * Log core class facade for the Monolog composer package.
  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. * log file path
  27. */
  28. protected static $path = null;
  29. /**
  30. * log file filename
  31. */
  32. protected static $filename = null;
  33. /**
  34. * create the monolog instance
  35. */
  36. public static function _init()
  37. {
  38. static::$monolog = new \Monolog\Logger('fuelphp');
  39. static::initialize();
  40. }
  41. /**
  42. * return the monolog instance
  43. */
  44. public static function instance()
  45. {
  46. return static::$monolog;
  47. }
  48. /**
  49. * initialize the created the monolog instance
  50. */
  51. public static function initialize()
  52. {
  53. // load the file config
  54. \Config::load('file', true);
  55. // get the required folder permissions
  56. $permission = \Config::get('file.chmod.folders', 0777);
  57. // determine the name and location of the logfile
  58. $path = \Config::get('log_path', APPPATH.'logs'.DS);
  59. // and make sure it exsts
  60. if ( ! is_dir($path) or ! is_writable($path))
  61. {
  62. \Config::set('log_threshold', \Fuel::L_NONE);
  63. throw new \FuelException('Unable to create the log file. The configured log path "'.$path.'" does not exist.');
  64. }
  65. // determine the name of the logfile
  66. $filename = \Config::get('log_file', null);
  67. if (empty($filename))
  68. {
  69. $filename = date('Y').DS.date('m').DS.date('d').'.php';
  70. }
  71. $fullpath = dirname($filename);
  72. // make sure the log directories exist
  73. try
  74. {
  75. // make sure the full path exists
  76. if ( ! is_dir($path.$fullpath))
  77. {
  78. \File::create_dir($path, $fullpath, $permission);
  79. }
  80. // open the file
  81. $handle = fopen($path.$filename, 'a');
  82. }
  83. catch (\Exception $e)
  84. {
  85. \Config::set('log_threshold', \Fuel::L_NONE);
  86. throw new \FuelException('Unable to access the log file. Please check the permissions on '.\Config::get('log_path').'. ('.$e->getMessage().')');
  87. }
  88. static::$path = $path;
  89. static::$filename = $filename;
  90. if ( ! filesize($path.$filename))
  91. {
  92. fwrite($handle, "<?php defined('COREPATH') or exit('No direct script access allowed'); ?>".PHP_EOL.PHP_EOL);
  93. chmod($path.$filename, \Config::get('file.chmod.files', 0666));
  94. }
  95. fclose($handle);
  96. // create the streamhandler, and activate the handler
  97. $stream = new \Monolog\Handler\StreamHandler($path.$filename, \Monolog\Logger::DEBUG);
  98. $formatter = new \Monolog\Formatter\LineFormatter("%level_name% - %datetime% --> %message%".PHP_EOL, \Config::get('log_date_format', 'Y-m-d H:i:s'));
  99. $stream->setFormatter($formatter);
  100. static::$monolog->pushHandler($stream);
  101. }
  102. /**
  103. * Get the current log filename, optionally with a prefix or suffix.
  104. */
  105. public static function logfile($prefix = '', $suffix = '')
  106. {
  107. $ext = pathinfo(static::$filename, PATHINFO_EXTENSION);
  108. $path = dirname(static::$filename);
  109. $file = pathinfo(static::$filename, PATHINFO_FILENAME);
  110. return static::$path.$path.DS.$prefix.$file.$suffix.($ext?('.'.$ext):'');
  111. }
  112. /**
  113. * Logs a message with the Info Log Level
  114. *
  115. * @param string $msg The log message
  116. * @param string $context The message context
  117. * @return bool If it was successfully logged
  118. */
  119. public static function info($msg, $context = null)
  120. {
  121. return static::write(\Fuel::L_INFO, $msg, $context);
  122. }
  123. /**
  124. * Logs a message with the Debug Log Level
  125. *
  126. * @param string $msg The log message
  127. * @param string $context The message context
  128. * @return bool If it was successfully logged
  129. */
  130. public static function debug($msg, $context = null)
  131. {
  132. return static::write(\Fuel::L_DEBUG, $msg, $context);
  133. }
  134. /**
  135. * Logs a message with the Warning Log Level
  136. *
  137. * @param string $msg The log message
  138. * @param string $context The message context
  139. * @return bool If it was successfully logged
  140. */
  141. public static function warning($msg, $context = null)
  142. {
  143. return static::write(\Fuel::L_WARNING, $msg, $context);
  144. }
  145. /**
  146. * Logs a message with the Error Log Level
  147. *
  148. * @param string $msg The log message
  149. * @param string $context The message context
  150. * @return bool If it was successfully logged
  151. */
  152. public static function error($msg, $context = null)
  153. {
  154. return static::write(\Fuel::L_ERROR, $msg, $context);
  155. }
  156. /**
  157. * Write a log entry to Monolog
  158. *
  159. * @param int|string $level the log level
  160. * @param string $msg the log message
  161. * @param array $context message context
  162. * @return bool
  163. * @throws \FuelException
  164. */
  165. public static function log($level, $msg, array $context = array())
  166. {
  167. // bail out if we don't need logging at all
  168. if ( ! static::need_logging($level))
  169. {
  170. return false;
  171. }
  172. // if profiling is active log the message to the profile
  173. if (\Config::get('profiling'))
  174. {
  175. \Console::log($msg);
  176. }
  177. // log the message
  178. static::instance()->log($level, $msg, $context);
  179. return true;
  180. }
  181. /**
  182. * Write Log File
  183. *
  184. * Generally this function will be called using the global log_message() function
  185. *
  186. * @param int|string $level the log level
  187. * @param string $msg the log message
  188. * @param string|array $context message context
  189. * @return bool
  190. * @throws \FuelException
  191. */
  192. public static function write($level, $msg, $context = null)
  193. {
  194. // bail out if we don't need logging at all
  195. if (($level = static::need_logging($level)) === false)
  196. {
  197. return false;
  198. }
  199. // for compatibility with Monolog contexts
  200. if (is_array($context))
  201. {
  202. return static::log($level, $msg, $context);
  203. }
  204. // if profiling is active log the message to the profile
  205. if (\Config::get('profiling'))
  206. {
  207. empty($context) ? \Console::log($msg) : \Console::log($context.' - '.$msg);
  208. }
  209. // log the message
  210. empty($context) ? static::instance()->log($level, $msg) : static::instance()->log($level, $context.' - '.$msg);
  211. return true;
  212. }
  213. /**
  214. * Check if a message with this log level needs logging
  215. *
  216. * @param int|string $level the log level
  217. * @return bool
  218. * @throws \FuelException
  219. */
  220. protected static function need_logging($level)
  221. {
  222. // defined default error labels
  223. static $levels = array(
  224. 100 => 'DEBUG',
  225. 200 => 'INFO',
  226. 250 => 'NOTICE',
  227. 300 => 'WARNING',
  228. 400 => 'ERROR',
  229. 500 => 'CRITICAL',
  230. 550 => 'ALERT',
  231. 600 => 'EMERGENCY',
  232. );
  233. // defined old default error labels
  234. static $oldlabels = array(
  235. 1 => 'Error',
  236. 2 => 'Warning',
  237. 3 => 'Debug',
  238. 4 => 'Info',
  239. );
  240. // get the levels defined to be logged
  241. $loglabels = \Config::get('log_threshold');
  242. // bail out if we don't need logging at all
  243. if ($loglabels == \Fuel::L_NONE)
  244. {
  245. // this entry should not be logged
  246. return false;
  247. }
  248. // if it's not an array, assume it's an "up to" level
  249. if ( ! is_array($loglabels))
  250. {
  251. $a = array();
  252. foreach ($levels as $l => $label)
  253. {
  254. $l >= $loglabels and $a[] = $l;
  255. }
  256. $loglabels = $a;
  257. }
  258. // convert the level to monolog standards if needed
  259. if (is_int($level) and isset($oldlabels[$level]))
  260. {
  261. $level = strtoupper($oldlabels[$level]);
  262. }
  263. if (is_string($level))
  264. {
  265. if ( ! $level = array_search($level, $levels))
  266. {
  267. $level = 250; // can't map it, convert it to a NOTICE
  268. }
  269. }
  270. // make sure $level has the correct value
  271. if ((is_int($level) and ! isset($levels[$level])) or (is_string($level) and ! array_search(strtoupper($level), $levels)))
  272. {
  273. throw new \FuelException('Invalid level "'.$level.'" passed to logger()');
  274. }
  275. // do we need to log the message with this level?
  276. if ( ! in_array($level, $loglabels))
  277. {
  278. // this entry should not be logged
  279. return false;
  280. }
  281. // this entry should be logged
  282. return $level;
  283. }
  284. }