Passed
Branch master (5c167b)
by smiley
02:26
created

LogTrait   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 140
Duplicated Lines 0 %

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
wmc 11
dl 0
loc 140
ccs 0
cts 30
cp 0
rs 10
c 0
b 0
f 0

10 Methods

Rating   Name   Duplication   Size   Complexity  
A info() 0 2 1
A error() 0 2 1
A log() 0 4 2
A alert() 0 2 1
A notice() 0 2 1
A critical() 0 2 1
A emergency() 0 2 1
A debug() 0 2 1
A setLogger() 0 2 1
A warning() 0 2 1
1
<?php
2
/**
3
 * Trait LogTrait
4
 *
5
 * @filesource   LogTrait.php
6
 * @created      08.01.2018
7
 * @package      chillerlan\Logger
8
 * @author       Smiley <[email protected]>
9
 * @copyright    2018 Smiley
10
 * @license      MIT
11
 */
12
13
namespace chillerlan\Logger;
14
15
use Psr\Log\{LoggerInterface, LogLevel};
16
17
/**
18
 *
19
 * @implements \Psr\Log\LoggerAwareInterface
20
 * @implements \Psr\Log\LoggerInterface
21
 */
22
trait LogTrait{
23
24
	/**
25
	 * @var \Psr\Log\LoggerInterface
26
	 */
27
	protected $log;
28
29
	/**
30
	 * Sets a logger.
31
	 *
32
	 * @param \Psr\Log\LoggerInterface $logger
33
	 *
34
	 * @return void
35
	 */
36
	public function setLogger(LoggerInterface $logger){
37
		$this->log = $logger;
38
	}
39
40
	/**
41
	 * Logs with an arbitrary level.
42
	 *
43
	 * @param mixed  $level
44
	 * @param string $message
45
	 * @param array  $context
46
	 *
47
	 * @return void
48
	 */
49
	protected function log($level, $message, array $context = null){
50
51
		if($this->log instanceof LoggerInterface){
0 ignored issues
show
introduced by
The condition $this->log instanceof Psr\Log\LoggerInterface can never be false since $this->log is always a sub-type of Psr\Log\LoggerInterface.
Loading history...
52
			$this->log->log($level, $message, $context ?? []);
53
		}
54
55
	}
56
57
	/**
58
	 * System is unusable.
59
	 *
60
	 * @param string $message
61
	 * @param array  $context
62
	 *
63
	 * @return void
64
	 */
65
	protected function emergency($message, array $context = null){
66
		$this->log(LogLevel::EMERGENCY, $message, $context);
67
	}
68
69
	/**
70
	 * Action must be taken immediately.
71
	 *
72
	 * Example: Entire website down, database unavailable, etc. This should
73
	 * trigger the SMS alerts and wake you up.
74
	 *
75
	 * @param string $message
76
	 * @param array  $context
77
	 *
78
	 * @return void
79
	 */
80
	protected function alert($message, array $context = null){
81
		$this->log(LogLevel::ALERT, $message, $context);
82
	}
83
84
	/**
85
	 * Critical conditions.
86
	 *
87
	 * Example: Application component unavailable, unexpected exception.
88
	 *
89
	 * @param string $message
90
	 * @param array  $context
91
	 *
92
	 * @return void
93
	 */
94
	protected function critical($message, array $context = null){
95
		$this->log(LogLevel::CRITICAL, $message, $context);
96
	}
97
98
	/**
99
	 * Runtime errors that do not require immediate action but should typically
100
	 * be logged and monitored.
101
	 *
102
	 * @param string $message
103
	 * @param array  $context
104
	 *
105
	 * @return void
106
	 */
107
	protected function error($message, array $context = null){
108
		$this->log(LogLevel::ERROR, $message, $context);
109
	}
110
111
	/**
112
	 * Exceptional occurrences that are not errors.
113
	 *
114
	 * Example: Use of deprecated APIs, poor use of an API, undesirable things
115
	 * that are not necessarily wrong.
116
	 *
117
	 * @param string $message
118
	 * @param array  $context
119
	 *
120
	 * @return void
121
	 */
122
	protected function warning($message, array $context = null){
123
		$this->log(LogLevel::WARNING, $message, $context);
124
	}
125
126
	/**
127
	 * Normal but significant events.
128
	 *
129
	 * @param string $message
130
	 * @param array  $context
131
	 *
132
	 * @return void
133
	 */
134
	protected function notice($message, array $context = null){
135
		$this->log(LogLevel::NOTICE, $message, $context);
136
	}
137
138
	/**
139
	 * Interesting events.
140
	 *
141
	 * Example: User logs in, SQL logs.
142
	 *
143
	 * @param string $message
144
	 * @param array  $context
145
	 *
146
	 * @return void
147
	 */
148
	protected function info($message, array $context = null){
149
		$this->log(LogLevel::INFO, $message, $context);
150
	}
151
152
	/**
153
	 * Detailed debug information.
154
	 *
155
	 * @param string $message
156
	 * @param array  $context
157
	 *
158
	 * @return void
159
	 */
160
	protected function debug($message, array $context = null){
161
		$this->log(LogLevel::DEBUG, $message, $context);
162
	}
163
164
}
165