123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125 |
- <?php namespace App\Servers\Logs;
- use Monolog\Handler\RotatingFileHandler;
- use Monolog\Logger;
- /**
- * 日志记录类
- *
- */
- class Log{
- /**
- * 日志记录
- * @param String $name 日志类型
- * @param String $message 日志信息
- * @param Array $content 日志上下文
- * @param Int $level 错误等级
- * @param Array $options 其他参数
- * string filename 文件名称
- * string maxFiles 文件数量
- */
- public function log(string $name,string $message,array $content=[],int $level=Logger::INFO,array $options=[])
- {
- // Monolog log channel
- $log = new Logger($name);
- // 最大文件数
- $maxFiles = empty($options['maxFiles']) ? 30 : $options['maxFiles'];
- // 错误等级
- $logPath = empty($options['filename']) ? 'logs/'.$name.'/'. basename($name).'.log' : 'logs/'.$name.'/'.$options['filename'].'.log';
- // 初始化Stores logs to files that are rotated every day and a limited number of files are kept.
- $log->pushHandler(new RotatingFileHandler(storage_path($logPath), $maxFiles,$level));
- // 对应日志级别记录日志
- $log->log($level,$message,$content);
- }
- /**
- * Adds a log record at the DEBUG level.
- * This method allows for compatibility with common interfaces.
- *
- */
- public function debug(string $name,string $message,array $content=[],array $options=[])
- {
- $this->log($name,$message,$content,Logger::DEBUG,$options);
- }
- /**
- * Adds a log record at the INFO level.
- *
- * This method allows for compatibility with common interfaces.
- *
- */
- public function info(string $name,string $message,array $content=[],array $options=[])
- {
- $this->log($name,$message,$content,Logger::INFO,$options);
- }
- /**
- * Adds a log record at the NOTICE level.
- *
- * This method allows for compatibility with common interfaces.
- *
- */
- public function notice(string $name,string $message,array $content=[],array $options=[])
- {
- $this->log($name,$message,$content,Logger::NOTICE, $options);
- }
- /**
- * Adds a log record at the WARNING level.
- *
- * This method allows for compatibility with common interfaces.
- *
- */
- public function warning(string $name,string $message,array $content=[],array $options=[])
- {
- $this->log($name,$message,$content,Logger::WARNING, $options);
- }
- /**
- * Adds a log record at the ERROR level.
- *
- * This method allows for compatibility with common interfaces.
- *
- */
- public function error(string $name,string $message,array $content=[],array $options=[])
- {
- $this->log($name,$message,$content,Logger::ERROR, $options);
- }
- /**
- * Adds a log record at the CRITICAL level.
- *
- * This method allows for compatibility with common interfaces.
- *
- */
- public function critical(string $name,string $message,array $content=[],array $options=[])
- {
- $this->log($name,$message,$content,Logger::CRITICAL, $options);
- }
- /**
- * Adds a log record at the ALERT level.
- *
- * This method allows for compatibility with common interfaces.
- *
- */
- public function alert(string $name,string $message,array $content=[],array $options=[])
- {
- $this->log($name,$message,$content,Logger::ALERT, $options);
- }
- /**
- * Adds a log record at the EMERGENCY level.
- *
- * This method allows for compatibility with common interfaces.
- *
- */
- public function emergency(string $name,string $message,array $content=[],array $options=[])
- {
- $this->log($name,$message,$content,Logger::EMERGENCY, $options);
- }
- }
|