1 | <?php |
||
8 | trait Loggable |
||
9 | { |
||
10 | /** @var LoggerInterface */ |
||
11 | private $logger; |
||
12 | |||
13 | /** |
||
14 | * Set a logger to the API. Must implement de PHP FIG PSR-3 LoggerInterface. |
||
15 | * |
||
16 | * @param LoggerInterface $logger |
||
17 | * |
||
18 | * @return $this return the instance for method chaining |
||
19 | */ |
||
20 | 3 | public function setLogger(LoggerInterface $logger) |
|
21 | { |
||
22 | 3 | $this->logger = $logger; |
|
23 | |||
24 | 3 | return $this; |
|
25 | } |
||
26 | |||
27 | /** |
||
28 | * If some logger is defined, log the info with the given severity level. |
||
29 | * |
||
30 | * @param string $level |
||
31 | * @param string $message |
||
32 | * @param array $context |
||
33 | * |
||
34 | * @return $this |
||
35 | */ |
||
36 | 3 | protected function log($level, $message, $context = []) |
|
37 | { |
||
38 | 3 | if ($this->logger !== null) { |
|
39 | 2 | $this->logger->log($level, $message, $context); |
|
40 | 2 | } |
|
41 | |||
42 | 3 | return $this; |
|
43 | } |
||
44 | |||
45 | /** |
||
46 | * Log a message, and maybe the context, with the severity level of EMERGENCY. |
||
47 | * |
||
48 | * @param string $message |
||
49 | * @param array $context |
||
50 | * |
||
51 | * @return $this |
||
52 | */ |
||
53 | 1 | protected function emergency($message, $context = []) |
|
54 | { |
||
55 | 1 | return $this->log(LogLevel::EMERGENCY, $message, $context); |
|
56 | } |
||
57 | |||
58 | /** |
||
59 | * Log a message, and maybe the context, with the severity level of ALERT. |
||
60 | * |
||
61 | * @param string $message |
||
62 | * @param array $context |
||
63 | * |
||
64 | * @return $this |
||
65 | */ |
||
66 | 1 | protected function alert($message, $context = []) |
|
67 | { |
||
68 | 1 | return $this->log(LogLevel::ALERT, $message, $context); |
|
69 | } |
||
70 | |||
71 | /** |
||
72 | * Log a message, and maybe the context, with the severity level of CRITICAL. |
||
73 | * |
||
74 | * @param string $message |
||
75 | * @param array $context |
||
76 | * |
||
77 | * @return $this |
||
78 | */ |
||
79 | 1 | protected function critical($message, $context = []) |
|
80 | { |
||
81 | 1 | return $this->log(LogLevel::CRITICAL, $message, $context); |
|
82 | } |
||
83 | |||
84 | /** |
||
85 | * Log a message, and maybe the context, with the severity level of ERROR. |
||
86 | * |
||
87 | * @param string $message |
||
88 | * @param array $context |
||
89 | * |
||
90 | * @return $this |
||
91 | */ |
||
92 | 1 | protected function error($message, $context = []) |
|
93 | { |
||
94 | 1 | return $this->log(LogLevel::ERROR, $message, $context); |
|
95 | } |
||
96 | |||
97 | /** |
||
98 | * Log a message, and maybe the context, with the severity level of WARNING. |
||
99 | * |
||
100 | * @param string $message |
||
101 | * @param array $context |
||
102 | * |
||
103 | * @return $this |
||
104 | */ |
||
105 | 1 | protected function warning($message, $context = []) |
|
106 | { |
||
107 | 1 | return $this->log(LogLevel::WARNING, $message, $context); |
|
108 | } |
||
109 | |||
110 | /** |
||
111 | * Log a message, and maybe the context, with the severity level of NOTICE. |
||
112 | * |
||
113 | * @param string $message |
||
114 | * @param array $context |
||
115 | * |
||
116 | * @return $this |
||
117 | */ |
||
118 | 1 | protected function notice($message, $context = []) |
|
119 | { |
||
120 | 1 | return $this->log(LogLevel::NOTICE, $message, $context); |
|
121 | } |
||
122 | |||
123 | /** |
||
124 | * Log a message, and maybe the context, with the severity level of INFO. |
||
125 | * |
||
126 | * @param string $message |
||
127 | * @param array $context |
||
128 | * |
||
129 | * @return $this |
||
130 | */ |
||
131 | 1 | protected function info($message, $context = []) |
|
132 | { |
||
133 | 1 | return $this->log(LogLevel::INFO, $message, $context); |
|
134 | } |
||
135 | |||
136 | /** |
||
137 | * Log a message, and maybe the context, with the severity level of DEBUG. |
||
138 | * |
||
139 | * @param string $message |
||
140 | * @param array $context |
||
141 | * |
||
142 | * @return $this |
||
143 | */ |
||
144 | 2 | protected function debug($message, $context = []) |
|
148 | } |
||
149 |