15295036569
2023-06-12 6bc6f63c8c7ef55c2d9d78ccc8917c87f8cd9101
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
<?php
 
/**
 * Copyright 2019 Huawei Technologies Co.,Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use
 * this file except in compliance with the License.  You may obtain a copy of the
 * License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software distributed
 * under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
 * CONDITIONS OF ANY KIND, either express or implied.  See the License for the
 * specific language governing permissions and limitations under the License.
 *
 */
 
namespace Obs\Log;
 
use Monolog\Formatter\LineFormatter;
use Monolog\Handler\RotatingFileHandler;
use Monolog\Logger;
 
class ObsLog extends Logger
{
    public static $log = null;
    
    protected $log_path = './';
    protected $log_name = null;
    protected $log_level = Logger::DEBUG;
    protected $log_maxFiles = 0;
    
    private $formatter = null;
    private $handler = null;
    private $filepath = '';
    
    public static function initLog($logConfig= [])
    {
        $s3log = new ObsLog(''); 
        $s3log->setConfig($logConfig);
        $s3log->cheakDir();
        $s3log->setFilePath();
        $s3log->setFormat();
        $s3log->setHande();
    }
    private function setFormat()
    {
        $output = '[%datetime%][%level_name%]'.'%message%' . "\n";
        $this->formatter = new LineFormatter($output);
        
    }
    private function setHande()
    {
        self::$log = new Logger('obs_logger');
        $rotating = new RotatingFileHandler($this->filepath, $this->log_maxFiles, $this->log_level);
        $rotating->setFormatter($this->formatter);
        self::$log->pushHandler($rotating);
    }
    private function setConfig($logConfig= [])
    {
        $arr = empty($logConfig) ? ObsConfig::LOG_FILE_CONFIG : $logConfig;
        $this->log_path = iconv('UTF-8', 'GBK',$arr['FilePath']);
        $this->log_name = iconv('UTF-8', 'GBK',$arr['FileName']);
        $this->log_maxFiles = is_numeric($arr['MaxFiles']) ? 0 : intval($arr['MaxFiles']);
        $this->log_level = $arr['Level'];
    }
    private function cheakDir()
    {
        if (!is_dir($this->log_path)){
            mkdir($this->log_path, 0755, true);
        }
    } 
    private function setFilePath()
    {
        $this->filepath =  $this->log_path.'/'.$this->log_name;
    }
    private static function writeLog($level, $msg)
    {
        switch ($level) {
            case DEBUG:
                self::$log->debug($msg);
                break;
            case INFO:
                self::$log->info($msg);
                break;
            case NOTICE:
                self::$log->notice($msg);
                break;
            case WARNING:
                self::$log->warning($msg);
                break;
            case ERROR:
                self::$log->error($msg);
                break;
            case CRITICAL:
                self::$log->critical($msg);
                break;
            case ALERT:
                self::$log->alert($msg);
                break;
            case EMERGENCY:
                self::$log->emergency($msg);
                break;
            default:
                break;
        }
        
    }
    
    public static function commonLog($level, $format, $args1 = null, $arg2 = null)
    {
        if(ObsLog::$log){
            if ($args1 === null && $arg2 === null) {
                $msg = urldecode($format);
            } else {
                $msg = sprintf($format, $args1, $arg2);
            }
            $back = debug_backtrace();
            $line = $back[0]['line'];
            $funcname = $back[1]['function'];
            $filename = basename($back[0]['file']);
            $message = '['.$filename.':'.$line.']: '.$msg;
            ObsLog::writeLog($level, $message);
        }
    }
}