Passed
Push — master ( da62c2...b88588 )
by Paul
03:15
created

Log   A

Complexity

Total Complexity 24

Size/Duplication

Total Lines 201
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
dl 0
loc 201
rs 10
c 0
b 0
f 0
wmc 24

16 Methods

Rating   Name   Duplication   Size   Complexity  
A warning() 0 3 1
A error() 0 3 1
A __construct() 0 6 2
A info() 0 3 1
A alert() 0 3 1
A critical() 0 3 1
A normalizeValue() 0 12 4
A debug() 0 3 1
A interpolate() 0 10 3
A log() 0 12 2
A __toString() 0 3 1
A isObjectOrArray() 0 3 2
A emergency() 0 3 1
A clear() 0 4 1
A logger() 0 3 1
A notice() 0 3 1
1
<?php
2
3
namespace GeminiLabs\Logger;
4
5
use DateTime;
6
use ReflectionClass;
7
8
class Log
9
{
10
	const EMERGENCY = 'emergency';
11
	const ALERT = 'alert';
12
	const CRITICAL  = 'critical';
13
	const ERROR = 'error';
14
	const WARNING = 'warning';
15
	const NOTICE = 'notice';
16
	const INFO = 'info';
17
	const DEBUG = 'debug';
18
19
	protected $file;
20
	protected $log;
21
22
	public function __construct( $filename )
23
	{
24
		$this->file = $filename;
25
		$this->log = file_exists( $filename )
26
			? file_get_contents( $filename )
27
			: '';
28
	}
29
30
	public function __toString()
31
	{
32
		return $this->log;
33
	}
34
35
	/**
36
	 * Action must be taken immediately.
37
	 * Example: Entire website down, database unavailable, etc. This should
38
	 * trigger the SMS alerts and wake you up.
39
	 * @param string $message
40
	 * @param array $context
41
	 * @return static
42
	 */
43
	public function alert( $message, array $context = [] )
44
	{
45
		return $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
	 * @param string $message
61
	 * @param array $context
62
	 * @return static
63
	 */
64
	public function critical( $message, array $context = [] )
65
	{
66
		return $this->log( static::CRITICAL, $message, $context );
67
	}
68
69
	/**
70
	 * Detailed debug information.
71
	 * @param string $message
72
	 * @param array $context
73
	 * @return static
74
	 */
75
	public function debug( $message, array $context = [] )
76
	{
77
		return $this->log( static::DEBUG, $message, $context );
78
	}
79
80
	/**
81
	 * System is unusable.
82
	 * @param string $message
83
	 * @param array $context
84
	 * @return static
85
	 */
86
	public function emergency( $message, array $context = [] )
87
	{
88
		return $this->log( static::EMERGENCY, $message, $context );
89
	}
90
91
	/**
92
	 * Runtime errors that do not require immediate action but should typically
93
	 * be logged and monitored.
94
	 * @param string $message
95
	 * @param array $context
96
	 * @return static
97
	 */
98
	public function error( $message, array $context = [] )
99
	{
100
		return $this->log( static::ERROR, $message, $context );
101
	}
102
103
	/**
104
	 * Interesting events.
105
	 * Example: User logs in, SQL logs.
106
	 * @param string $message
107
	 * @param array $context
108
	 * @return static
109
	 */
110
	public function info( $message, array $context = [] )
111
	{
112
		return $this->log( static::INFO, $message, $context );
113
	}
114
115
	/**
116
	 * @param mixed $level
117
	 * @param string $message
118
	 * @param array $context
119
	 * @return static
120
	 */
121
	public function log( $level, $message, array $context = [] )
122
	{
123
		$constants = (new ReflectionClass( __NAMESPACE__.'\Log' ))->getConstants();
124
		$constants = (array)apply_filters( Application::ID.'/log-levels', $constants );
125
		if( in_array( $level, $constants, true )) {
126
			$date = get_date_from_gmt( gmdate('Y-m-d H:i:s') );
127
			$level = strtoupper( $level );
128
			$message = $this->interpolate( $message, $context );
129
			$entry = "[$date] $level: $message" . PHP_EOL;
130
			file_put_contents( $this->file, $entry, FILE_APPEND|LOCK_EX );
131
		}
132
		return $this;
133
	}
134
135
	/**
136
	 * @return static
137
	 */
138
	public function logger()
139
	{
140
		return $this;
141
	}
142
143
	/**
144
	 * Normal but significant events.
145
	 * @param string $message
146
	 * @param array $context
147
	 * @return static
148
	 */
149
	public function notice( $message, array $context = [] )
150
	{
151
		return $this->log( static::NOTICE, $message, $context );
152
	}
153
154
	/**
155
	 * Exceptional occurrences that are not errors.
156
	 * Example: Use of deprecated APIs, poor use of an API, undesirable things
157
	 * that are not necessarily wrong.
158
	 * @param string $message
159
	 * @param array $context
160
	 * @return static
161
	 */
162
	public function warning( $message, array $context = [] )
163
	{
164
		return $this->log( static::WARNING, $message, $context );
165
	}
166
167
	/**
168
	 * @param mixed $message
169
	 * @param array $context
170
	 * @return array|string
171
	 */
172
	protected function interpolate( $message, array $context = [] )
173
	{
174
		if( $this->isObjectOrArray( $value )) {
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $value seems to be never defined.
Loading history...
175
			return print_r( $message, true );
176
		}
177
		$replace = [];
178
		foreach( $context as $key => $value ) {
179
			$replace['{'.$key.'}'] = $this->normalizeValue( $value );
180
		}
181
		return strtr( $message, $replace );
182
	}
183
184
	/**
185
	 * @param mixed $value
186
	 * @return bool
187
	 */
188
	protected function isObjectOrArray( $value )
189
	{
190
		return is_object( $value ) || is_array( $value );
191
	}
192
193
	/**
194
	 * @param mixed $value
195
	 * @return string
196
	 */
197
	protected function normalizeValue( $value )
198
	{
199
		if( $value instanceof DateTime ) {
200
			$value = $value->format( 'Y-m-d H:i:s' );
201
		}
202
		else if( $this->isObjectOrArray( $value )) {
203
			$value = json_encode( $value );
204
		}
205
		else if( is_resource( $value )) {
206
			$value = (string)$value;
207
		}
208
		return $value;
209
	}
210
}
211