logger.php
4.28 KB
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
<?php
if ( ! class_exists( 'blcLogger' ) ) :
define( 'BLC_LEVEL_DEBUG', 0 );
define( 'BLC_LEVEL_INFO', 1 );
define( 'BLC_LEVEL_WARNING', 2 );
define( 'BLC_LEVEL_ERROR', 3 );
/**
* Base class for loggers. Doesn't actually log anything anywhere.
*
* @package Broken Link Checker
* @author Janis Elsts
*/
class blcLogger {
protected $log_level = BLC_LEVEL_DEBUG;
function __construct( $param = '' ) {
}
function blcLogger( $param = '' ) {
$this->__construct( $param );
}
function log( $message, $object = null, $level = BLC_LEVEL_INFO ) {
}
function debug( $message, $object = null ) {
$this->log( $message, $object, BLC_LEVEL_DEBUG );
}
function info( $message, $object = null ) {
$this->log( $message, $object, BLC_LEVEL_INFO );
}
function warn( $message, $object = null ) {
$this->log( $message, $object, BLC_LEVEL_WARNING );
}
function error( $message, $object = null ) {
$this->log( $message, $object, BLC_LEVEL_ERROR );
}
function get_messages( $min_level = BLC_LEVEL_DEBUG ) {
return array();
}
function get_log( $min_level = BLC_LEVEL_DEBUG ) {
return array();
}
function clear() {
}
public function set_log_level( $level ) {
$this->log_level = $level;
}
}
/**
* A basic logger that uses WP options for permanent storage.
*
* Log entries are initially stored in memory and need to explicitly
* flushed to the database by calling blcCachedOptionLogger::save().
*
* @package Broken Link Checker
* @author Janis Elsts
*/
class blcCachedOptionLogger extends blcLogger {
var $option_name = '';
var $log;
var $filter_level = BLC_LEVEL_DEBUG;
function __construct( $option_name = '' ) {
$this->option_name = $option_name;
$oldLog = get_option( $this->option_name );
if ( is_array( $oldLog ) && ! empty( $oldLog ) ) {
$this->log = $oldLog;
} else {
$this->log = array();
}
}
function log( $message, $object = null, $level = BLC_LEVEL_DEBUG ) {
$new_entry = array( $level, $message, $object );
array_push( $this->log, $new_entry );
}
function get_log( $min_level = BLC_LEVEL_DEBUG ) {
$this->filter_level = $min_level;
return array_filter( $this->log, array( $this, '_filter_log' ) );
}
function _filter_log( $entry ) {
return ( $entry[0] >= $this->filter_level );
}
function get_messages( $min_level = BLC_LEVEL_DEBUG ) {
$messages = $this->get_log( $min_level );
return array_map( array( $this, '_get_log_message' ), $messages );
}
function _get_log_message( $entry ) {
return $entry[1];
}
function clear() {
$this->log = array();
delete_option( $this->option_name );
}
function save() {
update_option( $this->option_name, $this->log );
}
}
/**
* A dummy logger that doesn't log anything.
*/
class blcDummyLogger extends blcLogger { }
/**
* A basic logger that logs messages to a file.
*/
class blcFileLogger extends blcLogger {
protected $fileName; //phpcs:ignore WordPress.NamingConventions.ValidVariableName.PropertyNotSnakeCase
public function __construct( $fileName = '' ) {
$this->fileName = $fileName;
}
function log( $message, $object = null, $level = BLC_LEVEL_INFO ) {
if ( $level < $this->log_level ) {
return;
}
$line = sprintf(
'[%1$s] %2$s %3$s',
date( 'Y-m-d H:i:s P' ), //phpcs:ignore WordPress.DateTime.RestrictedFunctions.date_date
$this->get_level_string( $level ),
$message
);
if ( isset( $object ) ) {
$line .= ' ' . var_export( $object, true );
}
$line .= "\n";
error_log( $line, 3, $this->fileName );
}
function get_messages( $min_level = BLC_LEVEL_DEBUG ) {
return array( __CLASS__ . ':get_messages() is not implemented' );
}
function get_log( $min_level = BLC_LEVEL_DEBUG ) {
return array( __CLASS__ . ':get_log() is not implemented' );
}
public function clear() {
if ( is_file( $this->fileName ) && is_writable( $this->fileName ) ) {
$handle = fopen( $this->fileName, 'w' );
fclose( $handle );
}
}
protected function get_level_string( $level ) {
switch ( $level ) {
case BLC_LEVEL_DEBUG:
return 'DEBUG:';
case BLC_LEVEL_ERROR:
return 'ERROR:';
case BLC_LEVEL_WARNING:
return 'WARN:';
case BLC_LEVEL_INFO:
return 'INFO:';
}
return 'LOG:';
}
}
endif;