1 | <?php
|
---|
2 |
|
---|
3 | namespace Psr\Log;
|
---|
4 |
|
---|
5 | /**
|
---|
6 | * This is a simple Logger trait that classes unable to extend AbstractLogger
|
---|
7 | * (because they extend another class, etc) can include.
|
---|
8 | *
|
---|
9 | * It simply delegates all log-level-specific methods to the `log` method to
|
---|
10 | * reduce boilerplate code that a simple Logger that does the same thing with
|
---|
11 | * messages regardless of the error level has to implement.
|
---|
12 | */
|
---|
13 | trait LoggerTrait
|
---|
14 | {
|
---|
15 | /**
|
---|
16 | * System is unusable.
|
---|
17 | */
|
---|
18 | public function emergency(string|\Stringable $message, array $context = []): void
|
---|
19 | {
|
---|
20 | $this->log(LogLevel::EMERGENCY, $message, $context);
|
---|
21 | }
|
---|
22 |
|
---|
23 | /**
|
---|
24 | * Action must be taken immediately.
|
---|
25 | *
|
---|
26 | * Example: Entire website down, database unavailable, etc. This should
|
---|
27 | * trigger the SMS alerts and wake you up.
|
---|
28 | */
|
---|
29 | public function alert(string|\Stringable $message, array $context = []): void
|
---|
30 | {
|
---|
31 | $this->log(LogLevel::ALERT, $message, $context);
|
---|
32 | }
|
---|
33 |
|
---|
34 | /**
|
---|
35 | * Critical conditions.
|
---|
36 | *
|
---|
37 | * Example: Application component unavailable, unexpected exception.
|
---|
38 | */
|
---|
39 | public function critical(string|\Stringable $message, array $context = []): void
|
---|
40 | {
|
---|
41 | $this->log(LogLevel::CRITICAL, $message, $context);
|
---|
42 | }
|
---|
43 |
|
---|
44 | /**
|
---|
45 | * Runtime errors that do not require immediate action but should typically
|
---|
46 | * be logged and monitored.
|
---|
47 | */
|
---|
48 | public function error(string|\Stringable $message, array $context = []): void
|
---|
49 | {
|
---|
50 | $this->log(LogLevel::ERROR, $message, $context);
|
---|
51 | }
|
---|
52 |
|
---|
53 | /**
|
---|
54 | * Exceptional occurrences that are not errors.
|
---|
55 | *
|
---|
56 | * Example: Use of deprecated APIs, poor use of an API, undesirable things
|
---|
57 | * that are not necessarily wrong.
|
---|
58 | */
|
---|
59 | public function warning(string|\Stringable $message, array $context = []): void
|
---|
60 | {
|
---|
61 | $this->log(LogLevel::WARNING, $message, $context);
|
---|
62 | }
|
---|
63 |
|
---|
64 | /**
|
---|
65 | * Normal but significant events.
|
---|
66 | */
|
---|
67 | public function notice(string|\Stringable $message, array $context = []): void
|
---|
68 | {
|
---|
69 | $this->log(LogLevel::NOTICE, $message, $context);
|
---|
70 | }
|
---|
71 |
|
---|
72 | /**
|
---|
73 | * Interesting events.
|
---|
74 | *
|
---|
75 | * Example: User logs in, SQL logs.
|
---|
76 | */
|
---|
77 | public function info(string|\Stringable $message, array $context = []): void
|
---|
78 | {
|
---|
79 | $this->log(LogLevel::INFO, $message, $context);
|
---|
80 | }
|
---|
81 |
|
---|
82 | /**
|
---|
83 | * Detailed debug information.
|
---|
84 | */
|
---|
85 | public function debug(string|\Stringable $message, array $context = []): void
|
---|
86 | {
|
---|
87 | $this->log(LogLevel::DEBUG, $message, $context);
|
---|
88 | }
|
---|
89 |
|
---|
90 | /**
|
---|
91 | * Logs with an arbitrary level.
|
---|
92 | *
|
---|
93 | * @param mixed $level
|
---|
94 | *
|
---|
95 | * @throws \Psr\Log\InvalidArgumentException
|
---|
96 | */
|
---|
97 | abstract public function log($level, string|\Stringable $message, array $context = []): void;
|
---|
98 | }
|
---|