Log.php 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  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. // 初始化Stores logs to files that are rotated every day and a limited number of files are kept.
  28. $log->pushHandler(new RotatingFileHandler(storage_path($logPath), $maxFiles,$level));
  29. // 对应日志级别记录日志
  30. $log->log($level,$message,$content);
  31. }
  32. /**
  33. * Adds a log record at the DEBUG level.
  34. * This method allows for compatibility with common interfaces.
  35. *
  36. */
  37. public function debug(string $name,string $message,array $content=[],array $options=[])
  38. {
  39. $this->log($name,$message,$content,Logger::DEBUG,$options);
  40. }
  41. /**
  42. * Adds a log record at the INFO level.
  43. *
  44. * This method allows for compatibility with common interfaces.
  45. *
  46. */
  47. public function info(string $name,string $message,array $content=[],array $options=[])
  48. {
  49. $this->log($name,$message,$content,Logger::INFO,$options);
  50. }
  51. /**
  52. * Adds a log record at the NOTICE level.
  53. *
  54. * This method allows for compatibility with common interfaces.
  55. *
  56. */
  57. public function notice(string $name,string $message,array $content=[],array $options=[])
  58. {
  59. $this->log($name,$message,$content,Logger::NOTICE, $options);
  60. }
  61. /**
  62. * Adds a log record at the WARNING level.
  63. *
  64. * This method allows for compatibility with common interfaces.
  65. *
  66. */
  67. public function warning(string $name,string $message,array $content=[],array $options=[])
  68. {
  69. $this->log($name,$message,$content,Logger::WARNING, $options);
  70. }
  71. /**
  72. * Adds a log record at the ERROR level.
  73. *
  74. * This method allows for compatibility with common interfaces.
  75. *
  76. */
  77. public function error(string $name,string $message,array $content=[],array $options=[])
  78. {
  79. $this->log($name,$message,$content,Logger::ERROR, $options);
  80. }
  81. /**
  82. * Adds a log record at the CRITICAL level.
  83. *
  84. * This method allows for compatibility with common interfaces.
  85. *
  86. */
  87. public function critical(string $name,string $message,array $content=[],array $options=[])
  88. {
  89. $this->log($name,$message,$content,Logger::CRITICAL, $options);
  90. }
  91. /**
  92. * Adds a log record at the ALERT level.
  93. *
  94. * This method allows for compatibility with common interfaces.
  95. *
  96. */
  97. public function alert(string $name,string $message,array $content=[],array $options=[])
  98. {
  99. $this->log($name,$message,$content,Logger::ALERT, $options);
  100. }
  101. /**
  102. * Adds a log record at the EMERGENCY level.
  103. *
  104. * This method allows for compatibility with common interfaces.
  105. *
  106. */
  107. public function emergency(string $name,string $message,array $content=[],array $options=[])
  108. {
  109. $this->log($name,$message,$content,Logger::EMERGENCY, $options);
  110. }
  111. }