Issues (50)

src/Logger/Errorlog.php (1 issue)

Labels
Severity
1
<?php
2
3
/**
4
 * @license LGPLv3, https://opensource.org/licenses/LGPL-3.0
5
 * @copyright Aimeos (aimeos.org), 2015-2025
6
 * @package Base
7
 * @subpackage Logger
8
 */
9
10
11
namespace Aimeos\Base\Logger;
12
13
14
/**
15
 * Log messages to the error_log file.
16
 *
17
 * @package Base
18
 * @subpackage Logger
19
 */
20
class Errorlog implements Iface
21
{
22
	use Traits;
23
24
25
	private int $loglevel;
26
	private ?array $facilities;
27
	private string $requestid;
28
29
30
	/**
31
	 * Initializes the logger object.
32
	 *
33
	 * @param int Log level from \Aimeos\Base\Logger\Base
0 ignored issues
show
The type Aimeos\Base\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...
34
	 * @param string[]|null $facilities Facilities for which messages should be logged
35
	 * @param string|null $requestid Unique identifier to identify multiple log entries for the same request faster
36
	 */
37
	public function __construct( int $loglevel = Iface::ERR, ?array $facilities = null, ?string $requestid = null )
38
	{
39
		$this->loglevel = $loglevel;
40
		$this->facilities = $facilities;
41
		$this->requestid = $requestid ?: md5( php_uname( 'n' ) . getmypid() . date( 'Y-m-d H:i:s' ) );
42
	}
43
44
45
	/**
46
	 * Writes a message to the configured log facility.
47
	 *
48
	 * @param string|array|object $message Message text that should be written to the log facility
49
	 * @param int $prio Priority of the message for filtering
50
	 * @param string $facility Facility for logging different types of messages (e.g. message, auth, user, changelog)
51
	 * @return \Aimeos\Base\Logger\Iface Logger object for method chaining
52
	 * @throws \Aimeos\Base\Logger\Exception If the priority is invalid
53
	 * @see \Aimeos\Base\Logger\Base for available log level constants
54
	 */
55
	public function log( $message, int $prio = Iface::ERR, string $facility = 'message' ) : Iface
56
	{
57
		if( $prio <= $this->loglevel && ( $this->facilities === null || in_array( $facility, $this->facilities ) ) )
58
		{
59
			$level = $this->getLogLevel( $prio );
60
61
			if( !is_scalar( $message ) ) {
62
				$message = json_encode( $message );
63
			}
64
65
			if( error_log( '<' . $facility . '> [' . $level . '] [' . $this->requestid . '] ' . $message ) === false ) {
66
				throw new \Aimeos\Base\Logger\Exception( 'Unable to log message to error log' );
67
			}
68
		}
69
70
		return $this;
71
	}
72
}
73