|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/* |
|
4
|
|
|
* This file is part of the TYPO3 CMS project. |
|
5
|
|
|
* |
|
6
|
|
|
* It is free software; you can redistribute it and/or modify it under |
|
7
|
|
|
* the terms of the GNU General Public License, either version 2 |
|
8
|
|
|
* of the License, or any later version. |
|
9
|
|
|
* |
|
10
|
|
|
* For the full copyright and license information, please read the |
|
11
|
|
|
* LICENSE.txt file that was distributed with this source code. |
|
12
|
|
|
* |
|
13
|
|
|
* The TYPO3 project - inspiring people to share! |
|
14
|
|
|
*/ |
|
15
|
|
|
|
|
16
|
|
|
namespace TYPO3\CMS\Core\Log; |
|
17
|
|
|
|
|
18
|
|
|
use Psr\Log\InvalidArgumentException; |
|
19
|
|
|
|
|
20
|
|
|
/** |
|
21
|
|
|
* Log levels according to RFC 3164 |
|
22
|
|
|
*/ |
|
23
|
|
|
class LogLevel extends \Psr\Log\LogLevel |
|
24
|
|
|
{ |
|
25
|
|
|
/** |
|
26
|
|
|
* Reverse look up of log level to level name. |
|
27
|
|
|
* |
|
28
|
|
|
* @var array |
|
29
|
|
|
*/ |
|
30
|
|
|
protected static $levels = [ |
|
31
|
|
|
self::EMERGENCY, |
|
32
|
|
|
self::ALERT, |
|
33
|
|
|
self::CRITICAL, |
|
34
|
|
|
self::ERROR, |
|
35
|
|
|
self::WARNING, |
|
36
|
|
|
self::NOTICE, |
|
37
|
|
|
self::INFO, |
|
38
|
|
|
self::DEBUG, |
|
39
|
|
|
]; |
|
40
|
|
|
|
|
41
|
|
|
/** |
|
42
|
|
|
* Resolves the name of a log level and returns it in upper case letters |
|
43
|
|
|
* |
|
44
|
|
|
* @param int $level Log level. |
|
45
|
|
|
* @return string Log level name. |
|
46
|
|
|
*/ |
|
47
|
|
|
public static function getName(int $level): string |
|
48
|
|
|
{ |
|
49
|
|
|
return strtoupper(static::getInternalName($level)); |
|
50
|
|
|
} |
|
51
|
|
|
|
|
52
|
|
|
/** |
|
53
|
|
|
* Resolves the name of the log level and returns its internal string representation |
|
54
|
|
|
* |
|
55
|
|
|
* @param int $level |
|
56
|
|
|
* @return string |
|
57
|
|
|
*/ |
|
58
|
|
|
public static function getInternalName(int $level): string |
|
59
|
|
|
{ |
|
60
|
|
|
self::validateLevel($level); |
|
61
|
|
|
return static::$levels[$level]; |
|
62
|
|
|
} |
|
63
|
|
|
|
|
64
|
|
|
/** |
|
65
|
|
|
* Checks a level for validity, |
|
66
|
|
|
* whether it is an integer and in the range of 0-7. |
|
67
|
|
|
* |
|
68
|
|
|
* @param int $level log level to validate |
|
69
|
|
|
* @return bool TRUE if the given log level is valid, FALSE otherwise |
|
70
|
|
|
*/ |
|
71
|
|
|
public static function isValidLevel(int $level): bool |
|
72
|
|
|
{ |
|
73
|
|
|
return isset(static::$levels[$level]); |
|
74
|
|
|
} |
|
75
|
|
|
|
|
76
|
|
|
/** |
|
77
|
|
|
* Validates a log level. |
|
78
|
|
|
* |
|
79
|
|
|
* @param int $level log level to validate |
|
80
|
|
|
* @throws InvalidArgumentException if the given log level is invalid |
|
81
|
|
|
*/ |
|
82
|
|
|
public static function validateLevel(int $level): void |
|
83
|
|
|
{ |
|
84
|
|
|
if (!self::isValidLevel($level)) { |
|
85
|
|
|
throw new InvalidArgumentException('Invalid Log Level ' . $level, 1321637121); |
|
86
|
|
|
} |
|
87
|
|
|
} |
|
88
|
|
|
|
|
89
|
|
|
/** |
|
90
|
|
|
* Normalizes level by converting it from string to integer |
|
91
|
|
|
* |
|
92
|
|
|
* @param string|int $level |
|
93
|
|
|
* @return int |
|
94
|
|
|
*/ |
|
95
|
|
|
public static function normalizeLevel($level): int |
|
96
|
|
|
{ |
|
97
|
|
|
if (is_string($level)) { |
|
98
|
|
|
if (!defined(__CLASS__ . '::' . strtoupper($level))) { |
|
99
|
|
|
throw new InvalidArgumentException('Invalid Log Level ' . $level, 1550247164); |
|
100
|
|
|
} |
|
101
|
|
|
return array_search(strtolower($level), self::$levels, true); |
|
|
|
|
|
|
102
|
|
|
} |
|
103
|
|
|
|
|
104
|
|
|
return (int)$level; |
|
105
|
|
|
} |
|
106
|
|
|
|
|
107
|
|
|
/** |
|
108
|
|
|
* Returns a list of all log levels at least as severe as the specified level. |
|
109
|
|
|
* |
|
110
|
|
|
* @param int|string $level |
|
111
|
|
|
* @return array<string> |
|
112
|
|
|
*/ |
|
113
|
|
|
public static function atLeast($level): array |
|
114
|
|
|
{ |
|
115
|
|
|
$level = self::normalizeLevel($level); |
|
116
|
|
|
return array_filter(self::$levels, static fn ($intLevel) => $intLevel <= $level, ARRAY_FILTER_USE_KEY); |
|
117
|
|
|
} |
|
118
|
|
|
} |
|
119
|
|
|
|