Completed
Push — master ( 20c3c4...b6edec )
by David
11:30
created

Wordlift_Log_Service::trace()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 1
dl 0
loc 5
rs 9.4285
c 0
b 0
f 0
1
<?php
2
3
/**
4
 * The Log service.
5
 *
6
 * @since 1.0.0
7
 */
8
class Wordlift_Log_Service {
9
10
	const MESSAGE_TEMPLATE = '%-6s [%-40.40s] %s';
11
12
	const ERROR = 4;
13
	const WARN = 3;
14
	const INFO = 2;
15
	const DEBUG = 1;
16
	const TRACE = 0;
17
18
	/**
19
	 * The class related to the logs.
20
	 *
21
	 * @since  1.0.0
22
	 * @access private
23
	 * @var string $class_name The class related to the logs.
24
	 */
25
	private $class_name;
26
27
	/**
28
	 * The log levels for printing in log lines.
29
	 *
30
	 * @var array $levels An array of log levels.
31
	 */
32
	private static $levels = array(
33
		self::TRACE => 'TRACE',
34
		self::DEBUG => 'DEBUG',
35
		self::INFO  => 'INFO',
36
		self::WARN  => 'WARN',
37
		self::ERROR => 'ERROR',
38
	);
39
40
	/**
41
	 * A singleton instance for legacy logging.
42
	 *
43
	 * @since  3.10.0
44
	 * @access private
45
	 * @var \Wordlift_Log_Service $instance A singleton instance for legacy logging.
46
	 */
47
	private static $instance = null;
48
49
	/**
50
	 * Create an instance of the Log service.
51
	 * @since 1.0.0
52
	 *
53
	 * @param string $class_name The class related to the logs.
54
	 */
55
	public function __construct( $class_name ) {
56
57
		$this->class_name = $class_name;
58
59
	}
60
61
	/**
62
	 * Get the ROOT logger.
63
	 *
64
	 * @since 3.10.0
65
	 *
66
	 * @return \Wordlift_Log_Service A singleton instance for legacy logging.
67
	 */
68
	public static function get_instance() {
69
70
		return self::$instance ?: self::$instance = new Wordlift_Log_Service( 'ROOT' );
71
	}
72
73
74
	public static function get_logger( $class_name ) {
75
76
		return new Wordlift_Log_Service( $class_name );
77
78
	}
79
80
	/**
81
	 * Log a message.
82
	 *
83
	 * @since 1.0.0
84
	 *
85
	 * @param string $level   The log level.
86
	 * @param string $message The message to log.
87
	 */
88
	public function log( $level, $message ) {
89
90
		// Bail out if WordLift log level isn't defined, and WP debug is disabled.
91
		if ( ! defined( 'WL_LOG_LEVEL' ) && $level < self::INFO
92
			 && ( ! defined( 'WP_DEBUG' ) || false === WP_DEBUG ) ) {
93
			return;
94
		}
95
96
		// Bail out if the log message is below the minimum log level.
97
		if ( defined( 'WL_LOG_LEVEL' ) && $level < intval( WL_LOG_LEVEL ) ) {
98
			return;
99
		}
100
101
		// Bail out if there's a filter and we don't match it.
102
		if ( defined( 'WL_LOG_FILTER' ) && 1 !== preg_match( "/(^|,)$this->class_name($|,)/", WL_LOG_FILTER ) ) {
103
			return;
104
		}
105
106
		// Finally log the message.
107
		error_log( sprintf( self::MESSAGE_TEMPLATE, self::$levels[ $level ], $this->class_name, is_array( $message ) ? implode( ', ', $message ) : $message ) );
108
109
	}
110
111
	public function error( $message ) {
112
113
		$this->log( self::ERROR, $message );
114
115
	}
116
117
	public function warn( $message ) {
118
119
		$this->log( self::WARN, $message );
120
121
	}
122
123
	public function info( $message ) {
124
125
		$this->log( self::INFO, $message );
126
127
	}
128
129
	public function debug( $message ) {
130
131
		$this->log( self::DEBUG, $message );
132
133
	}
134
135
	public function trace( $message ) {
136
137
		$this->log( self::TRACE, $message );
138
139
	}
140
141
}
142