1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* @package core |
5
|
|
|
*/ |
6
|
|
|
/** |
7
|
|
|
* The Log class acts a simple wrapper to write errors to a file so that it can |
8
|
|
|
* be read at a later date. There is one Log file in Symphony, stored in the main |
9
|
|
|
* `LOGS` directory. |
10
|
|
|
*/ |
11
|
|
|
use Psr\Log\LogLevel; |
12
|
|
|
use Monolog\Logger; |
13
|
|
|
|
14
|
|
|
class Log |
15
|
|
|
{ |
16
|
|
|
/** |
17
|
|
|
* The actual Monolog Logger instance |
18
|
|
|
* @var Logger |
19
|
|
|
*/ |
20
|
|
|
private $log = null; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* Maps error levels to the appropriate log level |
24
|
|
|
* |
25
|
|
|
* @var array |
26
|
|
|
*/ |
27
|
|
|
private $errorLevelMap = array( |
28
|
|
|
E_ERROR => LogLevel::CRITICAL, |
29
|
|
|
E_WARNING => LogLevel::WARNING, |
30
|
|
|
E_PARSE => LogLevel::ALERT, |
31
|
|
|
E_NOTICE => LogLevel::NOTICE, |
32
|
|
|
E_CORE_ERROR => LogLevel::CRITICAL, |
33
|
|
|
E_CORE_WARNING => LogLevel::WARNING, |
34
|
|
|
E_COMPILE_ERROR => LogLevel::ALERT, |
35
|
|
|
E_COMPILE_WARNING => LogLevel::WARNING, |
36
|
|
|
E_USER_ERROR => LogLevel::ERROR, |
37
|
|
|
E_USER_WARNING => LogLevel::WARNING, |
38
|
|
|
E_USER_NOTICE => LogLevel::NOTICE, |
39
|
|
|
E_STRICT => LogLevel::NOTICE, |
40
|
|
|
E_RECOVERABLE_ERROR => LogLevel::ERROR, |
41
|
|
|
E_DEPRECATED => LogLevel::NOTICE, |
42
|
|
|
E_USER_DEPRECATED => LogLevel::NOTICE, |
43
|
|
|
); |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* Accepts an instance of the desired Logger. |
47
|
|
|
* |
48
|
|
|
* @param Logger $logger |
49
|
|
|
*/ |
50
|
|
|
public function __construct(Logger $logger) |
51
|
|
|
{ |
52
|
|
|
$this->log = $logger; |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* Accessor for `$log`. |
57
|
|
|
* |
58
|
|
|
* @return array |
59
|
|
|
*/ |
60
|
|
|
public function getLog() |
61
|
|
|
{ |
62
|
|
|
return $this->log; |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* Magic method allows developers to target the underlying Logger methods if they |
67
|
|
|
* like. |
68
|
|
|
* |
69
|
|
|
* @throws BadMethodCallException |
70
|
|
|
* @param string $method |
71
|
|
|
* @param array $args |
72
|
|
|
* @return mixed |
73
|
|
|
*/ |
74
|
|
|
public function __call($method, $args) |
75
|
|
|
{ |
76
|
|
|
if (is_callable(array($this->log, $method))) { |
77
|
|
|
return call_user_func_array(array($this->log, $method), $args); |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
throw new BadMethodCallException('The Log has no callable ' . $method); |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
/** |
84
|
|
|
* Provides an old style accessor for pushing messages into the Log. It is |
85
|
|
|
* deprecated, and it's recommended to use the direct PSR-3 log methods instead. |
86
|
|
|
* |
87
|
|
|
* @param string $message |
88
|
|
|
* The message to add to the Log |
89
|
|
|
* @param integer $type |
90
|
|
|
* A PHP error constant for this message, defaults to E_NOTICE |
91
|
|
|
* @param array $context |
92
|
|
|
*/ |
93
|
|
|
public function pushToLog($message, $type = E_NOTICE, array $context = null) |
94
|
|
|
{ |
95
|
|
|
$level = isset($this->errorLevelMap[$type]) ? $this->errorLevelMap[$type] : LogLevel::CRITICAL; |
96
|
|
|
$this->log->log($level, $message, is_array($context) ? $context : array()); |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
/** |
100
|
|
|
* Given an Exception, this function will add it to the internal `$_log` |
101
|
|
|
* so that it can be written to the Log. |
102
|
|
|
* |
103
|
|
|
* @since Symphony 2.3.2 |
104
|
|
|
* @param Exception $exception |
105
|
|
|
*/ |
106
|
|
|
public function pushExceptionToLog(Exception $exception) |
107
|
|
|
{ |
108
|
|
|
$message = sprintf( |
109
|
|
|
'%s %s - %s on line %d of %s', |
110
|
|
|
get_class($exception), |
111
|
|
|
$exception->getCode(), |
112
|
|
|
$exception->getMessage(), |
113
|
|
|
$exception->getLine(), |
114
|
|
|
$exception->getFile() |
115
|
|
|
); |
116
|
|
|
|
117
|
|
|
return $this->pushToLog($message, $exception->getCode(), array('exception' => $exception)); |
118
|
|
|
} |
119
|
|
|
} |
120
|
|
|
|