123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566 |
- <?php
- namespace App;
- use Monolog\Logger;
- use Monolog\Handler\StreamHandler;
- use Monolog\Processor\WebProcessor;
- class Log
- {
- public static function log($name, $content, $extra, $level=Logger::ERROR)
- {
- $logger = new Logger($name);
- # 日志文件位置及文件名
- $logFilePath = storage_path('qyWechat/' . date('m-d') . '/' .$name .'.log');
- try {
- $logger->pushHandler(new StreamHandler($logFilePath), $level);
- $logger->pushProcessor(new WebProcessor());
- switch($level) {
- case Logger::INFO:
- $logger->addInfo($content, $extra);
- break;
- case Logger::ERROR:
- $logger->addError($content, $extra);
- break;
- default:
- $logger->addInfo($content, $extra);
- break;
- }
- } catch (\Exception $e) {
- $exceptionLogPath = storage_path('logs/exception/monolog.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 logInfo($content, $extra=array(), $prefix='info')
- {
- self::log($prefix, $content, $extra, Logger::INFO);
- }
- /**
- * 错误日志
- * @param $title
- * @param $content
- * @param string $file
- */
- public static function logError($content, $extra=array(), $prefix='error')
- {
- self::log($prefix, $content, $extra, Logger::ERROR);
- }
- }
|