Debian Codegraph @

classes/Log/PhpSystemLogger.php

raw symbols SCIP
1 <?php
2
3 /**
4 * dmarc-srg - A php parser, viewer and summary report generator for incoming DMARC reports.
5 * Copyright (C) 2022 Aleksey Andreev (liuch)
6 *
7 * Available at:
8 * https://github.com/liuch/dmarc-srg
9 *
10 * This program is free software: you can redistribute it and/or modify it
11 * under the terms of the GNU General Public License as published by the Free
12 * Software Foundation, either version 3 of the License.
13 *
14 * This program is distributed in the hope that it will be useful, but WITHOUT
15 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
16 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
17 * more details.
18 *
19 * You should have received a copy of the GNU General Public License along with
20 * this program. If not, see <http://www.gnu.org/licenses/>.
21 *
22 * =========================
23 *
24 * This file contains the class PhpSystemLogger;
25 *
26 * @category API
27 * @package DmarcSrg
28 * @author Aleksey Andreev (liuch)
29 * @license https://www.gnu.org/licenses/gpl-3.0.html GNU/GPLv3
30 */
31
32 namespace Liuch\DmarcSrg\Log;
33
34 /**
35 * Implements a logger to log messages via PHP's system logger,
36 * using the OS's system logging mechanism or a file, depending on
37 * what the error_log PHP configuration directive is set to.
38 */
39 class PhpSystemLogger implements LoggerInterface
40 {
41 /**
42 * System is unusable
43 *
44 * @param string $message
45 * @param array $context
46 *
47 * @return void
48 */
49 public function emergency($message, array $context = []): void
50 {
51 $this->log(LogLevel::EMERGENCY, $message, $context);
52 }
53
54 /**
55 * Action must be taken immediately
56 *
57 * @param string $message
58 * @param array $context
59 *
60 * @return void
61 */
62 public function alert($message, array $context = []): void
63 {
64 $this->log(LogLevel::ALERT, $message, $context);
65 }
66
67 /**
68 * Critical conditions
69 *
70 * @param string $message
71 * @param array $context
72 *
73 * @return void
74 */
75 public function critical($message, array $context = []): void
76 {
77 $this->log(LogLevel::CRITICAL, $message, $context);
78 }
79
80 /**
81 * Runtime errors that do not require immediate action but should typically
82 * be logged and monitored
83 *
84 * @param string $message
85 * @param array $context
86 *
87 * @return void
88 */
89 public function error($message, array $context = []): void
90 {
91 $this->log(LogLevel::ERROR, $message, $context);
92 }
93
94 /**
95 * Exceptional occurrences that are not errors
96 *
97 * @param string $message
98 * @param array $context
99 *
100 * @return void
101 */
102 public function warning($message, array $context = []): void
103 {
104 $this->log(LogLevel::WARNING, $message, $context);
105 }
106
107 /**
108 * Normal but significant events
109 *
110 * @param string $message
111 * @param array $context
112 *
113 * @return void
114 */
115 public function notice($message, array $context = []): void
116 {
117 $this->log(LogLevel::NOTICE, $message, $context);
118 }
119
120 /**
121 * Interesting events
122 *
123 * @param string $message
124 * @param array $context
125 *
126 * @return void
127 */
128 public function info($message, array $context = []): void
129 {
130 $this->log(LogLevel::INFO, $message, $context);
131 }
132
133 /**
134 * Detailed debug information
135 *
136 * @param string $message
137 * @param array $context
138 *
139 * @return void
140 */
141 public function debug($message, array $context = []): void
142 {
143 $this->log(LogLevel::DEBUG, $message, $context);
144 }
145
146 /**
147 * Logs with an arbitrary level
148 *
149 * @param mixed $level
150 * @param string $message
151 * @param array $context
152 *
153 * @return void
154 */
155 public function log($level, $message, array $context = []): void
156 {
157 switch ($level) {
158 case LogLevel::EMERGENCY:
159 case LogLevel::ALERT:
160 case LogLevel::CRITICAL:
161 case LogLevel::ERROR:
162 case LogLevel::WARNING:
163 case LogLevel::NOTICE:
164 case LogLevel::INFO:
165 case LogLevel::DEBUG:
166 break;
167 default:
168 throw new \InvalidArgumentException('Invalid log level argument');
169 }
170 \error_log("dmarc-srg [{$level}]: {$message}");
171 }
172 }