Log   A
last analyzed

Complexity

Total Complexity 24

Size/Duplication

Total Lines 200
Duplicated Lines 0 %

Importance

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

16 Methods

Rating   Name   Duplication   Size   Complexity  
A warning() 0 3 1
A log() 0 12 2
A logger() 0 3 1
A notice() 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 debug() 0 3 1
A __toString() 0 3 1
A emergency() 0 3 1
A clear() 0 4 1
A normalizeValue() 0 12 4
A interpolate() 0 10 3
A isObjectOrArray() 0 3 2
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
	 * @return static
119
	 */
120
	public function log( $level, $message, array $context = [] )
121
	{
122
		$constants = (new ReflectionClass( __NAMESPACE__.'\Log' ))->getConstants();
123
		$constants = (array)apply_filters( Application::ID.'/log-levels', $constants );
124
		if( in_array( $level, $constants, true )) {
125
			$date = get_date_from_gmt( gmdate('Y-m-d H:i:s') );
126
			$level = strtoupper( $level );
127
			$message = $this->interpolate( $message, $context );
128
			$entry = "[$date] $level: $message" . PHP_EOL;
129
			file_put_contents( $this->file, $entry, FILE_APPEND|LOCK_EX );
130
		}
131
		return $this;
132
	}
133
134
	/**
135
	 * @return static
136
	 */
137
	public function logger()
138
	{
139
		return $this;
140
	}
141
142
	/**
143
	 * Normal but significant events.
144
	 * @param string $message
145
	 * @param array $context
146
	 * @return static
147
	 */
148
	public function notice( $message, array $context = [] )
149
	{
150
		return $this->log( static::NOTICE, $message, $context );
151
	}
152
153
	/**
154
	 * Exceptional occurrences that are not errors.
155
	 * Example: Use of deprecated APIs, poor use of an API, undesirable things
156
	 * that are not necessarily wrong.
157
	 * @param string $message
158
	 * @param array $context
159
	 * @return static
160
	 */
161
	public function warning( $message, array $context = [] )
162
	{
163
		return $this->log( static::WARNING, $message, $context );
164
	}
165
166
	/**
167
	 * @param mixed $message
168
	 * @param array $context
169
	 * @return array|string
170
	 */
171
	protected function interpolate( $message, array $context = [] )
172
	{
173
		if( $this->isObjectOrArray( $message )) {
174
			return print_r( $message, true );
175
		}
176
		$replace = [];
177
		foreach( $context as $key => $value ) {
178
			$replace['{'.$key.'}'] = $this->normalizeValue( $value );
179
		}
180
		return strtr( $message, $replace );
181
	}
182
183
	/**
184
	 * @param mixed $value
185
	 * @return bool
186
	 */
187
	protected function isObjectOrArray( $value )
188
	{
189
		return is_object( $value ) || is_array( $value );
190
	}
191
192
	/**
193
	 * @param mixed $value
194
	 * @return string
195
	 */
196
	protected function normalizeValue( $value )
197
	{
198
		if( $value instanceof DateTime ) {
199
			$value = $value->format( 'Y-m-d H:i:s' );
200
		}
201
		else if( $this->isObjectOrArray( $value )) {
202
			$value = json_encode( $value );
203
		}
204
		else if( is_resource( $value )) {
205
			$value = (string)$value;
206
		}
207
		return $value;
208
	}
209
}
210