Traits::getLogLevel()   B
last analyzed

Complexity

Conditions 9
Paths 9

Size

Total Lines 15
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 10
c 1
b 0
f 0
dl 0
loc 15
rs 8.0555
cc 9
nc 9
nop 1
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
 * Base logger class defining required error level constants
16
 *
17
 * @package Base
18
 * @subpackage Logger
19
 */
20
trait Traits
21
{
22
	/**
23
	 * Write as message of severity "emergency" to the log.
24
	 *
25
	 * @param string|array|object $message Message text that should be written to the log facility
26
	 * @param string $facility Facility for logging different types of messages (e.g. message, auth, user, changelog)
27
	 * @return \Aimeos\Base\Logger\Iface Logger object for method chaining
28
	 */
29
	public function emergency( $message, string $facility = 'message' ) : Iface
30
	{
31
		return $this->log( $message, Iface::EMERG, $facility );
32
	}
33
34
35
	/**
36
	 * Write as message of severity "critical" to the log.
37
	 *
38
	 * @param string|array|object $message Message text that should be written to the log facility
39
	 * @param string $facility Facility for logging different types of messages (e.g. message, auth, user, changelog)
40
	 * @return \Aimeos\Base\Logger\Iface Logger object for method chaining
41
	 */
42
	public function critical( $message, string $facility = 'message' ) : Iface
43
	{
44
		return $this->log( $message, Iface::CRIT, $facility );
45
	}
46
47
48
	/**
49
	 * Write as message of severity "alert" to the log.
50
	 *
51
	 * @param string|array|object $message Message text that should be written to the log facility
52
	 * @param string $facility Facility for logging different types of messages (e.g. message, auth, user, changelog)
53
	 * @return \Aimeos\Base\Logger\Iface Logger object for method chaining
54
	 */
55
	public function alert( $message, string $facility = 'message' ) : Iface
56
	{
57
		return $this->log( $message, Iface::ALERT, $facility );
58
	}
59
60
61
	/**
62
	 * Write as message of severity "error" to the log.
63
	 *
64
	 * @param string|array|object $message Message text that should be written to the log facility
65
	 * @param string $facility Facility for logging different types of messages (e.g. message, auth, user, changelog)
66
	 * @return \Aimeos\Base\Logger\Iface Logger object for method chaining
67
	 */
68
	public function error( $message, string $facility = 'message' ) : Iface
69
	{
70
		return $this->log( $message, Iface::ERR, $facility );
71
	}
72
73
74
	/**
75
	 * Write as message of severity "warning" to the log.
76
	 *
77
	 * @param string|array|object $message Message text that should be written to the log facility
78
	 * @param string $facility Facility for logging different types of messages (e.g. message, auth, user, changelog)
79
	 * @return \Aimeos\Base\Logger\Iface Logger object for method chaining
80
	 */
81
	public function warning( $message, string $facility = 'message' ) : Iface
82
	{
83
		return $this->log( $message, Iface::WARN, $facility );
84
	}
85
86
87
	/**
88
	 * Write as message of severity "notice" to the log.
89
	 *
90
	 * @param string|array|object $message Message text that should be written to the log facility
91
	 * @param string $facility Facility for logging different types of messages (e.g. message, auth, user, changelog)
92
	 * @return \Aimeos\Base\Logger\Iface Logger object for method chaining
93
	 */
94
	public function notice( $message, string $facility = 'message' ) : Iface
95
	{
96
		return $this->log( $message, Iface::NOTICE, $facility );
97
	}
98
99
100
	/**
101
	 * Write as message of severity "info" to the log.
102
	 *
103
	 * @param string|array|object $message Message text that should be written to the log facility
104
	 * @param string $facility Facility for logging different types of messages (e.g. message, auth, user, changelog)
105
	 * @return \Aimeos\Base\Logger\Iface Logger object for method chaining
106
	 */
107
	public function info( $message, string $facility = 'message' ) : Iface
108
	{
109
		return $this->log( $message, Iface::INFO, $facility );
110
	}
111
112
113
	/**
114
	 * Write as message of severity "debug" to the log.
115
	 *
116
	 * @param string|array|object $message Message text that should be written to the log facility
117
	 * @param string $facility Facility for logging different types of messages (e.g. message, auth, user, changelog)
118
	 * @return \Aimeos\Base\Logger\Iface Logger object for method chaining
119
	 */
120
	public function debug( $message, string $facility = 'message' ) : Iface
121
	{
122
		return $this->log( $message, Iface::DEBUG, $facility );
123
	}
124
125
126
	/**
127
	 * Checks if the given log constant is valid
128
	 *
129
	 * @param int $level Log constant
130
	 * @return mixed Log level
131
	 * @throws \Aimeos\Base\Logger\Exception If log constant is unknown
132
	 */
133
	protected function getLogLevel( int $level )
134
	{
135
		switch( $level )
136
		{
137
			case Iface::EMERG: return 'emergency';
138
			case Iface::ALERT: return 'alert';
139
			case Iface::CRIT: return 'critical';
140
			case Iface::ERR: return 'error';
141
			case Iface::WARN: return 'warning';
142
			case Iface::NOTICE: return 'notice';
143
			case Iface::INFO: return 'info';
144
			case Iface::DEBUG: return 'debug';
145
		}
146
147
		throw new \Aimeos\Base\Logger\Exception( sprintf( 'Invalid log level constant "%1$d"', $level ) );
148
	}
149
150
151
	/**
152
	 * Writes a message to the configured log facility.
153
	 *
154
	 * @param string|array|object $message Message text that should be written to the log facility
155
	 * @param int $prio Priority of the message for filtering
156
	 * @param string $facility Facility for logging different types of messages (e.g. message, auth, user, changelog)
157
	 * @return \Aimeos\Base\Logger\Iface Logger object for method chaining
158
	 */
159
	abstract public function log( $message, int $prio = Iface::ERR, string $facility = 'message' ) : Iface;
160
}
161