-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLevel.php
101 lines (88 loc) · 2.73 KB
/
Level.php
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
<?php
declare(strict_types=1);
namespace SonsOfPHP\Component\Logger;
use Psr\Log\LogLevel;
use SonsOfPHP\Contract\Logger\LevelInterface;
use UnhandledMatchError;
use ValueError;
/**
* @author Joshua Estes <[email protected]>
*/
enum Level: int implements LevelInterface
{
case Emergency = 0; // Highest
case Alert = 1;
case Critical = 2;
case Error = 3;
case Warning = 4;
case Notice = 5;
case Info = 6;
case Debug = 7; // Lowest
public static function fromName(string $name): LevelInterface
{
try {
return match (strtolower($name)) {
'emergency' => self::Emergency,
'alert' => self::Alert,
'critical' => self::Critical,
'error' => self::Error,
'warning' => self::Warning,
'notice' => self::Notice,
'info' => self::Info,
'debug' => self::Debug,
};
} catch (UnhandledMatchError $unhandledMatchError) {
throw new ValueError(sprintf('"%s" is invalid', $name), $unhandledMatchError->getCode(), $unhandledMatchError);
}
}
public static function tryFromName(string $name): ?LevelInterface
{
try {
return self::fromName($name);
} catch (ValueError) {
}
return null;
}
public function getName(): string
{
return match ($this) {
self::Emergency => 'EMERGENCY',
self::Alert => 'ALERT',
self::Critical => 'CRITICAL',
self::Error => 'ERROR',
self::Warning => 'WARNING',
self::Notice => 'NOTICE',
self::Info => 'INFO',
self::Debug => 'DEBUG',
};
}
public function equals(LevelInterface $level): bool
{
return $this->value === $level->value;
}
public function includes(LevelInterface $level): bool
{
return $this->value >= $level->value;
}
public function isHigherThan(LevelInterface $level): bool
{
return $this->value < $level->value;
}
public function isLowerThan(LevelInterface $level): bool
{
return $this->value > $level->value;
}
public function toPsrLogLevel(): string
{
return match ($this) {
self::Emergency => LogLevel::EMERGENCY,
self::Alert => LogLevel::ALERT,
self::Critical => LogLevel::CRITICAL,
self::Error => LogLevel::ERROR,
self::Warning => LogLevel::WARNING,
self::Notice => LogLevel::NOTICE,
self::Info => LogLevel::INFO,
self::Debug => LogLevel::DEBUG,
};
}
}