Completed
Pull Request — master (#105)
by
unknown
12:20
created

LaravelLogViewer::setLogLevel()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 7
rs 9.4285
cc 3
eloc 4
nc 2
nop 1
1
<?php
2
namespace Rap2hpoutre\LaravelLogViewer;
3
4
use Psr\Log\LogLevel;
5
use Illuminate\Support\Facades\Log;
6
7
/**
8
 * Class LaravelLogViewer
9
 * @package Rap2hpoutre\LaravelLogViewer
10
 */
11
class LaravelLogViewer
12
{
13
    /**
14
     * @var string file
15
     */
16
    private static $file;
17
18
    private static $levels_classes = [
19
        'debug' => 'info',
20
        'info' => 'info',
21
        'notice' => 'info',
22
        'warning' => 'warning',
23
        'error' => 'danger',
24
        'critical' => 'danger',
25
        'alert' => 'danger',
26
        'emergency' => 'danger',
27
        'processed' => 'info',
28
    ];
29
30
    private static $levels_imgs = [
31
        'debug' => 'info',
32
        'info' => 'info',
33
        'notice' => 'info',
34
        'warning' => 'warning',
35
        'error' => 'warning',
36
        'critical' => 'warning',
37
        'alert' => 'warning',
38
        'emergency' => 'warning',
39
        'processed' => 'info'
40
    ];
41
42
    /**
43
     * Log levels that are used
44
     * @var array
45
     */
46
    private static $log_levels = [
47
        'emergency',
48
        'alert',
49
        'critical',
50
        'error',
51
        'warning',
52
        'notice',
53
        'info',
54
        'debug',
55
        'processed'
56
    ];
57
58
    const MAX_FILE_SIZE = 52428800; // Why? Uh... Sorry
59
60
    /**
61
     * @param string $file
62
     */
63
    public static function setFile($file)
64
    {
65
        $file = self::pathToLogFile($file);
66
67
        if (app('files')->exists($file)) {
68
            self::$file = $file;
69
        }
70
    }
71
72
    /**
73
     * @param string $file
74
     * @return string
75
     * @throws \Exception
76
     */
77
    public static function pathToLogFile($file)
78
    {
79
        $logsPath = storage_path('logs');
80
81
        if (app('files')->exists($file)) { // try the absolute path
82
            return $file;
83
        }
84
        
85
        $file = $logsPath . '/' . $file;
86
87
        // check if requested file is really in the logs directory
88
        if (dirname($file) !== $logsPath) {
89
            throw new \Exception('No such log file');
90
        }
91
92
        return $file;
93
    }
94
95
    /**
96
     * @return string
97
     */
98
    public static function getFileName()
99
    {
100
        return basename(self::$file);
101
    }
102
103
    /**
104
     * @return array
105
     */
106
    public static function all()
107
    {
108
        $log = array();
109
110
        $pattern = '/\[\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2}\].*/';
111
112
        if (!self::$file) {
113
            $log_file = self::getFiles();
114
            if (!count($log_file)) {
115
                return [];
116
            }
117
            self::$file = $log_file[0];
118
        }
119
120
        if (app('files')->size(self::$file) > self::MAX_FILE_SIZE) {
121
            return null;
122
        }
123
124
        $file = app('files')->get(self::$file);
125
126
        preg_match_all($pattern, $file, $headings);
127
128
        if (!is_array($headings)) {
129
            return $log;
130
        }
131
132
        $log_data = preg_split($pattern, $file);
133
134
        if ($log_data[0] < 1) {
135
            array_shift($log_data);
136
        }
137
        
138
        foreach ($headings as $h) {
139
            for ($i=0, $j = count($h); $i < $j; $i++) {
140
                foreach (self::$log_levels as $level) {
141
                    if (strpos(strtolower($h[$i]), '.' . $level) || strpos(strtolower($h[$i]), $level . ':')) {
142
                        preg_match('/^\[(\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2})\](?:.*?(\w+)\.|.*?)' . $level . ': (.*?)( in .*?:[0-9]+)?$/i', $h[$i], $current);
143
                        if (!isset($current[3])) {
144
                            continue;
145
                        }
146
147
                        $log[] = array(
148
                            'context' => $current[2],
149
                            'level' => $level,
150
                            'level_class' => self::$levels_classes[$level],
151
                            'level_img' => self::$levels_imgs[$level],
152
                            'date' => $current[1],
153
                            'text' => $current[3],
154
                            'in_file' => isset($current[4]) ? $current[4] : null,
155
                            'stack' => preg_replace("/^\n*/", '', $log_data[$i])
156
                        );
157
                    }
158
                }
159
            }
160
        }
161
162
        return array_reverse($log);
163
    }
164
165
    /**
166
     * @param bool $basename
167
     * @return array
168
     */
169
    public static function getFiles($basename = false)
170
    {
171
        $files = glob(storage_path() . '/logs/*.log');
172
        $files = array_reverse($files);
173
        $files = array_filter($files, 'is_file');
174
        if ($basename && is_array($files)) {
175
            foreach ($files as $k => $file) {
176
                $files[$k] = basename($file);
177
            }
178
        }
179
        return array_values($files);
180
    }
181
182
183
    /**
184
     * SetLogLevel function
185
     *
186
     * @param array $loglevel
187
     * @return void
188
     */
189
    public static function setLogLevel($loglevel)
190
    {
191
        if (is_array($loglevel)&&count($loglevel)>0) {
192
            self::$log_levels=$loglevel;
193
            Log::debug('设置自定义配置成功!');
194
        }
195
    }
196
}
197