Guest User

Untitled

a guest
Jan 22nd, 2018
69
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.93 KB | None | 0 0
  1. <?php
  2.  
  3. namespace Lumberjack;
  4.  
  5. //error_reporting(E_ALL|E_STRICT);
  6.  
  7. /*******************************************
  8. * LumberJack
  9. *
  10. * LumberJack serves as a simple logging class with extensibility
  11. * through the use of LoggingAdapters, the default being BasicOutput.
  12. * Dependency injection is used to configure
  13. * which adapter will be utilized by LumberJack.
  14. *
  15. ********************************************/
  16.  
  17. final class LumberJack {
  18. const DEBUG = 5;
  19. const INFO = 4;
  20. const WARNING = 3;
  21. const ERROR = 2;
  22. const FATAL = 1;
  23. const ALL = 0;
  24.  
  25. private $adapter;
  26. private $level = self::WARNING;
  27. private $pattern = '%p [%d{M d, o - H:i:sA}] %m%n';
  28. private $identifier;
  29.  
  30. private static $loggers = NULL;
  31.  
  32. private function __construct($identifier, LoggingAppender $adapter) {
  33. $this->identifier = $identifier;
  34. $this->adapter = $adapter;
  35. }
  36.  
  37. public static function instance($identifier, LoggingAdapter $adapter = NULL) {
  38. if(isset(self::$loggers[$identifier]) == FALSE) {
  39. if(isset($adapter) == FALSE) {
  40. $adapter = new BasicOutput();
  41. }
  42. self::$loggers[$identifier] = new LumberJack($identifier, $adapter);
  43. }
  44. return self::$loggers[$identifier];
  45. }
  46.  
  47. public function info($message) {
  48. $this->log(self::INFO, $message);
  49. }
  50.  
  51. public function debug($message) {
  52. $this->log(self::DEBUG, $message);
  53. }
  54.  
  55. public function warn($message) {
  56. $this->log(self::WARN, $message);
  57. }
  58.  
  59. public function error($message) {
  60. $this->log(self::ERROR, $message);
  61. }
  62.  
  63. public function fatal($message) {
  64. $this->log(self::FATAL, $message);
  65. }
  66.  
  67. private function log($level, $message) {
  68. if($level <= $this->level) {
  69. $msg = PatternParser::parse($this->pattern, $this->identifier, $message, $level);
  70. $this->adapter->log($msg);
  71. }
  72. }
  73.  
  74. public function setLevel($level) {
  75. $this->level = $level;
  76. }
  77.  
  78. public function getLevel() {
  79. return $this->level;
  80. }
  81.  
  82. private function setPattern($pattern) {
  83. $this->pattern = $pattern;
  84. }
  85. }
  86.  
  87. final class PatternParser {
  88.  
  89. private static $trace = NULL;
  90.  
  91. public static function parse($pattern, $identifier, $message, $level) {
  92. $count = preg_match_all('/(?:%(\w)(?:{(.+)})?(?!\w))/', $pattern, $matches, PREG_SET_ORDER);
  93. foreach($matches as $match) {
  94. $replacement = '';
  95. switch($match[1]) {
  96. case 'd':
  97. $replacement = @date($match[2]);
  98. break;
  99. case 'F':
  100. $replacement = self::trace('file');
  101. break;
  102. case 'i':
  103. $replacement = $identifier;
  104. break;
  105. case 'l':
  106. $replacement = self::trace('location');
  107. break;
  108. case 'L':
  109. $replacement = self::trace('line');
  110. break;
  111. case 'm':
  112. $replacement = $message;
  113. break;
  114. case 'M':
  115. $replacement = self::trace('function');
  116. break;
  117. case 'n':
  118. $replacement = (PHP_SAPI === 'cli') ? PHP_EOL : '<br/>';
  119. break;
  120. case 'p':
  121. $replacement = self::convert($level);
  122. break;
  123. }
  124. $pattern = str_replace($match[0], $replacement, $pattern);
  125. }
  126. return $pattern;
  127. }
  128.  
  129. private static function trace($key) {
  130. if(isset(self::$trace)) {
  131. return isset($key) ? $trace[$key] : $trace;
  132. }
  133. $trace = debug_backtrace(DEBUG_BACKTRACE_PROVIDE_OBJECT|DEBUG_BACKTRACE_IGNORE_ARGS);
  134. foreach($trace as $current) {
  135. if(strpos($current['class'], 'LumberJack/') === FALSE) {
  136. $file = $current['file'];
  137. $current['location'] = $file;
  138. $current['file'] = substr(strrchr($file, '/'), 1);
  139. self::$trace = $current;
  140. break;
  141. }
  142. }
  143. }
  144.  
  145. private static function convert($level) {
  146. switch($level) {
  147. case LumberJack::FATAL:
  148. return 'FATAL';
  149. case LumberJack::ERROR:
  150. return 'ERROR';
  151. case LumberJack::WARNING:
  152. return 'WARNING';
  153. case LumberJack::INFO:
  154. return 'INFO';
  155. case LumberJack::DEBUG:
  156. return 'DEBUG';
  157. }
  158. }
  159. }
  160.  
  161.  
  162. /*******************************************
  163. * LoggingAppender
  164. *
  165. * LoggingAppender is an interface which can be implemented
  166. * to create new means of logging messages and errors from
  167. * the LumberJack library
  168. *
  169. * Two implements are contained below: BasicOutput and FileLogging
  170. * + BasicOutput simply prints messages to the screen, hence the name.
  171. * + FileLogging writes all messages to a set
  172. *
  173. ********************************************/
  174. abstract class LoggingAppender {
  175. abstract public function log($message);
  176. }
  177.  
  178. class BasicOutput extends LoggingAppender {
  179. public function log($message) {
  180. echo $message;
  181. }
  182. }
  183.  
  184. class FileLogging extends LoggingAppender {
  185. private $filename;
  186.  
  187. public function __construct($filename = NULL){
  188. $date = date('omd');
  189. if($filename == NULL){
  190. $filename = 'errors-' . $date . '.log';
  191. }
  192. $this->filename = $filename;
  193. }
  194.  
  195. public function log($message) {
  196. $handle = fopen($this->filename, 'a');
  197. if($handle != FALSE) {
  198. fwrite($handle, $message);
  199. fclose($handle);
  200. }
  201. }
  202. }
  203.  
  204. ?>
Add Comment
Please, Sign In to add comment