|
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
|
1 |
|
public function __construct() |
|
46
|
|
|
{ |
|
47
|
1 |
|
Hook::listen(__CLASS__); |
|
48
|
1 |
|
} |
|
49
|
|
|
|
|
50
|
|
|
/** |
|
51
|
|
|
* System is unusable. |
|
52
|
|
|
* |
|
53
|
|
|
* @param string $message |
|
54
|
|
|
* @param array $context |
|
55
|
|
|
* |
|
56
|
|
|
* @return void |
|
57
|
|
|
*/ |
|
58
|
|
|
public function emergency($message, array $context = []) |
|
59
|
|
|
{ |
|
60
|
|
|
$this->log('EMERGENCY', $message, $context); |
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
|
|
/** |
|
64
|
|
|
* Action must be taken immediately. |
|
65
|
|
|
* |
|
66
|
|
|
* Example: Entire website down, database unavailable, etc. This should |
|
67
|
|
|
* trigger the SMS alerts and wake you up. |
|
68
|
|
|
* |
|
69
|
|
|
* @param string $message |
|
70
|
|
|
* @param array $context |
|
71
|
|
|
* |
|
72
|
|
|
* @return void |
|
73
|
|
|
*/ |
|
74
|
|
|
public function alert($message, array $context = []) |
|
75
|
|
|
{ |
|
76
|
|
|
$this->log('ALERT', $message, $context); |
|
77
|
|
|
} |
|
78
|
|
|
|
|
79
|
|
|
/** |
|
80
|
|
|
* Critical conditions. |
|
81
|
|
|
* |
|
82
|
|
|
* Example: Application component unavailable, unexpected exception. |
|
83
|
|
|
* |
|
84
|
|
|
* @param string $message |
|
85
|
|
|
* @param array $context |
|
86
|
|
|
* |
|
87
|
|
|
* @return void |
|
88
|
|
|
*/ |
|
89
|
|
|
public function critical($message, array $context = []) |
|
90
|
|
|
{ |
|
91
|
|
|
$this->log('CRITICAL', $message, $context); |
|
92
|
|
|
} |
|
93
|
|
|
|
|
94
|
|
|
/** |
|
95
|
|
|
* Runtime errors that do not require immediate action but should typically |
|
96
|
|
|
* be logged and monitored. |
|
97
|
|
|
* |
|
98
|
|
|
* @param string $message |
|
99
|
|
|
* @param array $context |
|
100
|
|
|
* |
|
101
|
|
|
* @return void |
|
102
|
|
|
*/ |
|
103
|
|
|
public function error($message, array $context = []) |
|
104
|
|
|
{ |
|
105
|
|
|
$this->log('ERROR', $message, $context); |
|
106
|
|
|
} |
|
107
|
|
|
|
|
108
|
|
|
/** |
|
109
|
|
|
* Exceptional occurrences that are not errors. |
|
110
|
|
|
* |
|
111
|
|
|
* Example: Use of deprecated APIs, poor use of an API, undesirable things |
|
112
|
|
|
* that are not necessarily wrong. |
|
113
|
|
|
* |
|
114
|
|
|
* @param string $message |
|
115
|
|
|
* @param array $context |
|
116
|
|
|
* |
|
117
|
|
|
* @return void |
|
118
|
|
|
*/ |
|
119
|
|
|
public function warning($message, array $context = []) |
|
120
|
|
|
{ |
|
121
|
|
|
$this->log('WARNING', $message, $context); |
|
122
|
|
|
} |
|
123
|
|
|
|
|
124
|
|
|
/** |
|
125
|
|
|
* Normal but significant events. |
|
126
|
|
|
* |
|
127
|
|
|
* @param string $message |
|
128
|
|
|
* @param array $context |
|
129
|
|
|
* |
|
130
|
|
|
* @return void |
|
131
|
|
|
*/ |
|
132
|
|
|
public function notice($message, array $context = []) |
|
133
|
|
|
{ |
|
134
|
|
|
$this->log('NOTICE', $message, $context); |
|
135
|
|
|
} |
|
136
|
|
|
|
|
137
|
|
|
/** |
|
138
|
|
|
* Interesting events. |
|
139
|
|
|
* |
|
140
|
|
|
* Example: User logs in, SQL logs. |
|
141
|
|
|
* |
|
142
|
|
|
* @param string $message |
|
143
|
|
|
* @param array $context |
|
144
|
|
|
* |
|
145
|
|
|
* @return void |
|
146
|
|
|
*/ |
|
147
|
4 |
|
public function info($message, array $context = []) |
|
148
|
|
|
{ |
|
149
|
4 |
|
$this->log('INFO', $message, $context); |
|
150
|
4 |
|
} |
|
151
|
|
|
|
|
152
|
|
|
/** |
|
153
|
|
|
* Detailed debug information. |
|
154
|
|
|
* |
|
155
|
|
|
* @param string $message |
|
156
|
|
|
* @param array $context |
|
157
|
|
|
* |
|
158
|
|
|
* @return void |
|
159
|
|
|
*/ |
|
160
|
|
|
public function debug($message, array $context = []) |
|
161
|
|
|
{ |
|
162
|
|
|
$this->log('DEBUG', $message, $context); |
|
163
|
|
|
} |
|
164
|
|
|
|
|
165
|
|
|
/** |
|
166
|
|
|
* Shortcut for app information. |
|
167
|
|
|
* |
|
168
|
|
|
* @param string $message |
|
169
|
|
|
* @param array $context |
|
170
|
|
|
* |
|
171
|
|
|
* @return void |
|
172
|
|
|
*/ |
|
173
|
|
|
public function app($message, array $context = []) |
|
174
|
|
|
{ |
|
175
|
|
|
$this->log('APP', $message, $context); |
|
176
|
|
|
} |
|
177
|
|
|
|
|
178
|
|
|
/** |
|
179
|
|
|
* Logs with an arbitrary level. |
|
180
|
|
|
* |
|
181
|
|
|
* @param string $level |
|
182
|
|
|
* @param string $message |
|
183
|
|
|
* @param array $context |
|
184
|
|
|
* @return void |
|
185
|
|
|
*/ |
|
186
|
4 |
|
public function log($level, $message, array $context = []) |
|
187
|
|
|
{ |
|
188
|
4 |
|
if (!Container::get('config')->get('app_debug') && $level != 'APP') { |
|
189
|
4 |
|
return; |
|
190
|
|
|
} |
|
191
|
|
|
|
|
192
|
|
|
// build a replacement array with braces around the context keys |
|
193
|
|
|
$replace = []; |
|
194
|
|
|
foreach ($context as $key => $val) { |
|
195
|
|
|
// check that the value can be casted to string |
|
196
|
|
|
if (!is_array($val) && (!is_object($val) || method_exists($val, '__toString'))) { |
|
197
|
|
|
$replace['{' . $key . '}'] = $val; |
|
198
|
|
|
} |
|
199
|
|
|
} |
|
200
|
|
|
|
|
201
|
|
|
$message = strtr($message, $replace); |
|
202
|
|
|
$message = date('[Y-m-d H:i:s]') . "\r\n" . "[{$level}]" . "\r\n" . $message . "\r\n\r\n"; |
|
203
|
|
|
$logPath = Container::get('config')->get('app_full_path') . '/logs'; |
|
204
|
|
|
if (!file_exists($logPath)) { |
|
205
|
|
|
Helper::mkdirs($logPath); |
|
206
|
|
|
} |
|
207
|
|
|
|
|
208
|
|
|
if (file_exists($logPath)) { |
|
209
|
|
|
file_put_contents($logPath . '/' . date('Ymd') . '.log', $message, FILE_APPEND); |
|
210
|
|
|
} |
|
211
|
|
|
} |
|
212
|
|
|
} |
|
213
|
|
|
|