forked from ThemeFuse/Brizy
-
Notifications
You must be signed in to change notification settings - Fork 0
/
logger.php
218 lines (190 loc) · 4.51 KB
/
logger.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
<?php
class Brizy_Logger {
const EMERGENCY_LOG = 'EMERGENCY';
const ALERT_LOG = 'ALERT';
const CRITICAL_LOG = 'CRITICAL';
const ERROR_LOG = 'ERROR';
const WARNING_LOG = 'WARNING';
const NOTICE_LOG = 'NOTICE';
const INFO_LOG = 'INFO';
const DEBUG_LOG = 'DEBUG';
/**
* @var self
*/
static protected $instance = null;
/**
* Get logger instance.
*/
static public function instance() {
if ( self::$instance ) {
return self::$instance;
}
return self::$instance = new self();
}
/**
* Create logger table.
*/
static public function install() {
global $wpdb;
$create_table_query = "
CREATE TABLE IF NOT EXISTS `{$wpdb->prefix}brizy_logs` (
`id` bigint PRIMARY KEY AUTO_INCREMENT,
`type` text NOT NULL,
`message` text NOT NULL,
`context` text NOT NULL,
`session_id` text NOT NULL,
`date` datetime NOT NULL
) DEFAULT CHARSET=utf8;";
require_once( ABSPATH . 'wp-admin/includes/upgrade.php' );
dbDelta( $create_table_query );
self::clean();
}
/**
* Create logger table.
*/
static public function clean() {
global $wpdb;
$wpdb->query( "TRUNCATE `{$wpdb->prefix}brizy_logs`" );
}
/**
* @param $type
* @param $message
* @param array $context
*/
private function write_log( $type, $message, $context = array() ) {
global $wpdb;
if ( ! BRIZY_LOG ) {
return;
}
$wpdb->insert( "{$wpdb->prefix}brizy_logs", array(
'type' => $type,
'message' => $message,
'context' => serialize( $context ),
'session_id' => session_id(),
'date' => current_time( 'mysql', 1 )
), array(
'%s',
'%s',
'%s',
'%s',
'%s'
) );
}
/**
* System is unusable.
*
* @param string $message
* @param array $context
*
* @return void
*/
public function emergency( $message, array $context = array() ) {
$this->log( self::EMERGENCY_LOG, $message, $context );
}
/**
* Action must be taken immediately.
*
* Example: Entire website down, database unavailable, etc. This should
* trigger the SMS alerts and wake you up.
*
* @param string $message
* @param array $context
*
* @return void
*/
public function alert( $message, array $context = array() ) {
$this->log( self::ALERT_LOG, $message, $context );
}
/**
* Critical conditions.
*
* Example: Application component unavailable, unexpected exception.
*
* @param string $message
* @param array $contextgt
*
* @return void
*/
public function critical( $message, array $context = array() ) {
$this->log( self::CRITICAL_LOG, $message, $context );
}
/**
* Runtime errors that do not require immediate action but should typically
* be logged and monitored.
*
* @param string $message
* @param array $context
*
* @return void
*/
public function error( $message, array $context = array() ) {
$this->log( self::ERROR_LOG, $message, $context );
}
/**
* Exceptional occurrences that are not errors.
*
* Example: Use of deprecated APIs, poor use of an API, undesirable things
* that are not necessarily wrong.
*
* @param string $message
* @param array $context
*
* @return void
*/
public function warning( $message, array $context = array() ) {
$this->log( self::WARNING_LOG, $message, $context );
}
/**
* Normal but significant events.
*
* @param string $message
* @param array $context
*
* @return void
*/
public function notice( $message, array $context = array() ) {
$this->log( self::NOTICE_LOG, $message, $context );
}
/**
* Interesting events.
*
* Example: User logs in, SQL logs.
*
* @param string $message
* @param array $context
*
* @return void
*/
public function info( $message, array $context = array() ) {
$this->log( self::INFO_LOG, $message, $context );
}
/**
* Detailed debug information.
*
* @param string $message
* @param array $context
*
* @return void
*/
public function debug( $message, array $context = array() ) {
$this->log( self::DEBUG_LOG, $message, $context );
}
/**
* Logs with an arbitrary level.
*
* @param mixed $level
* @param string $message
* @param array $context
*
* @return void
*/
public function log( $level, $message, array $context = array() ) {
$this->write_log( $level, $message, $context );
}
/**
* @param Exception $exception
*/
public function exception( \Exception $exception ) {
$this->log( self::ERROR_LOG, $exception->getMessage(), array( $exception->getTraceAsString() ) );
}
}