Log.php 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  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. // 检查日志目录权限
  31. $logDir = sprintf('%s/logs/', ROOT_PATH);
  32. if (!is_writable($logDir)) {
  33. system_log(sprintf('由于没有日志目录 %s 的写权限,无法生成日志文件,只能直接输出', $logDir));
  34. $stream = 'php://stdout';
  35. } else {
  36. // 云函数只能在 /tmp 目录下写文件
  37. $stream = IS_SCF ? 'php://stdout' : sprintf('%s/logs/%s.log', ROOT_PATH, date('Y-m/d'));
  38. }
  39. $handler = new StreamHandler(
  40. $stream,
  41. config('debug') ? Logger::DEBUG : Logger::INFO
  42. );
  43. $handler->setFormatter(new ColoredLineFormatter(null, "[%datetime%] %channel%.%level_name%: %message%\n"));
  44. $logger = new Logger('ff');
  45. $logger->pushHandler($handler);
  46. self::$loggerInstance = $logger;
  47. }
  48. return self::$loggerInstance;
  49. }
  50. /**
  51. * @param $message
  52. * @param array $context
  53. *
  54. * @return bool
  55. * @throws \Exception
  56. */
  57. public static function debug($message, array $context = [])
  58. {
  59. return self::logger()->addDebug($message, $context);
  60. }
  61. /**
  62. * @param $message
  63. * @param array $context
  64. *
  65. * @return bool
  66. * @throws \Exception
  67. */
  68. public static function info($message, array $context = [])
  69. {
  70. return self::logger()->addInfo($message, $context);
  71. }
  72. /**
  73. * @param $message
  74. * @param array $context
  75. *
  76. * @return bool
  77. * @throws \Exception
  78. */
  79. public static function notice($message, array $context = [])
  80. {
  81. return self::logger()->addNotice($message, $context);
  82. }
  83. /**
  84. * @param $message
  85. * @param array $context
  86. *
  87. * @return bool
  88. * @throws \Exception
  89. */
  90. public static function warning($message, array $context = [])
  91. {
  92. return self::logger()->addWarning($message, $context);
  93. }
  94. /**
  95. * @param $message
  96. * @param array $context
  97. *
  98. * @return bool
  99. * @throws \Exception
  100. */
  101. public static function error($message, array $context = [])
  102. {
  103. return self::logger()->addError($message, $context);
  104. }
  105. /**
  106. * @param $message
  107. * @param array $context
  108. *
  109. * @return bool
  110. * @throws \Exception
  111. */
  112. public static function alert($message, array $context = [])
  113. {
  114. return self::logger()->addAlert($message, $context);
  115. }
  116. /**
  117. * @param $message
  118. * @param array $context
  119. *
  120. * @return bool
  121. * @throws \Exception
  122. */
  123. public static function emergency($message, array $context = [])
  124. {
  125. return self::logger()->addEmergency($message, $context);
  126. }
  127. }