logger.php 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. <?php
  2. /**
  3. * Logging functions.
  4. *
  5. * @author Taddeus Kroes
  6. * @version 1.0
  7. * @date 13-07-2012
  8. */
  9. namespace Minimalistic;
  10. /**
  11. * Logger class.
  12. *
  13. * A Logger object provides five functions to process log messages.
  14. *
  15. * @package Minimalistic
  16. */
  17. class Logger {
  18. const CRITICAL = 0;
  19. const ERROR = 1;
  20. const WARNING = 2;
  21. const INFO = 3;
  22. const DEBUG = 4;
  23. static $level_names = array('CRITICAL', 'ERROR', 'WARNING', 'INFO', 'DEBUG');
  24. const DEFAULT_FORMAT = '%(datetime): %(level): %(message)';
  25. private $properties = array();
  26. private $output = array();
  27. private $format = self::DEFAULT_FORMAT;
  28. private $level = self::WARNING;
  29. function set_format($format) {
  30. $this->format = (string)$format;
  31. }
  32. function get_format() {
  33. return $this->format;
  34. }
  35. function get_level() {
  36. return $this->level;
  37. }
  38. function get_level_name() {
  39. return self::$level_names[$this->level];
  40. }
  41. function set_level($level) {
  42. if( is_string($level) ) {
  43. $level = strtoupper($level);
  44. if( !defined('self::'.$level) )
  45. throw new \InvalidArgumentException(sprintf('Invalid debug level %s.', $level));
  46. $level = constant('self::'.$level);
  47. }
  48. if( $level < self::CRITICAL || $level > self::DEBUG )
  49. throw new \InvalidArgumentException(sprintf('Invalid debug level %d.', $level));
  50. $this->level = $level;
  51. }
  52. function set_property($name, $value) {
  53. $this->properties[$name] = (string)$value;
  54. }
  55. function critical($message) {
  56. $this->process($message, self::CRITICAL);
  57. }
  58. function error($message) {
  59. $this->process($message, self::ERROR);
  60. }
  61. function warning($message) {
  62. $this->process($message, self::WARNING);
  63. }
  64. function info($message) {
  65. $this->process($message, self::INFO);
  66. }
  67. function debug($message) {
  68. $this->process($message, self::DEBUG);
  69. }
  70. private function process($message, $level) {
  71. if( $level <= $this->level )
  72. $this->output[] = array($message, $level);
  73. }
  74. function dumps() {
  75. $logger = $this;
  76. $output = '';
  77. foreach( $this->output as $i => $tuple ) {
  78. list($message, $level) = $tuple;
  79. $i && $output .= "\n";
  80. $output .= preg_replace_callback(
  81. '/%\(([a-z-_ ]*)\)/i',
  82. function ($matches) use ($logger, $message, $level) {
  83. $name = $matches[1];
  84. if( $name == 'message' )
  85. return $message;
  86. if( $name == 'level' )
  87. return Logger::$level_names[$level];
  88. return $logger->get_formatted_property($matches[1]);
  89. },
  90. $this->format
  91. );
  92. }
  93. return $output;
  94. }
  95. function dump() {
  96. echo $this->dumps();
  97. }
  98. function clear() {
  99. $this->output = array();
  100. }
  101. function save($path) {
  102. file_put_contents($path, $this->dumps());
  103. }
  104. function handle_exception(\Exception $e) {
  105. if( $e === null )
  106. return;
  107. $message = sprintf("Uncaught %s in file %s, line %d: %s\n\n%s", get_class($e),
  108. $e->getFile(), $e->getLine(), $e->getMessage(), $e->getTraceAsString());
  109. $this->critical($message);
  110. $this->dump();
  111. }
  112. function set_as_exception_handler() {
  113. set_exception_handler(array($this, 'handle_exception'));
  114. }
  115. function get_formatted_property($property) {
  116. if( isset($this->properties[$property]) )
  117. return $this->properties[$property];
  118. switch( $property ) {
  119. case 'loglevel':
  120. return $this->get_level_name();
  121. case 'date':
  122. return strftime('%d-%m-%Y');
  123. case 'time':
  124. return strftime('%H:%M:%S');
  125. case 'datetime':
  126. return strftime('%d-%m-%Y %H:%M:%S');
  127. }
  128. throw new \InvalidArgumentException(sprintf('Invalid logging property "%s".', $property));
  129. }
  130. }
  131. ?>