12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485 |
- <?php
- namespace App\Support;
- use App\Models\Sys\SysLogRecord;
- use Monolog\Logger;
- use Monolog\Handler\StreamHandler;
- use Monolog\Processor\WebProcessor;
- class Log
- {
- private static function _save($name, $content, $extra, $level = Logger::INFO)
- {
- $logger = new Logger($name);
- # 日志文件位置及文件名
- $logFilePath = storage_path('kwaiAD/' . date('m-d') . '/' .$name .'.log');
- try {
- $logger->pushHandler(new StreamHandler($logFilePath), $level);
- $logger->pushProcessor(new WebProcessor());
- switch($level) {
- case Logger::INFO:
- $logger->info($content, $extra);
- break;
- case Logger::ERROR:
- $logger->error($content, $extra);
- break;
- case Logger::DEBUG:
- $logger->debug($content, $extra);
- break;
- default:
- $logger->warning('日志类型传递有误,暂时存在三种类型:', ['info', 'error', 'debug']);
- }
- } catch (\Exception $e) {
- $exceptionLogPath = storage_path('logs/exception.log');
- file_put_contents($exceptionLogPath, date('Y-m-d H:i:s') . " " . $e->getMessage() . "[". json_encode($extra, JSON_UNESCAPED_UNICODE) ."【".$name."】]". "\r\n", FILE_APPEND);
- }
- }
- /**
- * 信息日志
- * @param $title
- * @param $content
- * @param string $file
- */
- public static function info($content, $extra = [], $prefix = 'info')
- {
- self::_save($prefix, $content, $extra, Logger::INFO);
- }
- /**
- * 错误日志
- * @param $title
- * @param $content
- * @param string $file
- */
- public static function error($content, $extra = [], $prefix = 'error')
- {
- self::_save($prefix, $content, $extra, Logger::ERROR);
- if ($prefix != 'exception') {
- SysLogRecord::query()->insert([
- 'level' => 'ERROR',
- 'content' => $content,
- 'file' => $prefix
- ]);
- }
- }
- /**
- * 调试日志
- * @param $content
- * @param array $extra
- * @param string $prefix
- */
- public static function debug($content, $extra = [], $prefix = 'debug')
- {
- self::_save($prefix, $content, $extra, Logger::DEBUG);
- }
- }
|