|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* @license LGPLv3, http://opensource.org/licenses/LGPL-3.0 |
|
5
|
|
|
* @copyright Aimeos (aimeos.org), 2014-2022 |
|
6
|
|
|
* @package MW |
|
7
|
|
|
* @subpackage Logger |
|
8
|
|
|
*/ |
|
9
|
|
|
|
|
10
|
|
|
|
|
11
|
|
|
namespace Aimeos\Base\Logger; |
|
12
|
|
|
|
|
13
|
|
|
|
|
14
|
|
|
/** |
|
15
|
|
|
* Log messages using Monolog. |
|
16
|
|
|
* |
|
17
|
|
|
* @package MW |
|
18
|
|
|
* @subpackage Logger |
|
19
|
|
|
*/ |
|
20
|
|
|
class Monolog implements Iface |
|
21
|
|
|
{ |
|
22
|
|
|
use Traits; |
|
23
|
|
|
|
|
24
|
|
|
|
|
25
|
|
|
private $logger; |
|
26
|
|
|
private $facilities; |
|
27
|
|
|
|
|
28
|
|
|
|
|
29
|
|
|
/** |
|
30
|
|
|
* Initializes the logger object. |
|
31
|
|
|
* |
|
32
|
|
|
* @param Monolog\Logger $logger Monolog logger object |
|
|
|
|
|
|
33
|
|
|
* @param array|null $facilities Facilities for which messages should be logged |
|
34
|
|
|
*/ |
|
35
|
|
|
public function __construct( \Monolog\Logger $logger, array $facilities = null ) |
|
36
|
|
|
{ |
|
37
|
|
|
$this->logger = $logger; |
|
38
|
|
|
$this->facilities = $facilities; |
|
39
|
|
|
} |
|
40
|
|
|
|
|
41
|
|
|
|
|
42
|
|
|
/** |
|
43
|
|
|
* Writes a message to the configured log facility. |
|
44
|
|
|
* |
|
45
|
|
|
* @param string|array|object $message Message text that should be written to the log facility |
|
46
|
|
|
* @param int $priority Priority of the message for filtering |
|
47
|
|
|
* @param string $facility Facility for logging different types of messages (e.g. message, auth, user, changelog) |
|
48
|
|
|
* @return \Aimeos\Base\Logger\Iface Logger object for method chaining |
|
49
|
|
|
* @throws \Aimeos\Base\Logger\Exception If an error occurs |
|
50
|
|
|
* @see \Aimeos\Base\Logger\Iface for available log level constants |
|
51
|
|
|
*/ |
|
52
|
|
|
public function log( $message, int $priority = Iface::ERR, string $facility = 'message' ) : Iface |
|
53
|
|
|
{ |
|
54
|
|
|
try |
|
55
|
|
|
{ |
|
56
|
|
|
if( $this->facilities === null || in_array( $facility, $this->facilities ) ) |
|
57
|
|
|
{ |
|
58
|
|
|
if( !is_scalar( $message ) ) { |
|
59
|
|
|
$message = json_encode( $message ); |
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
|
|
$this->logger->log( $this->getLogLevel( $priority ), $message ); |
|
63
|
|
|
} |
|
64
|
|
|
} |
|
65
|
|
|
catch( \Exception $e ) { |
|
66
|
|
|
throw new Exception( $e->getMessage(), $e->getCode(), $e ); |
|
67
|
|
|
} |
|
68
|
|
|
|
|
69
|
|
|
return $this; |
|
70
|
|
|
} |
|
71
|
|
|
|
|
72
|
|
|
|
|
73
|
|
|
/** |
|
74
|
|
|
* Checks if the given log constant is valid |
|
75
|
|
|
* |
|
76
|
|
|
* @param int $level Log constant |
|
77
|
|
|
* @return mixed Log level |
|
78
|
|
|
* @throws \Aimeos\Base\Logger\Exception If log constant is unknown |
|
79
|
|
|
*/ |
|
80
|
|
|
protected function getLogLevel( int $level ) |
|
81
|
|
|
{ |
|
82
|
|
|
switch( $level ) |
|
83
|
|
|
{ |
|
84
|
|
|
case \Aimeos\Base\Logger\Iface::EMERG: return \Monolog\Logger::EMERGENCY; |
|
85
|
|
|
case \Aimeos\Base\Logger\Iface::ALERT: return \Monolog\Logger::ALERT; |
|
86
|
|
|
case \Aimeos\Base\Logger\Iface::CRIT: return \Monolog\Logger::CRITICAL; |
|
87
|
|
|
case \Aimeos\Base\Logger\Iface::ERR: return \Monolog\Logger::ERROR; |
|
88
|
|
|
case \Aimeos\Base\Logger\Iface::WARN: return \Monolog\Logger::WARNING; |
|
89
|
|
|
case \Aimeos\Base\Logger\Iface::NOTICE: return \Monolog\Logger::NOTICE; |
|
90
|
|
|
case \Aimeos\Base\Logger\Iface::INFO: return \Monolog\Logger::INFO; |
|
91
|
|
|
case \Aimeos\Base\Logger\Iface::DEBUG: return \Monolog\Logger::DEBUG; |
|
92
|
|
|
} |
|
93
|
|
|
|
|
94
|
|
|
throw new \Aimeos\Base\Logger\Exception( 'Invalid log level' ); |
|
95
|
|
|
} |
|
96
|
|
|
} |
|
97
|
|
|
|