1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace GeminiLabs\Castor\Helpers; |
4
|
|
|
|
5
|
|
|
use ReflectionClass; |
6
|
|
|
|
7
|
|
|
class Log |
8
|
|
|
{ |
9
|
|
|
const EMERGENCY = 'emergency'; |
10
|
|
|
const ALERT = 'alert'; |
11
|
|
|
const CRITICAL = 'critical'; |
12
|
|
|
const ERROR = 'error'; |
13
|
|
|
const WARNING = 'warning'; |
14
|
|
|
const NOTICE = 'notice'; |
15
|
|
|
const INFO = 'info'; |
16
|
|
|
const DEBUG = 'debug'; |
17
|
|
|
|
18
|
|
|
protected $file; |
19
|
|
|
protected $log; |
20
|
|
|
|
21
|
|
|
public function __construct( $filename ) |
22
|
|
|
{ |
23
|
|
|
$this->file = $filename; |
24
|
|
|
$this->log = file_exists( $filename ) |
25
|
|
|
? file_get_contents( $filename ) |
26
|
|
|
: ''; |
27
|
|
|
} |
28
|
|
|
|
29
|
|
|
public function __toString() |
30
|
|
|
{ |
31
|
|
|
return $this->log; |
32
|
|
|
} |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* Action must be taken immediately. |
36
|
|
|
* Example: Entire website down, database unavailable, etc. This should |
37
|
|
|
* trigger the SMS alerts and wake you up. |
38
|
|
|
* |
39
|
|
|
* @param string $message |
40
|
|
|
* @param array $context |
41
|
|
|
* @return void |
42
|
|
|
*/ |
43
|
|
|
public function alert( $message, array $context = [] ) |
44
|
|
|
{ |
45
|
|
|
$this->log( static::ALERT, $message, $context ); |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* @return void |
50
|
|
|
*/ |
51
|
|
|
public function clear() |
52
|
|
|
{ |
53
|
|
|
$this->log = ''; |
54
|
|
|
file_put_contents( $this->file, $this->log ); |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* Critical conditions. |
59
|
|
|
* Example: Application component unavailable, unexpected exception. |
60
|
|
|
* |
61
|
|
|
* @param string $message |
62
|
|
|
* @param array $context |
63
|
|
|
* @return void |
64
|
|
|
*/ |
65
|
|
|
public function critical( $message, array $context = [] ) |
66
|
|
|
{ |
67
|
|
|
$this->log( static::CRITICAL, $message, $context ); |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* Detailed debug information. |
72
|
|
|
* |
73
|
|
|
* @param string $message |
74
|
|
|
* @param array $context |
75
|
|
|
* @return void |
76
|
|
|
*/ |
77
|
|
|
public function debug( $message, array $context = [] ) |
78
|
|
|
{ |
79
|
|
|
$this->log( static::DEBUG, $message, $context ); |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
/** |
83
|
|
|
* System is unusable. |
84
|
|
|
* |
85
|
|
|
* @param string $message |
86
|
|
|
* @param array $context |
87
|
|
|
* @return void |
88
|
|
|
*/ |
89
|
|
|
public function emergency( $message, array $context = [] ) |
90
|
|
|
{ |
91
|
|
|
$this->log( static::EMERGENCY, $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
|
|
|
* @return void |
101
|
|
|
*/ |
102
|
|
|
public function error( $message, array $context = [] ) |
103
|
|
|
{ |
104
|
|
|
$this->log( static::ERROR, $message, $context ); |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
/** |
108
|
|
|
* Interesting events. |
109
|
|
|
* Example: User logs in, SQL logs. |
110
|
|
|
* |
111
|
|
|
* @param string $message |
112
|
|
|
* @param array $context |
113
|
|
|
* @return void |
114
|
|
|
*/ |
115
|
|
|
public function info( $message, array $context = [] ) |
116
|
|
|
{ |
117
|
|
|
$this->log( static::INFO, $message, $context ); |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
/** |
121
|
|
|
* Normal but significant events. |
122
|
|
|
* |
123
|
|
|
* @param string $message |
124
|
|
|
* @param array $context |
125
|
|
|
* @return void |
126
|
|
|
*/ |
127
|
|
|
public function notice( $message, array $context = [] ) |
128
|
|
|
{ |
129
|
|
|
$this->log( static::NOTICE, $message, $context ); |
130
|
|
|
} |
131
|
|
|
|
132
|
|
|
/** |
133
|
|
|
* Exceptional occurrences that are not errors. |
134
|
|
|
* Example: Use of deprecated APIs, poor use of an API, undesirable things |
135
|
|
|
* that are not necessarily wrong. |
136
|
|
|
* |
137
|
|
|
* @param string $message |
138
|
|
|
* @param array $context |
139
|
|
|
* @return void |
140
|
|
|
*/ |
141
|
|
|
public function warning( $message, array $context = [] ) |
142
|
|
|
{ |
143
|
|
|
$this->log( static::WARNING, $message, $context ); |
144
|
|
|
} |
145
|
|
|
|
146
|
|
|
/** |
147
|
|
|
* @param string $message |
148
|
|
|
* @param array $context |
149
|
|
|
* @return array|string |
150
|
|
|
*/ |
151
|
|
|
protected function interpolate( $message, array $context = [] ) |
152
|
|
|
{ |
153
|
|
|
if( is_array( $message )) { |
154
|
|
|
return htmlspecialchars( print_r( $array, true ), ENT_QUOTES, 'UTF-8' ); |
|
|
|
|
155
|
|
|
} |
156
|
|
|
$replace = []; |
157
|
|
|
foreach( $context as $key => $val ) { |
158
|
|
|
if( is_object( $val ) && get_class( $val ) === 'DateTime' ) { |
159
|
|
|
$val = $val->format( 'Y-m-d H:i:s' ); |
160
|
|
|
} |
161
|
|
|
else if( is_object( $val ) || is_array( $val )) { |
162
|
|
|
$val = json_encode( $val ); |
163
|
|
|
} |
164
|
|
|
else if( is_resource( $val )) { |
165
|
|
|
$val = (string) $val; |
166
|
|
|
} |
167
|
|
|
$replace['{'.$key.'}'] = $val; |
168
|
|
|
} |
169
|
|
|
return strtr( $message, $replace ); |
170
|
|
|
} |
171
|
|
|
|
172
|
|
|
/** |
173
|
|
|
* @param mixed $level |
174
|
|
|
* @param string $message |
175
|
|
|
* @param array $context |
176
|
|
|
* @return void |
177
|
|
|
*/ |
178
|
|
|
protected function log( $level, $message, array $context = [] ) |
179
|
|
|
{ |
180
|
|
|
if( !defined( 'CASTOR_DEBUG' ) |
181
|
|
|
|| CASTOR_DEBUG !== true |
182
|
|
|
|| !in_array( $level, (new ReflectionClass( __NAMESPACE__.'\Log' ))->getConstants(), true ) |
183
|
|
|
)return; |
184
|
|
|
$date = get_date_from_gmt( gmdate('Y-m-d H:i:s') ); |
185
|
|
|
$level = strtoupper( $level ); |
186
|
|
|
$message = $this->interpolate( $message, $context ); |
187
|
|
|
$entry = "[$date] $level: $message" . PHP_EOL; |
188
|
|
|
file_put_contents( $this->file, $entry, FILE_APPEND|LOCK_EX ); |
189
|
|
|
} |
190
|
|
|
} |
191
|
|
|
|
This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.