Log.php 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. <?php namespace App\Servers\Logs;
  2. use Monolog\Handler\RotatingFileHandler;
  3. use Monolog\Logger;
  4. /**
  5. * 日志记录类
  6. *
  7. */
  8. class Log{
  9. /**
  10. * 日志记录
  11. * @param String $name 日志类型
  12. * @param String $message 日志信息
  13. * @param Array $content 日志上下文
  14. * @param Int $level 错误等级
  15. * @param Array $options 其他参数
  16. * string filename 文件名称
  17. * string maxFiles 文件数量
  18. */
  19. public function log(string $name,string $message,array $content=[],int $level=Logger::INFO,array $options=[])
  20. {
  21. // Monolog log channel
  22. $log = new Logger($name);
  23. // 最大文件数
  24. $maxFiles = empty($options['maxFiles']) ? 30 : $options['maxFiles'];
  25. // 错误等级
  26. $logPath = empty($options['filename']) ? 'logs/'.$name.'/'. basename($name).'.log' : 'logs/'.$name.'/'.$options['filename'].'.log';
  27. // ✅ 关键修复:自动创建日志目录
  28. $fullPath = storage_path($logPath);
  29. $logDir = dirname($fullPath);
  30. if (!is_dir($logDir)) {
  31. // 递归创建目录,权限 0755
  32. if (!mkdir($logDir, 0755, true)) {
  33. // 如果创建失败,记录到错误日志
  34. error_log("Failed to create log directory: " . $logDir);
  35. return;
  36. }
  37. }
  38. // 初始化Stores logs to files that are rotated every day and a limited number of files are kept.
  39. $log->pushHandler(new RotatingFileHandler(storage_path($logPath), $maxFiles,$level));
  40. // 对应日志级别记录日志
  41. $log->log($level,$message,$content);
  42. }
  43. /**
  44. * Adds a log record at the DEBUG level.
  45. * This method allows for compatibility with common interfaces.
  46. *
  47. */
  48. public function debug(string $name,string $message,array $content=[],array $options=[])
  49. {
  50. $this->log($name,$message,$content,Logger::DEBUG,$options);
  51. }
  52. /**
  53. * Adds a log record at the INFO level.
  54. *
  55. * This method allows for compatibility with common interfaces.
  56. *
  57. */
  58. public function info(string $name,string $message,array $content=[],array $options=[])
  59. {
  60. $this->log($name,$message,$content,Logger::INFO,$options);
  61. }
  62. /**
  63. * Adds a log record at the NOTICE level.
  64. *
  65. * This method allows for compatibility with common interfaces.
  66. *
  67. */
  68. public function notice(string $name,string $message,array $content=[],array $options=[])
  69. {
  70. $this->log($name,$message,$content,Logger::NOTICE, $options);
  71. }
  72. /**
  73. * Adds a log record at the WARNING level.
  74. *
  75. * This method allows for compatibility with common interfaces.
  76. *
  77. */
  78. public function warning(string $name,string $message,array $content=[],array $options=[])
  79. {
  80. $this->log($name,$message,$content,Logger::WARNING, $options);
  81. }
  82. /**
  83. * Adds a log record at the ERROR level.
  84. *
  85. * This method allows for compatibility with common interfaces.
  86. *
  87. */
  88. public function error(string $name,string $message,array $content=[],array $options=[])
  89. {
  90. $this->log($name,$message,$content,Logger::ERROR, $options);
  91. }
  92. /**
  93. * Adds a log record at the CRITICAL level.
  94. *
  95. * This method allows for compatibility with common interfaces.
  96. *
  97. */
  98. public function critical(string $name,string $message,array $content=[],array $options=[])
  99. {
  100. $this->log($name,$message,$content,Logger::CRITICAL, $options);
  101. }
  102. /**
  103. * Adds a log record at the ALERT level.
  104. *
  105. * This method allows for compatibility with common interfaces.
  106. *
  107. */
  108. public function alert(string $name,string $message,array $content=[],array $options=[])
  109. {
  110. $this->log($name,$message,$content,Logger::ALERT, $options);
  111. }
  112. /**
  113. * Adds a log record at the EMERGENCY level.
  114. *
  115. * This method allows for compatibility with common interfaces.
  116. *
  117. */
  118. public function emergency(string $name,string $message,array $content=[],array $options=[])
  119. {
  120. $this->log($name,$message,$content,Logger::EMERGENCY, $options);
  121. }
  122. }