123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169 |
- <?php
- namespace app\utils;
- /**
- * Author:Panda
- * Email:joeyoung0314@qq.com
- * Class LogUtil
- * @package App\Utils
- */
- class LogUtil {
- /**==============日志系统==============**/
- static $logArr = array();
- static $logRootPath = '';//日志根目录
- static $logExtPath = '';//日志根目录02
- static $logExtTypePath = 'api/';//默认通道Api
- public static function getInstance($logExtTypePath){
- self::$logExtTypePath = $logExtTypePath;
- }
- /**
- * 记录各类日志
- * 日志级别:普通
- * @param string $controller 控制器
- * @param string $action 方法
- * @param ...$content //日志参数
- */
- public static function insert(string $controller,string $action, ...$content){
- self::writeLog($content,'Api-Middleware-Log', 'request_log',"{$controller}/{$action}",'local.INFO');
- }
- /**
- * debug 日志
- * @param ...$content
- */
- public static function debug(...$content){
- self::writeLog($content,'Api-Middleware-Log', 'request_log',"debug",'local.WARNING');
- }
- /**
- * 日志级别:普通
- * @param string $logTitle 日志名称
- * @param string $logName 日志目录
- * @param string $logFile 日志文件名
- * @param array $content 日志参数
- */
- public static function info($logTitle = '', $logName = 'Common', $logFile = 'common',$content = [] ){
- self::writeLog($content,$logName,$logFile,$logTitle,'local.INFO');
- }
- /**
- * 日志级别:警告
- * @param string $logTitle 日志名称
- * @param string $logName 日志目录
- * @param string $logFile 日志文件名
- * @param array $content 日志参数
- */
- public static function warning($logTitle = '', $logName = 'Common', $logFile = 'common',$content = [] ){
- self::writeLog($content,$logName,$logFile,$logTitle,'local.WARNING');
- }
- /**
- * 日志级别:错误
- * @param string $logTitle 日志名称
- * @param string $logName 日志目录
- * @param string $logFile 日志文件名
- * @param array $content 日志参数
- */
- public static function error($logTitle = '', $logName = 'Common', $logFile = 'common',$content = [] ){
- self::writeLog($content,$logName,$logFile,$logTitle,'local.ERROR');
- }
- /**
- * 日志内容保存
- * @param $content
- * @param string $logName 日志模块名称
- * @param string $logFile 日志内容
- * @param string $logTitle 日志内容头部
- * @param string $status
- *
- */
- private static function writeLog($content, $logName = 'Common', $logFile = 'common', $logTitle = '',$status = 'INFO') {
- $logPath = $logName.'_'.$logFile;
- $logWhite = self::whiteLog();
- if(in_array($logPath,$logWhite)) return;//验证日志黑名单
- //获取日志文件目录
- if (empty(self::$logRootPath)) self::$logRootPath = realpath(ROOT_PATH) . '/runtime/Log/'.self::$logExtTypePath;
- if (empty(self::$logExtPath)) self::$logExtPath = realpath(ROOT_PATH) . '/runtime/Log02/'.self::$logExtTypePath;
- if (empty(self::$logArr[$logName][$logFile])) {
- self::$logArr[$logName][$logFile] = '================ Start ================'.PHP_EOL;
- }
- if (is_array($content)) $content = json_encode($content, JSON_UNESCAPED_UNICODE);
- if (!empty($logTitle)) $logTitle .= ':';
- self::$logArr[$logName][$logFile] .= '['.(date('Y-m-d H:i:s')."] {$status}: {$logTitle}{$content}".PHP_EOL.PHP_EOL);
- }
- public static function close() {
- try{
- if (empty(self::$logArr)) return;
- //没有Log目录 创建Log02目录
- if(!file_exists(self::$logRootPath)){
- $tem_path = self::$logExtPath;
- if(!file_exists($tem_path)){
- mkdir($tem_path, 0777 ,true);
- }
- $logPath = $tem_path.date('Y_m_d');
- }else{
- $logPath = self::$logRootPath.date('Y_m_d');
- }
- //创建日志文件目录
- if (!file_exists($logPath)) mkdir($logPath);
- //逐个日志文件写入
- foreach (self::$logArr as $key => $value) {
- $logDir = $logPath.'/'.$key;
- if (!file_exists($logDir)) mkdir($logDir , 0777 , true);
- self::logEnd($value, $logDir);
- }
- self::$logArr = null;
- }catch (\Exception $e){
- return;
- }
- }
- public static function logEnd($data, $logDir) {
- foreach ($data as $k => $v) {
- //获取日志文件名 避免单个日志文件太大
- $count = 1;
- while(true) {
- //生成日志文件名
- $logFile = "{$logDir}".'/'."{$k}_{$count}.log";
- //第一次写入日志
- if (!is_file($logFile)) break;
- //日志文件未大于1M
- $file = filesize($logFile) / 1024;
- if ($file < 1024 * 100) break;
- $count++;
- }
- $v = rtrim($v);
- $v .= PHP_EOL.'================ End ================'.PHP_EOL.PHP_EOL;
- error_log($v, 3, $logFile);
- // $ch = fopen($logFile, 'ab');
- // fwrite($ch, $v);
- // fclose($ch);
- }
- }
- /**
- * 日志黑名单
- * @return array
- */
- public static function whiteLog()
- {
- return array(
- // 'Framework/PassportController_login',
- );
- }
- }
|