-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLogLevel.php
More file actions
107 lines (95 loc) · 2.48 KB
/
LogLevel.php
File metadata and controls
107 lines (95 loc) · 2.48 KB
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
102
103
104
105
106
107
<?php declare(strict_types=1);
namespace StellarWP\Foundation\Log;
use UnhandledMatchError;
/**
* A wrapper implementation for getting Monolog
* levels.
*/
final class LogLevel
{
/**
* Detailed debug information
*/
public const int DEBUG = 100;
/**
* Interesting events
*
* Examples: User logs in, SQL logs.
*/
public const int INFO = 200;
/**
* Uncommon events
*/
public const int NOTICE = 250;
/**
* Exceptional occurrences that are not errors
*
* Examples: Use of deprecated APIs, poor use of an API,
* undesirable things that are not necessarily wrong.
*/
public const int WARNING = 300;
/**
* Runtime errors
*/
public const int ERROR = 400;
/**
* Critical conditions
*
* Example: Application component unavailable, unexpected exception.
*/
public const int CRITICAL = 500;
/**
* Action must be taken immediately
*
* Example: Entire website down, database unavailable, etc.
* This should trigger the SMS alerts and wake you up.
*/
public const int ALERT = 550;
/**
* Urgent alert.
*/
public const int EMERGENCY = 600;
/**
* Get a Monolog log level code from a name.
*
* @param string $level The log level.
*
* @throws UnhandledMatchError
*
* @return int The Monolog level code.
*/
public static function fromName(string $level): int {
return match ($level) {
default => throw new UnhandledMatchError($level),
'debug', 'Debug', 'DEBUG' => self::DEBUG,
'info', 'Info', 'INFO' => self::INFO,
'notice', 'Notice', 'NOTICE' => self::NOTICE,
'warning', 'Warning', 'WARNING' => self::WARNING,
'error', 'Error', 'ERROR' => self::ERROR,
'critical', 'Critical', 'CRITICAL' => self::CRITICAL,
'alert', 'Alert', 'ALERT' => self::ALERT,
'emergency', 'Emergency', 'EMERGENCY' => self::EMERGENCY,
};
}
/**
* Returns the PSR-3 level matching the Monolog level code.
*
*
* @throws UnhandledMatchError
*
* @phpstan-return \Psr\Log\LogLevel::*
*/
public static function toPsrLogLevel(int $level): string {
return match ($level) {
default => throw new UnhandledMatchError((string) $level),
self::DEBUG => \Psr\Log\LogLevel::DEBUG,
self::INFO => \Psr\Log\LogLevel::INFO,
self::NOTICE => \Psr\Log\LogLevel::NOTICE,
self::WARNING => \Psr\Log\LogLevel::WARNING,
self::ERROR => \Psr\Log\LogLevel::ERROR,
self::CRITICAL => \Psr\Log\LogLevel::CRITICAL,
self::ALERT => \Psr\Log\LogLevel::ALERT,
self::EMERGENCY => \Psr\Log\LogLevel::EMERGENCY,
};
}
}