1 | <?php |
||
27 | class Logger |
||
28 | { |
||
29 | const DEFAULT_NAMESPACE = 'PSFS'; |
||
30 | use SingletonTrait; |
||
31 | /** |
||
32 | * @var \Monolog\Logger |
||
33 | */ |
||
34 | protected $logger; |
||
35 | /** |
||
36 | * @var resource |
||
37 | */ |
||
38 | private $stream; |
||
39 | /** |
||
40 | * @var string |
||
41 | */ |
||
42 | protected $log_level; |
||
43 | |||
44 | /** |
||
45 | * Logger constructor. |
||
46 | * @throws exception\GeneratorException |
||
47 | */ |
||
48 | 2 | public function __construct() |
|
49 | { |
||
50 | 2 | $config = Config::getInstance(); |
|
51 | 2 | $args = func_get_args(); |
|
52 | 2 | list($logger, $debug, $path) = $this->setup($config, $args); |
|
53 | 2 | $this->stream = fopen($path . DIRECTORY_SEPARATOR . date('Ymd') . '.log', 'a+'); |
|
54 | 2 | $this->addPushLogger($logger, $debug, $config); |
|
55 | 2 | $this->log_level = Config::getParam('log.level', 'info'); |
|
56 | 2 | } |
|
57 | |||
58 | 1 | public function __destruct() |
|
59 | { |
||
60 | 1 | fclose($this->stream); |
|
61 | 1 | } |
|
62 | |||
63 | /** |
||
64 | * @param string $msg |
||
65 | * @param array $context |
||
66 | * @return bool |
||
67 | */ |
||
68 | 1 | public function defaultLog($msg = '', array $context = []) |
|
69 | { |
||
70 | 1 | return $this->logger->addNotice($msg, $this->addMinimalContext($context)); |
|
71 | } |
||
72 | |||
73 | /** |
||
74 | * @param string $msg |
||
75 | * @param array $context |
||
76 | * |
||
77 | * @return bool |
||
78 | */ |
||
79 | 4 | public function infoLog($msg = '', array $context = []) |
|
80 | { |
||
81 | 4 | return $this->logger->addInfo($msg, $this->addMinimalContext($context)); |
|
82 | } |
||
83 | |||
84 | /** |
||
85 | * @param string $msg |
||
86 | * @param array $context |
||
87 | * |
||
88 | * @return bool |
||
89 | */ |
||
90 | 22 | public function debugLog($msg = '', array $context = []) |
|
91 | { |
||
92 | 22 | return ($this->log_level === 'debug') ? $this->logger->addDebug($msg, $this->addMinimalContext($context)) : null; |
|
93 | } |
||
94 | |||
95 | /** |
||
96 | * @param $msg |
||
97 | * @param array $context |
||
98 | * |
||
99 | * @return bool |
||
100 | */ |
||
101 | 4 | public function errorLog($msg, array $context = []) |
|
102 | { |
||
103 | 4 | return $this->logger->addError($msg, $this->addMinimalContext($context)); |
|
104 | } |
||
105 | |||
106 | /** |
||
107 | * @param $msg |
||
108 | * @param array $context |
||
109 | * @return bool |
||
110 | */ |
||
111 | 3 | public function warningLog($msg, array $context = []) |
|
115 | |||
116 | /** |
||
117 | * @param string $logger |
||
118 | * @param boolean $debug |
||
119 | * @param Config $config |
||
120 | */ |
||
121 | 2 | private function addPushLogger($logger, $debug, Config $config) |
|
137 | |||
138 | /** |
||
139 | * @param Config $config |
||
140 | * @param array $args |
||
141 | * @return array |
||
142 | * @throws exception\GeneratorException |
||
143 | */ |
||
144 | 2 | private function setup(Config $config, array $args = array()) |
|
159 | |||
160 | /** |
||
161 | * @param Config $config |
||
162 | * |
||
163 | * @return string |
||
164 | */ |
||
165 | 2 | private function setLoggerName(Config $config) |
|
172 | |||
173 | /** |
||
174 | * @param $logger |
||
175 | * |
||
176 | * @return mixed |
||
177 | */ |
||
178 | 2 | private function cleanLoggerName($logger) |
|
185 | |||
186 | /** |
||
187 | * @param Config $config |
||
188 | * @return string |
||
189 | * @throws exception\GeneratorException |
||
190 | */ |
||
191 | 2 | private function createLoggerPath(Config $config) |
|
199 | |||
200 | /** |
||
201 | * @param string $msg |
||
202 | * @param int $type |
||
203 | * @param array $context |
||
204 | */ |
||
205 | 23 | public static function log($msg, $type = LOG_DEBUG, array $context = []) |
|
229 | |||
230 | /** |
||
231 | * @param bool $debug |
||
232 | * @return StreamHandler |
||
233 | */ |
||
234 | 2 | private function addDefaultStreamHandler($debug = false) |
|
246 | |||
247 | /** |
||
248 | * @param array $context |
||
249 | * @return array |
||
250 | */ |
||
251 | 8 | private function addMinimalContext(array $context = []) |
|
257 | } |
||
258 |
The PSR-1: Basic Coding Standard recommends that a file should either introduce new symbols, that is classes, functions, constants or similar, or have side effects. Side effects are anything that executes logic, like for example printing output, changing ini settings or writing to a file.
The idea behind this recommendation is that merely auto-loading a class should not change the state of an application. It also promotes a cleaner style of programming and makes your code less prone to errors, because the logic is not spread out all over the place.
To learn more about the PSR-1, please see the PHP-FIG site on the PSR-1.