1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465 |
- <?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('playlet/' . 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);
- }
- }
|