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 a database table. |
16
|
|
|
* |
17
|
|
|
* @package Base |
18
|
|
|
* @subpackage Logger |
19
|
|
|
*/ |
20
|
|
|
class DB implements Iface |
21
|
|
|
{ |
22
|
|
|
use Traits; |
23
|
|
|
|
24
|
|
|
|
25
|
|
|
private \Aimeos\Base\DB\Statement\Iface $stmt; |
26
|
|
|
private string $requestid; |
27
|
|
|
private ?array $facilities; |
28
|
|
|
private int $loglevel; |
29
|
|
|
|
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* Initializes the logger object. |
33
|
|
|
* |
34
|
|
|
* The log statement must be like: |
35
|
|
|
* INSERT INTO logtable (facility, logtime, priority, message, requestid) VALUES (?, ?, ?, ?, ?) |
36
|
|
|
* |
37
|
|
|
* @param \Aimeos\Base\DB\Statement\Iface $stmt Database statement object for inserting data |
38
|
|
|
* @param int $loglevel Minimum priority for logging |
39
|
|
|
* @param string[]|null $facilities Facilities for which messages should be logged |
40
|
|
|
* @param string|null $requestid Unique identifier to identify multiple log entries for the same request faster |
41
|
|
|
*/ |
42
|
|
|
public function __construct( \Aimeos\Base\DB\Statement\Iface $stmt, int $loglevel = Iface::ERR, |
43
|
|
|
?array $facilities = null, ?string $requestid = null ) |
44
|
|
|
{ |
45
|
|
|
$this->stmt = $stmt; |
46
|
|
|
$this->loglevel = $loglevel; |
47
|
|
|
$this->facilities = $facilities; |
48
|
|
|
$this->requestid = $requestid ?: md5( php_uname( 'n' ) . getmypid() . date( 'Y-m-d H:i:s' ) ); |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* Writes a message to the configured log facility. |
54
|
|
|
* |
55
|
|
|
* @param string|array|object $message Message text that should be written to the log facility |
56
|
|
|
* @param int $prio Priority of the message for filtering |
57
|
|
|
* @param string $facility Facility for logging different types of messages (e.g. message, auth, user, changelog) |
58
|
|
|
* @return \Aimeos\Base\Logger\Iface Logger object for method chaining |
59
|
|
|
* @throws \Aimeos\Base\Logger\Exception If the priority is invalid |
60
|
|
|
* @throws \Aimeos\Base\DB\Exception If an error occurs while adding log message |
61
|
|
|
* @see \Aimeos\Base\Logger\Base for available log level constants |
62
|
|
|
*/ |
63
|
|
|
public function log( $message, int $prio = Iface::ERR, string $facility = 'message' ) : Iface |
64
|
|
|
{ |
65
|
|
|
if( $prio <= $this->loglevel && ( $this->facilities === null || in_array( $facility, $this->facilities ) ) ) |
66
|
|
|
{ |
67
|
|
|
$this->getLogLevel( $prio ); |
68
|
|
|
|
69
|
|
|
if( !is_scalar( $message ) ) { |
70
|
|
|
$message = json_encode( $message ); |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
$this->stmt->bind( 1, $facility ); |
74
|
|
|
$this->stmt->bind( 2, date( 'Y-m-d H:i:s' ) ); |
75
|
|
|
$this->stmt->bind( 3, $prio, \Aimeos\Base\DB\Statement\Base::PARAM_INT ); |
76
|
|
|
$this->stmt->bind( 4, $message ); |
77
|
|
|
$this->stmt->bind( 5, $this->requestid ); |
78
|
|
|
$this->stmt->execute()->finish(); |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
return $this; |
82
|
|
|
} |
83
|
|
|
} |
84
|
|
|
|