123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104 |
- <?php
- namespace App;
- use Monolog\Logger;
- use Monolog\Handler\StreamHandler;
- use Monolog\Processor\WebProcessor;
- class Log
- {
- public static function errorLog($params,$returnValue,$interfaceName,$flag,$headers=[],$responseTime=NULL){
- $data['time']=date("Y-m-d H:i:s",time());
- $data["interfaceName"]=$interfaceName;
- $data['responseTime'] = $responseTime;
- $data["params"]=$params;
- $data["headers"]=$headers;
- $data["returnValue"]=$returnValue;
-
- if($flag){
- $tmpDir="success/";
- }else{
- $tmpDir="error/";
- }
- $name=date("Y-m-d",time()).".txt";
- error_log(json_encode($data, JSON_UNESCAPED_UNICODE)."\n",3,"/log/order_consumption_system_new/".$tmpDir.$name);
- }
- public static function scriptLog($params,$err_msg,$flag){
- $data['time']=date("Y-m-d H:i:s",time());
- $data["params"]=$params;
- $data["err_msg"]=$err_msg;
- if($flag){
- $tmpDir="script_success/";
- }else{
- $tmpDir="script_error/";
- }
- $name=date("Y-m-d",time()).".txt";
- error_log(json_encode($data, JSON_UNESCAPED_UNICODE)."\n",3,"/log/order_consumption_system_new/".$tmpDir.$name);
- }
- public static function importLog($params, $type) {
- $data['time'] = date("Y-m-d H:i:s",time());
- $data['params'] = $params;
- $data['type'] = $type;
- $tmpDir="import/";
- $name=date("Y-m-d",time()).".txt";
- error_log(json_encode($data, JSON_UNESCAPED_UNICODE)."\n",3,"/log/order_consumption_system_new/".$tmpDir.$name);
- }
- /**
- * 信息日志
- * @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);
- }
- public static function log($name, $content, $extra, $level=Logger::ERROR)
- {
- $logger = new Logger($name);
- # 日志文件位置及文件名
- $logFilePath = storage_path('xhb/' . 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);
- }
- }
- }
|