-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathclass-logger.php
114 lines (98 loc) · 2.78 KB
/
class-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
<?php
/**
* Logger
*
* @package Logging
*/
namespace MLA\Commons\Plugin\Logging;
if ( ! class_exists( '\MLA\Commons\Plugin\Logging\Logger' ) ) {
/**
* Provide a logger that can be used for debugging and change logging. This
* is a fallback class that is only used if the Monolog-based logger has not
* loaded (e.g., in unit tests).
*
* @package Logging
* @subpackage Logger
* @class Logger
*/
class Logger {
/**
* Log slug/prefix.
*
* @var string
*/
private $slug = '';
/**
* Suppress logging.
*
* @var bool
*/
private $suppress = false;
/**
* Log to PHP error_log.
*
* @param string $message Message to log.
* @param array $context Array of extra data to append to log entry.
* @param bool $is_debug True if message is a debug message.
*/
private function log( $message, $context, $is_debug = true ) {
if ( $this->suppress || ( $is_debug && true !== WP_DEBUG ) ) {
return false;
}
if ( ! empty( $context ) ) {
$message .= ' *** ' . serialize( $context );
}
error_log( $this->slug . $message );
}
/**
* Create log. Since Monolog isn't available, we just send what we have to
* the PHP error log and prefix it with the slug. If null is provided as
* slug, we dev-null everything.
*
* @param string $slug Log slug/prefix.
*/
public function createLog( $slug = null ) { // @codingStandardsIgnoreLine camelCase
if ( null === $slug ) {
$this->suppress = true;
} else if ( is_string( $slug ) && strlen( $slug ) ) {
$this->slug = '[' . $slug . '] ';
}
}
/**
* Add debug message.
*
* @param string $message Message to log.
* @param array $context Array of extra data to append to log entry.
*/
public function addDebug( $message, array $context = array() ) { // @codingStandardsIgnoreLine camelCase
$this->log( $message, $context );
}
/**
* Add error message.
*
* @param string $message Message to log.
* @param array $context Array of extra data to append to log entry.
*/
public function addError( $message, array $context = array() ) { // @codingStandardsIgnoreLine camelCase
$this->log( $message, $context, false );
}
/**
* Add info message.
*
* @param string $message Message to log.
* @param array $context Array of extra data to append to log entry.
*/
public function addInfo( $message, array $context = array() ) { // @codingStandardsIgnoreLine camelCase
$this->log( $message, $context );
}
/**
* Add warning message.
*
* @param string $message Message to log.
* @param array $context Array of extra data to append to log entry.
*/
public function addWarning( $message, array $context = array() ) { // @codingStandardsIgnoreLine camelCase
$this->log( $message, $context );
}
}
}