Log.php 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. <?php
  2. /**
  3. * 日志
  4. *
  5. * @author mybsdc <[email protected]>
  6. * @date 2019/3/3
  7. * @time 12:01
  8. */
  9. namespace Luolongfei\Lib;
  10. use Monolog\Logger;
  11. use Monolog\Handler\StreamHandler;
  12. use Bramus\Monolog\Formatter\ColoredLineFormatter;
  13. class Log
  14. {
  15. /**
  16. * @var Logger
  17. */
  18. protected static $loggerInstance;
  19. /**
  20. * 由于php不能在类外使用已实例化的对象来访问静态属性,但可以在类外访问类里的静态方法,故定义此方法实现类外访问静态属性
  21. *
  22. * 注意,info等方法不写日志,error方法才写日志到指定目录
  23. *
  24. * @return Logger
  25. * @throws \Exception
  26. */
  27. public static function logger()
  28. {
  29. if (!self::$loggerInstance instanceof Logger) {
  30. $handler = new StreamHandler(
  31. config('debug') ? 'php://stdout' : sprintf('%s/logs/%s.log', ROOT_PATH, date('Y-m/d')),
  32. config('debug') ? Logger::DEBUG : Logger::INFO
  33. );
  34. if (config('debug')) {
  35. $handler->setFormatter(new ColoredLineFormatter(null, "[%datetime%] %channel%.%level_name%: %message%\n"));
  36. }
  37. $logger = new Logger('pusher');
  38. $logger->pushHandler($handler);
  39. self::$loggerInstance = $logger;
  40. }
  41. return self::$loggerInstance;
  42. }
  43. /**
  44. * @param $message
  45. * @param array $context
  46. *
  47. * @return bool
  48. * @throws \Exception
  49. */
  50. public static function debug($message, array $context = [])
  51. {
  52. return self::logger()->addDebug($message, $context);
  53. }
  54. /**
  55. * @param $message
  56. * @param array $context
  57. *
  58. * @return bool
  59. * @throws \Exception
  60. */
  61. public static function info($message, array $context = [])
  62. {
  63. return self::logger()->addInfo($message, $context);
  64. }
  65. /**
  66. * @param $message
  67. * @param array $context
  68. *
  69. * @return bool
  70. * @throws \Exception
  71. */
  72. public static function notice($message, array $context = [])
  73. {
  74. return self::logger()->addNotice($message, $context);
  75. }
  76. /**
  77. * @param $message
  78. * @param array $context
  79. *
  80. * @return bool
  81. * @throws \Exception
  82. */
  83. public static function warning($message, array $context = [])
  84. {
  85. return self::logger()->addWarning($message, $context);
  86. }
  87. /**
  88. * @param $message
  89. * @param array $context
  90. *
  91. * @return bool
  92. * @throws \Exception
  93. */
  94. public static function error($message, array $context = [])
  95. {
  96. return self::logger()->addError($message, $context);
  97. }
  98. /**
  99. * @param $message
  100. * @param array $context
  101. *
  102. * @return bool
  103. * @throws \Exception
  104. */
  105. public static function alert($message, array $context = [])
  106. {
  107. return self::logger()->addAlert($message, $context);
  108. }
  109. /**
  110. * @param $message
  111. * @param array $context
  112. *
  113. * @return bool
  114. * @throws \Exception
  115. */
  116. public static function emergency($message, array $context = [])
  117. {
  118. return self::logger()->addEmergency($message, $context);
  119. }
  120. }