1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Kotori.php |
4
|
|
|
* |
5
|
|
|
* A Tiny Model-View-Controller PHP Framework |
6
|
|
|
* |
7
|
|
|
* This content is released under the Apache 2 License |
8
|
|
|
* |
9
|
|
|
* Copyright (c) 2015-2017 Kotori Technology. All rights reserved. |
10
|
|
|
* |
11
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License"); |
12
|
|
|
* you may not use this file except in compliance with the License. |
13
|
|
|
* You may obtain a copy of the License at |
14
|
|
|
* |
15
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0 |
16
|
|
|
* |
17
|
|
|
* Unless required by applicable law or agreed to in writing, software |
18
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS, |
19
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
20
|
|
|
* See the License for the specific language governing permissions and |
21
|
|
|
* limitations under the License. |
22
|
|
|
*/ |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* Logging Class |
26
|
|
|
* |
27
|
|
|
* @package Kotori |
28
|
|
|
* @subpackage Debug |
29
|
|
|
* @author Kokororin |
30
|
|
|
* @link https://kotori.love |
31
|
|
|
*/ |
32
|
|
|
namespace Kotori\Debug; |
33
|
|
|
|
34
|
|
|
use Kotori\Core\Container; |
35
|
|
|
use Kotori\Core\Helper; |
36
|
|
|
use Psr\Log\LoggerInterface; |
37
|
|
|
|
38
|
|
|
class Logger implements LoggerInterface |
39
|
|
|
{ |
40
|
|
|
/** |
41
|
|
|
* Class constructor |
42
|
|
|
* |
43
|
|
|
* Initialize Logger. |
44
|
|
|
* |
45
|
|
|
* @return void |
|
|
|
|
46
|
|
|
*/ |
47
|
1 |
|
public function __construct() |
48
|
|
|
{ |
49
|
1 |
|
Hook::listen(__CLASS__); |
50
|
1 |
|
} |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* System is unusable. |
54
|
|
|
* |
55
|
|
|
* @param string $message |
56
|
|
|
* @param array $context |
57
|
|
|
* |
58
|
|
|
* @return void |
59
|
|
|
*/ |
60
|
|
|
public function emergency($message, array $context = []) |
61
|
|
|
{ |
62
|
|
|
$this->log('EMERGENCY', $message, $context); |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* Action must be taken immediately. |
67
|
|
|
* |
68
|
|
|
* Example: Entire website down, database unavailable, etc. This should |
69
|
|
|
* trigger the SMS alerts and wake you up. |
70
|
|
|
* |
71
|
|
|
* @param string $message |
72
|
|
|
* @param array $context |
73
|
|
|
* |
74
|
|
|
* @return void |
75
|
|
|
*/ |
76
|
|
|
public function alert($message, array $context = []) |
77
|
|
|
{ |
78
|
|
|
$this->log('ALERT', $message, $context); |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
/** |
82
|
|
|
* Critical conditions. |
83
|
|
|
* |
84
|
|
|
* Example: Application component unavailable, unexpected exception. |
85
|
|
|
* |
86
|
|
|
* @param string $message |
87
|
|
|
* @param array $context |
88
|
|
|
* |
89
|
|
|
* @return void |
90
|
|
|
*/ |
91
|
|
|
public function critical($message, array $context = []) |
92
|
|
|
{ |
93
|
|
|
$this->log('CRITICAL', $message, $context); |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
/** |
97
|
|
|
* Runtime errors that do not require immediate action but should typically |
98
|
|
|
* be logged and monitored. |
99
|
|
|
* |
100
|
|
|
* @param string $message |
101
|
|
|
* @param array $context |
102
|
|
|
* |
103
|
|
|
* @return void |
104
|
|
|
*/ |
105
|
|
|
public function error($message, array $context = []) |
106
|
|
|
{ |
107
|
|
|
$this->log('ERROR', $message, $context); |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
/** |
111
|
|
|
* Exceptional occurrences that are not errors. |
112
|
|
|
* |
113
|
|
|
* Example: Use of deprecated APIs, poor use of an API, undesirable things |
114
|
|
|
* that are not necessarily wrong. |
115
|
|
|
* |
116
|
|
|
* @param string $message |
117
|
|
|
* @param array $context |
118
|
|
|
* |
119
|
|
|
* @return void |
120
|
|
|
*/ |
121
|
|
|
public function warning($message, array $context = []) |
122
|
|
|
{ |
123
|
|
|
$this->log('WARNING', $message, $context); |
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
/** |
127
|
|
|
* Normal but significant events. |
128
|
|
|
* |
129
|
|
|
* @param string $message |
130
|
|
|
* @param array $context |
131
|
|
|
* |
132
|
|
|
* @return void |
133
|
|
|
*/ |
134
|
|
|
public function notice($message, array $context = []) |
135
|
|
|
{ |
136
|
|
|
$this->log('NOTICE', $message, $context); |
137
|
|
|
} |
138
|
|
|
|
139
|
|
|
/** |
140
|
|
|
* Interesting events. |
141
|
|
|
* |
142
|
|
|
* Example: User logs in, SQL logs. |
143
|
|
|
* |
144
|
|
|
* @param string $message |
145
|
|
|
* @param array $context |
146
|
|
|
* |
147
|
|
|
* @return void |
148
|
|
|
*/ |
149
|
4 |
|
public function info($message, array $context = []) |
150
|
|
|
{ |
151
|
4 |
|
$this->log('INFO', $message, $context); |
152
|
4 |
|
} |
153
|
|
|
|
154
|
|
|
/** |
155
|
|
|
* Detailed debug information. |
156
|
|
|
* |
157
|
|
|
* @param string $message |
158
|
|
|
* @param array $context |
159
|
|
|
* |
160
|
|
|
* @return void |
161
|
|
|
*/ |
162
|
|
|
public function debug($message, array $context = []) |
163
|
|
|
{ |
164
|
|
|
$this->log('DEBUG', $message, $context); |
165
|
|
|
} |
166
|
|
|
|
167
|
|
|
/** |
168
|
|
|
* Shortcut for app information. |
169
|
|
|
* |
170
|
|
|
* @param string $message |
171
|
|
|
* @param array $context |
172
|
|
|
* |
173
|
|
|
* @return void |
174
|
|
|
*/ |
175
|
|
|
public function app($message, array $context = []) |
176
|
|
|
{ |
177
|
|
|
$this->log('APP', $message, $context); |
178
|
|
|
} |
179
|
|
|
|
180
|
|
|
/** |
181
|
|
|
* Logs with an arbitrary level. |
182
|
|
|
* |
183
|
|
|
* @param mixed $level |
184
|
|
|
* @param string $message |
185
|
|
|
* @param array $context |
186
|
|
|
* @return void |
187
|
|
|
*/ |
188
|
4 |
|
public function log($level, $message, array $context = []) |
189
|
|
|
{ |
190
|
4 |
|
if (!Container::get('config')->get('app_debug') && $level != 'APP') { |
191
|
4 |
|
return; |
192
|
|
|
} |
193
|
|
|
|
194
|
|
|
// build a replacement array with braces around the context keys |
195
|
|
|
$replace = []; |
196
|
|
|
foreach ($context as $key => $val) { |
197
|
|
|
// check that the value can be casted to string |
198
|
|
|
if (!is_array($val) && (!is_object($val) || method_exists($val, '__toString'))) { |
199
|
|
|
$replace['{' . $key . '}'] = $val; |
200
|
|
|
} |
201
|
|
|
} |
202
|
|
|
|
203
|
|
|
$message = strtr($message, $replace); |
204
|
|
|
$message = date('[Y-m-d H:i:s]') . "\r\n" . "[{$level}]" . "\r\n" . $message . "\r\n\r\n"; |
205
|
|
|
$logPath = Container::get('config')->get('app_full_path') . '/logs'; |
206
|
|
|
if (!file_exists($logPath)) { |
207
|
|
|
Helper::mkdirs($logPath); |
208
|
|
|
} |
209
|
|
|
|
210
|
|
|
if (file_exists($logPath)) { |
211
|
|
|
file_put_contents($logPath . '/' . date('Ymd') . '.log', $message, FILE_APPEND); |
212
|
|
|
} |
213
|
|
|
} |
214
|
|
|
} |
215
|
|
|
|
Adding a
@return
annotation to a constructor is not recommended, since a constructor does not have a meaningful return value.Please refer to the PHP core documentation on constructors.