Completed
Push — master ( 94b58b...843436 )
by Aimeos
02:17
created

Laravel5::translatePriority()   B

Complexity

Conditions 9
Paths 9

Size

Total Lines 24

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 24
rs 8.0555
c 0
b 0
f 0
cc 9
nc 9
nop 1
1
<?php
2
3
/**
4
 * @license LGPLv3, http://opensource.org/licenses/LGPL-3.0
5
 * @copyright Aimeos (aimeos.org), 2015-2017
6
 * @package MW
7
 * @subpackage Logger
8
 */
9
10
11
namespace Aimeos\MW\Logger;
12
13
14
/**
15
 * Log messages using the Laravel 5 logger.
16
 *
17
 * @package MW
18
 * @subpackage Logger
19
 */
20
class Laravel5
21
	extends \Aimeos\MW\Logger\Base
0 ignored issues
show
Coding Style introduced by
The extends keyword must be on the same line as the class name
Loading history...
Coding Style introduced by
Expected 0 spaces between "Base" and comma; 1 found
Loading history...
22
	implements \Aimeos\MW\Logger\Iface
0 ignored issues
show
Coding Style introduced by
The implements keyword must be on the same line as the class name
Loading history...
23
{
24
	private $logger = null;
25
26
27
	/**
28
	 * Initializes the logger object.
29
	 *
30
	 * @param \Illuminate\Contracts\Logging\Log $logger Laravel logger object
31
	 */
32
	public function __construct( \Illuminate\Contracts\Logging\Log $logger )
33
	{
34
		$this->logger = $logger;
35
	}
36
37
38
	/**
39
	 * Writes a message to the configured log facility.
40
	 *
41
	 * @param string $message Message text that should be written to the log facility
42
	 * @param integer $priority Priority of the message for filtering
43
	 * @param string $facility Facility for logging different types of messages (e.g. message, auth, user, changelog)
44
	 * @throws \Aimeos\MW\Logger\Exception If an error occurs in Zend_Log
45
	 * @see \Aimeos\MW\Logger\Base for available log level constants
46
	 */
47
	public function log( $message, $priority = \Aimeos\MW\Logger\Base::ERR, $facility = 'message' )
48
	{
49
		try
50
		{
51
			if( !is_scalar( $message ) ) {
52
				$message = json_encode( $message );
53
			}
54
55
			$this->logger->log( $message, $this->translatePriority( $priority ) );
56
		}
57
		catch( \Exception $e )	{
58
			throw new \Aimeos\MW\Logger\Exception( $e->getMessage(), $e->getCode(), $e );
59
		}
60
	}
61
62
63
	/**
64
	 * Translates the log priority to the log levels of Monolog.
65
	 *
66
	 * @param integer $priority Log level from \Aimeos\MW\Logger\Base
67
	 * @return integer Log level from Monolog\Logger
68
	 * @throws \Aimeos\MW\Logger\Exception If log level is unknown
69
	 */
70
	protected function translatePriority( $priority )
71
	{
72
		switch( $priority )
73
		{
74
			case \Aimeos\MW\Logger\Base::EMERG:
75
				return 'emergency';
76
			case \Aimeos\MW\Logger\Base::ALERT:
77
				return 'alert';
78
			case \Aimeos\MW\Logger\Base::CRIT:
79
				return 'critical';
80
			case \Aimeos\MW\Logger\Base::ERR:
81
				return 'error';
82
			case \Aimeos\MW\Logger\Base::WARN:
83
				return 'warning';
84
			case \Aimeos\MW\Logger\Base::NOTICE:
85
				return 'notice';
86
			case \Aimeos\MW\Logger\Base::INFO:
87
				return 'info';
88
			case \Aimeos\MW\Logger\Base::DEBUG:
89
				return 'debug';
90
			default:
91
				throw new \Aimeos\MW\Logger\Exception( 'Invalid log level' );
92
		}
93
	}
94
}