123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169 |
- <?php
- namespace app\utils;
- class LogUtil {
-
- static $logArr = array();
- static $logRootPath = '';
- static $logExtPath = '';
- static $logExtTypePath = 'api/';
- public static function getInstance($logExtTypePath){
- self::$logExtTypePath = $logExtTypePath;
- }
-
- public static function insert(string $controller,string $action, ...$content){
- self::writeLog($content,'Api-Middleware-Log', 'request_log',"{$controller}/{$action}",'local.INFO');
- }
-
- public static function debug(...$content){
- self::writeLog($content,'Api-Middleware-Log', 'request_log',"debug",'local.WARNING');
- }
-
- public static function info($logTitle = '', $logName = 'Common', $logFile = 'common',$content = [] ){
- self::writeLog($content,$logName,$logFile,$logTitle,'local.INFO');
- }
-
- public static function warning($logTitle = '', $logName = 'Common', $logFile = 'common',$content = [] ){
- self::writeLog($content,$logName,$logFile,$logTitle,'local.WARNING');
- }
-
- public static function error($logTitle = '', $logName = 'Common', $logFile = 'common',$content = [] ){
- self::writeLog($content,$logName,$logFile,$logTitle,'local.ERROR');
- }
-
- 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;
-
- 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;
-
- $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);
- }
- }
-
- public static function whiteLog()
- {
- return array(
- );
- }
- }
|