Passed
Push — master ( 8f16ed...3311dc )
by Aimeos
10:04
created

lib/mwlib/src/MW/Logger/Errorlog.php (1 issue)

Labels
Severity
1
<?php
2
3
/**
4
 * @license LGPLv3, http://opensource.org/licenses/LGPL-3.0
5
 * @copyright Metaways Infosystems GmbH, 2011
6
 * @copyright Aimeos (aimeos.org), 2015-2018
7
 * @package MW
8
 * @subpackage Logger
9
 */
10
11
12
namespace Aimeos\MW\Logger;
13
14
15
/**
16
 * Log messages to the error_log file.
17
 *
18
 * @package MW
19
 * @subpackage Logger
20
 */
21
class Errorlog extends \Aimeos\MW\Logger\Base implements \Aimeos\MW\Logger\Iface
22
{
23
	private $loglevel;
24
	private $facilities;
25
26
27
	/**
28
	 * Initializes the logger object.
29
	 *
30
	 * @param integer Log level from \Aimeos\MW\Logger\Base
0 ignored issues
show
The type Aimeos\MW\Logger\Log was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
31
	 * @param string[]|null $facilities Facilities for which messages should be logged
32
	 */
33
	public function __construct( $loglevel = \Aimeos\MW\Logger\Base::ERR, array $facilities = null )
34
	{
35
		$this->loglevel = $loglevel;
36
		$this->facilities = $facilities;
37
	}
38
39
40
	/**
41
	 * Writes a message to the configured log facility.
42
	 *
43
	 * @param string|array|object $message Message text that should be written to the log facility
44
	 * @param integer $priority Priority of the message for filtering
45
	 * @param string $facility Facility for logging different types of messages (e.g. message, auth, user, changelog)
46
	 * @throws \Aimeos\MW\Logger\Exception If the priority is invalid
47
	 * @see \Aimeos\MW\Logger\Base for available log level constants
48
	 */
49
	public function log( $message, $priority = \Aimeos\MW\Logger\Base::ERR, $facility = 'message' )
50
	{
51
		if( $priority <= $this->loglevel
52
			&& ( $this->facilities === null || in_array( $facility, $this->facilities ) ) )
53
		{
54
			switch( $priority )
55
			{
56
				// @codingStandardsIgnoreStart
57
				case \Aimeos\MW\Logger\Base::EMERG: $level = '[emergency]'; break;
58
				case \Aimeos\MW\Logger\Base::ALERT: $level = '[alert]'; break;
59
				case \Aimeos\MW\Logger\Base::CRIT: $level = '[critical]'; break;
60
				case \Aimeos\MW\Logger\Base::ERR: $level = '[error]'; break;
61
				case \Aimeos\MW\Logger\Base::WARN: $level = '[warning]'; break;
62
				case \Aimeos\MW\Logger\Base::NOTICE: $level = '[notice]'; break;
63
				case \Aimeos\MW\Logger\Base::INFO: $level = '[info]'; break;
64
				case \Aimeos\MW\Logger\Base::DEBUG: $level = '[debug]'; break;
65
				// @codingStandardsIgnoreEnd
66
				default:
67
					throw new \Aimeos\MW\Logger\Exception( sprintf( 'Invalid log priority %1$s', $priority ) );
68
			}
69
70
			if( !is_scalar( $message ) ) {
71
				$message = json_encode( $message );
72
			}
73
74
			if( error_log( '<' . $facility . '> ' . $level . ' ' . $message ) === false ) {
75
				throw new \Aimeos\MW\Logger\Exception( sprintf(
76
					'Unable to log message with priority "%1$d": %2$s', $priority, $message ) );
77
			}
78
		}
79
	}
80
}
81