/lighter/handlers/Logger.php

https://github.com/mbegoc/lighter · PHP · 491 lines · 143 code · 96 blank · 252 comment · 9 complexity · ca2db0e27575735dd13919783a224b51 MD5 · raw file

  1. <?php
  2. namespace lighter\handlers;
  3. use lighter\handlers\Config;
  4. use \Exception;
  5. /**
  6. * This class aims to messages.
  7. * @name Logger
  8. * @package lighter
  9. * @subpackage handlers
  10. * @since 0.1
  11. * @version 0.1
  12. * @author Michel Begoc
  13. * @copyright (c) 2011 Michel Begoc
  14. * @license MIT - see http://www.opensource.org/licenses/mit-license.php
  15. *
  16. */
  17. class Logger {
  18. /**
  19. * Logging levels constants
  20. * @var string
  21. */
  22. const TRACE = 'TraceLogger',
  23. DUMP = 'DumpLogger',
  24. PROFILE = 'ProfileLogger',
  25. DEBUG = 'DebugLogger',
  26. INFO = 'InfoLogger',
  27. WARNING = 'WarningLogger',
  28. ERROR = 'ErrorLogger',
  29. FATAL = 'FatalLogger',
  30. NONE = 'Logger';
  31. /**
  32. * the instances list of the debugger
  33. * @staticvar array
  34. */
  35. private static $instances = array();
  36. /**
  37. * the configuration object
  38. * @var lighter\handlers\Config
  39. */
  40. private $config;
  41. /**
  42. * the name of the debug section
  43. * @var string
  44. */
  45. private $section;
  46. /**
  47. * a time field for profiling purpose
  48. * @var array
  49. */
  50. private $time = array();
  51. /**
  52. * the file handler in which the messages are written
  53. *
  54. * @var resource
  55. */
  56. private $file = null;
  57. /**
  58. * a private constructor for the singleton
  59. * @param string $section
  60. */
  61. public function __construct($section) {
  62. $this->section = $section;
  63. $this->config = Config::getInstance();
  64. }
  65. /**
  66. * this method is a convenient method to handle the instances pool of loggers.
  67. *
  68. * @static
  69. * @param string $section
  70. * @param string $level - provided by class constants
  71. * @return lighter\handlers\Logger
  72. */
  73. public static function getInstance($section = "default", $level = null) {
  74. if (!isset(self::$instances[$section])) {
  75. if ($level == null) {
  76. $levels = Config::getInstance()->getValue('log', 'levels', array());
  77. if (isset($levels[$section])) {
  78. $level = $levels[$section];
  79. } else {
  80. $level = Config::getInstance()->getValue('log', 'level', self::WARNING);
  81. }
  82. }
  83. $class = __NAMESPACE__.'\\'.$level;
  84. self::$instances[$section] = new $class($section);
  85. }
  86. return self::$instances[$section];
  87. }
  88. /**
  89. * close de file handler
  90. */
  91. public function __destruct() {
  92. fclose($this->file);
  93. }
  94. /**
  95. * add a message with a time stamp
  96. *
  97. * @param string $level
  98. * @param string $content
  99. */
  100. protected function addTimedMessage($level, $content) {
  101. $this->addMessage('<'.date('Y-m-d h:m:s').'> '.$level.' : '.$content);
  102. }
  103. /**
  104. * add a simple message
  105. *
  106. * @param string $content
  107. */
  108. protected function addMessage($content) {
  109. if ($this->file === null) {
  110. if (!file_exists($this->config->getValue('log', 'path', './'))) {
  111. mkdir($this->config->getValue('log', 'path', './'), 0755, true);
  112. }
  113. $path = $this->config->getValue('log', 'path', './').$this->section.'.log';
  114. $this->file = fopen($path, 'a');
  115. }
  116. fwrite($this->file, $content."\n");
  117. }
  118. /**
  119. * log a trace
  120. */
  121. public function trace() {}
  122. /**
  123. * log a dump of a variable
  124. *
  125. * @param mixed $variable
  126. */
  127. public function dump($variable) {}
  128. /**
  129. * log a debug message
  130. *
  131. * @param string $message
  132. */
  133. public function debug($message) {}
  134. /**
  135. * start a profiling operation
  136. *
  137. * @param string $label
  138. */
  139. public function startProfiling($label) {}
  140. /**
  141. * end a profiling operation started with startProfiling.
  142. * If the profiling operation has not been started, this method will throw a
  143. * NonexistentProfilingOperation Exception.
  144. *
  145. * @param string $label
  146. * @throw lighter\handlers\NonexistentProfilingOperation
  147. */
  148. public function endProfiling($label) {}
  149. /**
  150. * log an informationnal message
  151. *
  152. * @param string $message
  153. */
  154. public function info($message) {}
  155. /**
  156. * log a warning message
  157. *
  158. * @param string $message
  159. */
  160. public function warning($message) {}
  161. /**
  162. * log an error message
  163. *
  164. * @param string $message
  165. */
  166. public function error($message) {}
  167. /**
  168. * log a fatal error message
  169. *
  170. * @param unknown_type $message
  171. */
  172. public function fatal($message) {}
  173. }
  174. /**
  175. * implements the fatal level
  176. *
  177. * @name FatalLogger
  178. * @package lighter
  179. * @subpackage handlers
  180. * @since 0.1
  181. * @version 0.1
  182. * @author Michel Begoc
  183. * @copyright (c) 2011 Michel Begoc
  184. * @license MIT - see http://www.opensource.org/licenses/mit-license.php
  185. *
  186. */
  187. class FatalLogger extends Logger {
  188. public function __construct($section) {
  189. parent::__construct($section);
  190. }
  191. /**
  192. * @see lighter\handlers.Logger::fatal()
  193. */
  194. public function fatal($message) {
  195. $this->addTimedMessage('Fatal error', $message);
  196. }
  197. }
  198. /**
  199. * implements the error level
  200. *
  201. * @name ErrorLogger
  202. * @package lighter
  203. * @subpackage handlers
  204. * @since 0.1
  205. * @version 0.1
  206. * @author Michel Begoc
  207. * @copyright (c) 2011 Michel Begoc
  208. * @license MIT - see http://www.opensource.org/licenses/mit-license.php
  209. *
  210. */
  211. class ErrorLogger extends FatalLogger {
  212. public function __construct($section) {
  213. parent::__construct($section);
  214. }
  215. /**
  216. * @see lighter\handlers.Logger::error()
  217. */
  218. public function error($message) {
  219. $this->addTimedMessage('Error', $message);
  220. }
  221. }
  222. /**
  223. * implements the warning level
  224. *
  225. * @name WarningLogger
  226. * @package lighter
  227. * @subpackage handlers
  228. * @since 0.1
  229. * @version 0.1
  230. * @author Michel Begoc
  231. * @copyright (c) 2011 Michel Begoc
  232. * @license MIT - see http://www.opensource.org/licenses/mit-license.php
  233. *
  234. */
  235. class WarningLogger extends ErrorLogger {
  236. public function __construct($section) {
  237. parent::__construct($section);
  238. }
  239. /**
  240. * @see lighter\handlers.Logger::warn()
  241. */
  242. public function warning($message) {
  243. $this->addTimedMessage('Warning', $message);
  244. }
  245. }
  246. /**
  247. * implements the info level
  248. *
  249. * @name InfoLogger
  250. * @package lighter
  251. * @subpackage handlers
  252. * @since 0.1
  253. * @version 0.1
  254. * @author Michel Begoc
  255. * @copyright (c) 2011 Michel Begoc
  256. * @license MIT - see http://www.opensource.org/licenses/mit-license.php
  257. *
  258. */
  259. class InfoLogger extends WarningLogger {
  260. public function __construct($section) {
  261. parent::__construct($section);
  262. }
  263. /**
  264. * @see lighter\handlers.Logger::info()
  265. */
  266. public function info($message) {
  267. $this->addTimedMessage('Info', $message);
  268. }
  269. }
  270. /**
  271. * implements the profiling level
  272. *
  273. * @name ProfileLogger
  274. * @package lighter
  275. * @subpackage handlers
  276. * @since 0.1
  277. * @version 0.1
  278. * @author Michel Begoc
  279. * @copyright (c) 2011 Michel Begoc
  280. * @license MIT - see http://www.opensource.org/licenses/mit-license.php
  281. *
  282. */
  283. class ProfileLogger extends WarningLogger {
  284. public function __construct($section) {
  285. parent::__construct($section);
  286. }
  287. /**
  288. * @see lighter\handlers.Logger::startProfiling()
  289. */
  290. public function startProfiling($label) {
  291. $this->time[$label] = microtime(true);
  292. }
  293. /**
  294. * @see lighter\handlers.Logger::endProfiling()
  295. */
  296. public function endProfiling($label) {
  297. $endTime = microtime(true);
  298. if (isset($this->time[$label])) {
  299. $time = $endTime - $this->time[$label];
  300. $this->addTimedMessage('Profiling', $label.': '.$time.' s');
  301. } else {
  302. throw new UnexpectedValueException('This profiling operation doesn\'t exist.');
  303. }
  304. }
  305. }
  306. /**
  307. * implements the debug level
  308. *
  309. * @name DebugLogger
  310. * @package lighter
  311. * @subpackage handlers
  312. * @since 0.1
  313. * @version 0.1
  314. * @author Michel Begoc
  315. * @copyright (c) 2011 Michel Begoc
  316. * @license MIT - see http://www.opensource.org/licenses/mit-license.php
  317. *
  318. */
  319. class DebugLogger extends ProfileLogger {
  320. public function __construct($section) {
  321. parent::__construct($section);
  322. }
  323. /**
  324. * @see lighter\handlers.Logger::debug()
  325. */
  326. public function debug($message) {
  327. $this->addTimedMessage('Debug', $message);
  328. }
  329. }
  330. /**
  331. * implements the dump level
  332. *
  333. * @name DumpLogger
  334. * @package lighter
  335. * @subpackage handlers
  336. * @since 0.1
  337. * @version 0.1
  338. * @author Michel Begoc
  339. * @copyright (c) 2011 Michel Begoc
  340. * @license MIT - see http://www.opensource.org/licenses/mit-license.php
  341. *
  342. */
  343. class DumpLogger extends DebugLogger {
  344. public function __construct($section) {
  345. parent::__construct($section);
  346. }
  347. /**
  348. * @see lighter\handlers.Logger::dump()
  349. */
  350. public function dump($variable) {
  351. $this->addTimedMessage('Dump', '');
  352. $this->addMessage(print_r($variable, true));
  353. }
  354. }
  355. /**
  356. * implements the trace level
  357. *
  358. * @name TraceLogger
  359. * @package lighter
  360. * @subpackage handlers
  361. * @since 0.1
  362. * @version 0.1
  363. * @author Michel Begoc
  364. * @copyright (c) 2011 Michel Begoc
  365. * @license MIT - see http://www.opensource.org/licenses/mit-license.php
  366. *
  367. */
  368. class TraceLogger extends DumpLogger {
  369. public function __construct($section) {
  370. parent::__construct($section);
  371. }
  372. public function trace($title = null) {
  373. $backtrace = debug_backtrace();
  374. $this->addTimedMessage('Trace', '');
  375. foreach ($backtrace as $trace) {
  376. $this->addMessage($trace["class"].$trace["type"].$trace["function"]);
  377. }
  378. }
  379. }
  380. /**
  381. * The exception lauched by the profile level.
  382. * Thrown when a nonexistent profiling operation is terminated.
  383. *
  384. * @name NonexistentProfilingOperation
  385. * @package lighter
  386. * @subpackage handlers
  387. * @since 0.1
  388. * @version 0.1
  389. * @author Michel Begoc
  390. * @copyright (c) 2011 Michel Begoc
  391. * @license MIT - see http://www.opensource.org/licenses/mit-license.php
  392. *
  393. */
  394. class NonexistentProfilingOperation extends Exception {}