Passed
Push — master ( b1c60f...73b3e6 )
by Aimeos
02:08
created

Monolog::log()   A

Complexity

Conditions 5
Paths 8

Size

Total Lines 18
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

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