|
1
|
|
|
<?php namespace Arcanedev\LogViewer\Utilities; |
|
2
|
|
|
|
|
3
|
|
|
use Arcanedev\LogViewer\Contracts\LogLevelsInterface; |
|
4
|
|
|
use Illuminate\Translation\Translator; |
|
5
|
|
|
use Psr\Log\LogLevel; |
|
6
|
|
|
use ReflectionClass; |
|
7
|
|
|
|
|
8
|
|
|
/** |
|
9
|
|
|
* Class LogLevels |
|
10
|
|
|
* |
|
11
|
|
|
* @package Arcanedev\LogViewer\Utilities |
|
12
|
|
|
* @author ARCANEDEV <[email protected]> |
|
13
|
|
|
*/ |
|
14
|
|
|
class LogLevels implements LogLevelsInterface |
|
15
|
|
|
{ |
|
16
|
|
|
/* ------------------------------------------------------------------------------------------------ |
|
17
|
|
|
| Properties |
|
18
|
|
|
| ------------------------------------------------------------------------------------------------ |
|
19
|
|
|
*/ |
|
20
|
|
|
/** |
|
21
|
|
|
* The log levels. |
|
22
|
|
|
* |
|
23
|
|
|
* @var array |
|
24
|
|
|
*/ |
|
25
|
|
|
protected static $levels = []; |
|
26
|
|
|
|
|
27
|
|
|
/** |
|
28
|
|
|
* The Translator instance. |
|
29
|
|
|
* |
|
30
|
|
|
* @var \Illuminate\Translation\Translator |
|
31
|
|
|
*/ |
|
32
|
|
|
private $translator; |
|
33
|
|
|
|
|
34
|
|
|
/* ------------------------------------------------------------------------------------------------ |
|
35
|
|
|
| Constructor |
|
36
|
|
|
| ------------------------------------------------------------------------------------------------ |
|
37
|
|
|
*/ |
|
38
|
|
|
/** |
|
39
|
|
|
* Create LogLevels instance. |
|
40
|
|
|
* |
|
41
|
|
|
* @param \Illuminate\Translation\Translator $translator |
|
42
|
|
|
*/ |
|
43
|
231 |
|
public function __construct(Translator $translator) |
|
44
|
|
|
{ |
|
45
|
231 |
|
$this->setTranslator($translator); |
|
46
|
231 |
|
} |
|
47
|
|
|
|
|
48
|
|
|
/* ------------------------------------------------------------------------------------------------ |
|
49
|
|
|
| Getters & Setters |
|
50
|
|
|
| ------------------------------------------------------------------------------------------------ |
|
51
|
|
|
*/ |
|
52
|
|
|
/** |
|
53
|
|
|
* Set the Translator instance. |
|
54
|
|
|
* |
|
55
|
|
|
* @param \Illuminate\Translation\Translator $translator |
|
56
|
|
|
* |
|
57
|
|
|
* @return self |
|
58
|
|
|
*/ |
|
59
|
231 |
|
public function setTranslator(Translator $translator) |
|
60
|
|
|
{ |
|
61
|
231 |
|
$this->translator = $translator; |
|
62
|
|
|
|
|
63
|
231 |
|
return $this; |
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
|
|
/* ------------------------------------------------------------------------------------------------ |
|
67
|
|
|
| Main Functions |
|
68
|
|
|
| ------------------------------------------------------------------------------------------------ |
|
69
|
|
|
*/ |
|
70
|
|
|
/** |
|
71
|
|
|
* Get the log levels. |
|
72
|
|
|
* |
|
73
|
|
|
* @param bool|false $flip |
|
74
|
|
|
* |
|
75
|
|
|
* @return array |
|
76
|
|
|
*/ |
|
77
|
93 |
|
public function lists($flip = false) |
|
78
|
|
|
{ |
|
79
|
93 |
|
return self::all($flip); |
|
80
|
|
|
} |
|
81
|
|
|
|
|
82
|
|
|
/** |
|
83
|
|
|
* Get translated levels. |
|
84
|
|
|
* |
|
85
|
|
|
* @param string|null $locale |
|
86
|
|
|
* |
|
87
|
|
|
* @return array |
|
88
|
|
|
*/ |
|
89
|
51 |
|
public function names($locale = null) |
|
90
|
|
|
{ |
|
91
|
51 |
|
$levels = self::all(true); |
|
92
|
|
|
|
|
93
|
51 |
|
array_walk($levels, function (&$name, $level) use ($locale) { |
|
94
|
51 |
|
$name = $this->getTranslatedName($level, $locale); |
|
95
|
51 |
|
}); |
|
96
|
|
|
|
|
97
|
51 |
|
return $levels; |
|
98
|
|
|
} |
|
99
|
|
|
|
|
100
|
|
|
/** |
|
101
|
|
|
* Get PSR log levels. |
|
102
|
|
|
* |
|
103
|
|
|
* @param bool|false $flip |
|
104
|
|
|
* |
|
105
|
|
|
* @return array |
|
106
|
|
|
*/ |
|
107
|
234 |
|
public static function all($flip = false) |
|
108
|
|
|
{ |
|
109
|
234 |
|
if (empty(self::$levels)) { |
|
110
|
3 |
|
$class = new ReflectionClass(new LogLevel); |
|
111
|
3 |
|
self::$levels = $class->getConstants(); |
|
112
|
3 |
|
} |
|
113
|
|
|
|
|
114
|
234 |
|
return $flip ? array_flip(self::$levels) : self::$levels; |
|
115
|
|
|
} |
|
116
|
|
|
|
|
117
|
|
|
/* ------------------------------------------------------------------------------------------------ |
|
118
|
|
|
| Other Functions |
|
119
|
|
|
| ------------------------------------------------------------------------------------------------ |
|
120
|
|
|
*/ |
|
121
|
|
|
/** |
|
122
|
|
|
* Translate a level. |
|
123
|
|
|
* |
|
124
|
|
|
* @param string $level |
|
125
|
|
|
* @param string|null $locale |
|
126
|
|
|
* |
|
127
|
|
|
* @return string |
|
128
|
|
|
*/ |
|
129
|
51 |
|
private function getTranslatedName($level, $locale) |
|
130
|
|
|
{ |
|
131
|
51 |
|
if ($locale === 'auto') { |
|
132
|
6 |
|
$locale = null; |
|
133
|
6 |
|
} |
|
134
|
|
|
|
|
135
|
51 |
|
return $this->translator->get('log-viewer::levels.' . $level, [], $locale); |
|
136
|
|
|
} |
|
137
|
|
|
} |
|
138
|
|
|
|