Completed
Push — master ( 120ed1...111628 )
by raphael
8s
created

LaravelLogViewer   A

Complexity

Total Complexity 23

Size/Duplication

Total Lines 153
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Importance

Changes 18
Bugs 1 Features 1
Metric Value
wmc 23
c 18
b 1
f 1
lcom 1
cbo 0
dl 0
loc 153
rs 10

6 Methods

Rating   Name   Duplication   Size   Complexity  
A setFile() 0 8 2
A pathToLogFile() 0 15 3
A getFileName() 0 4 1
C all() 0 55 12
A getFiles() 0 12 4
A getLogLevels() 0 5 1
1
<?php
2
namespace Rap2hpoutre\LaravelLogViewer;
3
4
use Illuminate\Support\Facades\File;
5
use Psr\Log\LogLevel;
6
use ReflectionClass;
7
8
/**
9
 * Class LaravelLogViewer
10
 * @package Rap2hpoutre\LaravelLogViewer
11
 */
12
class LaravelLogViewer
13
{
14
15
    /**
16
     * @var string file
17
     */
18
    private static $file;
19
20
    private static $levels_classes = [
21
        'debug' => 'info',
22
        'info' => 'info',
23
        'notice' => 'info',
24
        'warning' => 'warning',
25
        'error' => 'danger',
26
        'critical' => 'danger',
27
        'alert' => 'danger',
28
        'emergency' => 'danger',
29
    ];
30
31
    private static $levels_imgs = [
32
        'debug' => 'info',
33
        'info' => 'info',
34
        'notice' => 'info',
35
        'warning' => 'warning',
36
        'error' => 'warning',
37
        'critical' => 'warning',
38
        'alert' => 'warning',
39
        'emergency' => 'warning',
40
    ];
41
42
    const MAX_FILE_SIZE = 52428800; // Why? Uh... Sorry
43
44
    /**
45
     * @param string $file
46
     */
47
    public static function setFile($file)
48
    {
49
        $file = self::pathToLogFile($file);
50
51
        if (File::exists($file)) {
52
            self::$file = $file;
53
        }
54
    }
55
56
    public static function pathToLogFile($file)
57
    {
58
        $logsPath = storage_path('logs');
59
60
        if (! File::exists($file)) { // try the absolute path
61
            $file = $logsPath . '/' . $file;
62
        }
63
64
        // check if requested file is really in the logs directory
65
        if (dirname($file) !== $logsPath) {
66
            throw new \Exception('No such log file');
67
        }
68
69
        return $file;
70
    }
71
72
    /**
73
     * @return string
74
     */
75
    public static function getFileName()
76
    {
77
        return basename(self::$file);
78
    }
79
80
    /**
81
     * @return array
82
     */
83
    public static function all()
84
    {
85
        $log = array();
86
87
        $log_levels = self::getLogLevels();
88
89
        $pattern = '/\[\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2}\].*/';
90
91
        if (!self::$file) {
92
            $log_file = self::getFiles();
93
            if(!count($log_file)) {
94
                return [];
95
            }
96
            self::$file = $log_file[0];
97
        }
98
99
        if (File::size(self::$file) > self::MAX_FILE_SIZE) return null;
100
101
        $file = File::get(self::$file);
102
103
        preg_match_all($pattern, $file, $headings);
104
105
        if (!is_array($headings)) return $log;
106
107
        $log_data = preg_split($pattern, $file);
108
109
        if ($log_data[0] < 1) {
110
            array_shift($log_data);
111
        }
112
113
        foreach ($headings as $h) {
114
            for ($i=0, $j = count($h); $i < $j; $i++) {
115
                foreach ($log_levels as $level_key => $level_value) {
116
                    if (strpos(strtolower($h[$i]), '.' . $level_value)) {
117
118
                        preg_match('/^\[(\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2})\].*?\.' . $level_key . ': (.*?)( in .*?:[0-9]+)?$/', $h[$i], $current);
119
120
                        if (!isset($current[2])) continue;
121
122
                        $log[] = array(
123
                            'level' => $level_value,
124
                            'level_class' => self::$levels_classes[$level_value],
125
                            'level_img' => self::$levels_imgs[$level_value],
126
                            'date' => $current[1],
127
                            'text' => $current[2],
128
                            'in_file' => isset($current[3]) ? $current[3] : null,
129
                            'stack' => preg_replace("/^\n*/", '', $log_data[$i])
130
                        );
131
                    }
132
                }
133
            }
134
        }
135
136
        return array_reverse($log);
137
    }
138
139
    /**
140
     * @param bool $basename
141
     * @return array
142
     */
143
    public static function getFiles($basename = false)
144
    {
145
        $files = glob(storage_path() . '/logs/*');
146
        $files = array_reverse($files);
147
        $files = array_filter($files, 'is_file');
148
        if ($basename && is_array($files)) {
149
            foreach ($files as $k => $file) {
150
                $files[$k] = basename($file);
151
            }
152
        }
153
        return array_values($files);
154
    }
155
156
    /**
157
     * @return array
158
     */
159
    private static function getLogLevels()
160
    {
161
        $class = new ReflectionClass(new LogLevel);
162
        return $class->getConstants();
163
    }
164
}
165