Logger.php 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * Author: 芸众商城 www.yunzshop.com
  5. * Date: 22/02/2017
  6. * Time: 00:57
  7. */
  8. namespace app\common\helpers;
  9. use Monolog\Handler\HandlerInterface;
  10. use Monolog\Handler\StreamHandler;
  11. use Monolog\Logger as Mlogger;
  12. use Monolog\Processor\WebProcessor;
  13. /**
  14. *
  15. * monolog 日志封装类
  16. *
  17. * monolog是 Laravel,Symfony,Silex 默认集成的日志库, 同时大量其他框架提供了集成扩展.
  18. * 它是最流行的 php log库, 自带超多handler, 长期维护, 稳定更新.
  19. * 它支持以各种方式记录日志: 记录到文件,mail,nosql,mail,irc,firephp,elasticsearch服务器....
  20. *```php
  21. * \app\common\helpers\Logger::debug('这是一条debug日志');
  22. * \app\common\helpers\Logger::info('这是一条info日志');
  23. * \app\common\helpers\Logger::warn('这是一条warn日志');
  24. * \app\common\helpers\Logger::error('这是一条error日志');
  25. *```
  26. * yunshop-monolog 默认注册的StreamHandler的日志级别为 debug.
  27. * 如果你想改变它的级别或者不想使用StreamHandler, 就需要先取出这个handler.
  28. * 假设,我们现在的在生产环境下的日志需求是这样:
  29. * 1. 只想在本地文件中记录Error以上级别的日志供常规检查
  30. * 2. info 以上的日志向发到外部的 MongoDb 数据库中,供日志监控和分析
  31. * 3. 不记录任何debug信息.
  32. *
  33. *```php
  34. * $logger = Logger::getLogger();
  35. * $stream_handler = $logger->popHandler(); // 取出 StreamHandler 对象
  36. * $stream_handler->setLevel(Logger::ERROR); // 重设其日志级别
  37. * $logger->pushHandler($stream_handler); // 注册修改后的StreamHandler 对象
  38. * $mongodb = new MongoDBHandler(new \Mongo("mongodb://***.***.***.***:27017"), "logs", "prod", Logger::INFO);
  39. * $logger->pushHandler($mongodb); // 文件
  40. *```
  41. *
  42. * @method static Mlogger pushHandler(HandlerInterface $handler) Pushes a handler on to the stack.
  43. * @method static Mlogger pushProcessor(callable $callback)
  44. * @method static Mlogger setHandlers(array $handlers) Set handlers, replacing all existing ones. If a map is passed, keys will be ignored.
  45. * @method static HandlerInterface popHandler() Pops a handler from the stack
  46. * @method static HandlerInterface[] getHandlers()
  47. * @method static callable popProcessor()
  48. * @method static callable[] getProcessors()
  49. *
  50. * @method static bool debug(string $message, array $context = array())
  51. * @method static bool info(string $message, array $context = array())
  52. * @method static bool notice(string $message, array $context = array())
  53. * @method static bool warn(string $message, array $context = array())
  54. * @method static bool warning(string $message, array $context = array())
  55. * @method static bool err(string $message, array $context = array())
  56. * @method static bool error(string $message, array $context = array())
  57. * @method static bool crit(string $message, array $context = array())
  58. * @method static bool critical(string $message, array $context = array())
  59. * @method static bool alert(string $message, array $context = array())
  60. * @method static bool emerg(string $message, array $context = array())
  61. * @method static bool emergency(string $message, array $context = array())
  62. *
  63. * @method static bool addRecord(string $level, $message, array $context = array())
  64. * @method static bool addDebug(string $message, array $context = array())
  65. * @method static bool addInfo(string $message, array $context = array())
  66. * @method static bool addNotice(string $message, array $context = array())
  67. * @method static bool addWarning(string $message, array $context = array())
  68. * @method static bool addError(string $message, array $context = array())
  69. * @method static bool addCritical(string $message, array $context = array())
  70. * @method static bool addAlert(string $message, array $context = array())
  71. * @method static bool addEmergency(string $message, array $context = array())
  72. *
  73. */
  74. class Logger
  75. {
  76. const DEBUG = 100;
  77. const INFO = 200;
  78. const NOTICE = 250;
  79. const WARNING = 300;
  80. const ERROR = 400;
  81. const CRITICAL = 500;
  82. const ALERT = 550;
  83. const EMERGENCY = 600;
  84. /** @var Mlogger */
  85. static protected $logger;
  86. static public function init()
  87. {
  88. if (!self::$logger instanceof Mlogger) {
  89. self::$logger = new Mlogger('yunshop');
  90. //@todo 配置日志记录目录
  91. $handler = new StreamHandler(base_path() . '/data/logs/' . date('y_m_d') . '.log', Logger::DEBUG);
  92. $handler->getFormatter()->allowInlineLineBreaks();
  93. $handler->getFormatter()->ignoreEmptyContextAndExtra();
  94. self::$logger->pushProcessor(new WebProcessor());
  95. self::$logger->pushHandler($handler); // 文件
  96. }
  97. }
  98. static public function getLogger()
  99. {
  100. self::init();
  101. return self::$logger;
  102. }
  103. static public function __callStatic($method, $paramters)
  104. {
  105. self::init();
  106. if (method_exists(self::$logger, $method)) {
  107. return call_user_func_array(array(self::$logger, $method), $paramters);
  108. }
  109. if (method_exists('Mlogger', $method)) {
  110. return forward_static_call_array(array('Mlogger', $method), $paramters);
  111. } else {
  112. throw new \RuntimeException('方法不存在');
  113. }
  114. }
  115. }