Log.php 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  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. self::logger()->debug($message, $context);
  60. return true;
  61. }
  62. /**
  63. * @param $message
  64. * @param array $context
  65. *
  66. * @return bool
  67. * @throws \Exception
  68. */
  69. public static function info($message, array $context = [])
  70. {
  71. self::logger()->info($message, $context);
  72. return true;
  73. }
  74. /**
  75. * @param $message
  76. * @param array $context
  77. *
  78. * @return bool
  79. * @throws \Exception
  80. */
  81. public static function notice($message, array $context = [])
  82. {
  83. self::logger()->notice($message, $context);
  84. return true;
  85. }
  86. /**
  87. * @param $message
  88. * @param array $context
  89. *
  90. * @return bool
  91. * @throws \Exception
  92. */
  93. public static function warning($message, array $context = [])
  94. {
  95. self::logger()->warning($message, $context);
  96. return true;
  97. }
  98. /**
  99. * @param $message
  100. * @param array $context
  101. *
  102. * @return bool
  103. * @throws \Exception
  104. */
  105. public static function error($message, array $context = [])
  106. {
  107. self::logger()->error($message, $context);
  108. return true;
  109. }
  110. /**
  111. * @param $message
  112. * @param array $context
  113. *
  114. * @return bool
  115. * @throws \Exception
  116. */
  117. public static function alert($message, array $context = [])
  118. {
  119. self::logger()->alert($message, $context);
  120. return true;
  121. }
  122. /**
  123. * @param $message
  124. * @param array $context
  125. *
  126. * @return bool
  127. * @throws \Exception
  128. */
  129. public static function emergency($message, array $context = [])
  130. {
  131. self::logger()->emergency($message, $context);
  132. return true;
  133. }
  134. }