12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576 |
- <?php
- namespace App;
- use Monolog\Handler\StreamHandler;
- use Monolog\Logger;
- use Monolog\Processor\WebProcessor;
- class Log
- {
- private static function _save($name, $content, $extra, $level = Logger::INFO)
- {
- $logger = new Logger($name);
- # 日志文件位置及文件名
- $logFilePath = storage_path('yhq/' . 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);
- }
- /**
- * 调试日志
- * @param $content
- * @param array $extra
- * @param string $prefix
- */
- public static function debug($content, $extra = [], $prefix = 'debug')
- {
- self::_save($prefix, $content, $extra, Logger::DEBUG);
- }
- }
|