Log.php 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. <?php
  2. /**
  3. * 日志
  4. *
  5. * @author mybsdc <[email protected]>
  6. * @date 2019/3/3
  7. * @time 12:01
  8. */
  9. namespace Luolongfei\Libs;
  10. use Monolog\Logger;
  11. use Monolog\Handler\StreamHandler;
  12. use Bramus\Monolog\Formatter\ColoredLineFormatter;
  13. class Log extends Base
  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. // 云函数只能在 /tmp 目录下写文件
  31. $handler = new StreamHandler(
  32. config('debug') || IS_SCF ? 'php://stdout' : sprintf('%s/logs/%s.log', ROOT_PATH, date('Y-m/d')),
  33. config('debug') ? Logger::DEBUG : Logger::INFO
  34. );
  35. if (config('debug')) {
  36. $handler->setFormatter(new ColoredLineFormatter(null, "[%datetime%] %channel%.%level_name%: %message%\n"));
  37. }
  38. $logger = new Logger('pusher');
  39. $logger->pushHandler($handler);
  40. self::$loggerInstance = $logger;
  41. }
  42. return self::$loggerInstance;
  43. }
  44. /**
  45. * @param $message
  46. * @param array $context
  47. *
  48. * @return bool
  49. * @throws \Exception
  50. */
  51. public static function debug($message, array $context = [])
  52. {
  53. return self::logger()->addDebug($message, $context);
  54. }
  55. /**
  56. * @param $message
  57. * @param array $context
  58. *
  59. * @return bool
  60. * @throws \Exception
  61. */
  62. public static function info($message, array $context = [])
  63. {
  64. return self::logger()->addInfo($message, $context);
  65. }
  66. /**
  67. * @param $message
  68. * @param array $context
  69. *
  70. * @return bool
  71. * @throws \Exception
  72. */
  73. public static function notice($message, array $context = [])
  74. {
  75. return self::logger()->addNotice($message, $context);
  76. }
  77. /**
  78. * @param $message
  79. * @param array $context
  80. *
  81. * @return bool
  82. * @throws \Exception
  83. */
  84. public static function warning($message, array $context = [])
  85. {
  86. return self::logger()->addWarning($message, $context);
  87. }
  88. /**
  89. * @param $message
  90. * @param array $context
  91. *
  92. * @return bool
  93. * @throws \Exception
  94. */
  95. public static function error($message, array $context = [])
  96. {
  97. return self::logger()->addError($message, $context);
  98. }
  99. /**
  100. * @param $message
  101. * @param array $context
  102. *
  103. * @return bool
  104. * @throws \Exception
  105. */
  106. public static function alert($message, array $context = [])
  107. {
  108. return self::logger()->addAlert($message, $context);
  109. }
  110. /**
  111. * @param $message
  112. * @param array $context
  113. *
  114. * @return bool
  115. * @throws \Exception
  116. */
  117. public static function emergency($message, array $context = [])
  118. {
  119. return self::logger()->addEmergency($message, $context);
  120. }
  121. }