-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathWrapper.php
132 lines (117 loc) · 4.16 KB
/
Wrapper.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
<?php
namespace Boerl\Zf1LogPsr3;
use Psr\Log\AbstractLogger;
use Psr\Log\LogLevel;
use Zend_Log as Zf1Logger;
class Wrapper extends AbstractLogger
{
/* @var Zf1Logger The original ZF1 Logger */
protected $zf1Logger;
/* @var array<string,mixed> Config. */
protected $config;
/**
* @return string[] Placeholders with these names are always in an event passed to
* {@see Zend_Log_Formatter_Interface::format()}.
* Source: {@see Zend_Log::_packEvent()}
*/
public static function getZf1LogBaseEventKeys()
{
return ['timestamp', 'message', 'priority', 'priorityName'];
}
/**
* @param Zf1Logger $zf1Logger The original ZF1 Logger to be wrapped.
* @param array<string,mixed> $config {@see setConfig()}
*/
public function __construct(Zf1Logger $zf1Logger, array $config = [])
{
$this->zf1Logger = $zf1Logger;
$this->setConfig($config);
}
/**
* @param array $config Will be merged with {@see getDefaultConfig()} into the configuration for this logger.
*/
public function setConfig(array $config = [])
{
$this->config = array_merge($this->getDefaultConfig(), $config);
}
/**
* @return array<string,mixed> Default configuration.
* - `add_log_level` : bool [=true] - Whether to include `logLevel` in the `extras` array.
* - `rewrite_placeholders` : bool [=true] - Whether to rewrite PSR-3 style placeholders into ZF1 style.
*/
public function getDefaultConfig()
{
return [
'add_log_level' => true,
'rewrite_placeholders' => true,
];
}
/**
* @inheritdoc
*/
public function log($psrLogLevel, $message, array $context = [])
{
// Translate from PSR3 log level to ZF1 priority.
$zf1LogPriority = $this->psrLogLevelToZf1($psrLogLevel);
// Add `logLevel` to the context.
if ($this->config['add_log_level']) {
$context = array_merge(['logLevel' => $psrLogLevel], $context);
}
// Replace PSR placeholders with ZF1 placeholders.
if ($this->config['rewrite_placeholders']) {
$this->psrPlaceHoldersToZf1($message, array_keys($context));
}
try {
$this->zf1Logger->log($message, $zf1LogPriority, $context);
} catch (\Zend_Log_Exception $zf1LogEx) {
throw new InvalidArgumentException($zf1LogEx->getMessage(), $zf1LogEx->getCode(), $zf1LogEx);
}
}
/**
* Translates a PSR-3 Log Level to a ZF1 Log Priority.
*
* @param int $psrLogLevel
* @return int|null
*/
public static function psrLogLevelToZf1($psrLogLevel)
{
switch ($psrLogLevel) {
case LogLevel::EMERGENCY:
return Zf1Logger::EMERG;
case LogLevel::ALERT:
return Zf1Logger::ALERT;
case LogLevel::CRITICAL:
return Zf1Logger::CRIT;
case LogLevel::ERROR:
return Zf1Logger::ERR;
case LogLevel::WARNING:
return Zf1Logger::WARN;
case LogLevel::NOTICE:
return Zf1Logger::NOTICE;
case LogLevel::INFO:
return Zf1Logger::INFO;
case LogLevel::DEBUG:
return Zf1Logger::DEBUG;
}
return null;
}
/**
* Rewrites PSR-3 style placeholders to ZF1 style. E.g. `{example}` becomes `%example%`.
*
* @param string $message Will be rewritten.
* @param string[] $names Only replace these placeholders. (Optional. By default all will be replaced.)
* {@see getZf1LogBaseEventKeys Base ZF1 placeholders} will always be replaced.
*/
public static function psrPlaceHoldersToZf1(&$message, array $names = null)
{
$names = is_null($names) ? $names : array_merge(static::getZf1LogBaseEventKeys(), $names);
$message = preg_replace_callback('/{(?<name>[A-Za-z0-9_.]+)}/', function($match) use ($names) {
$name = $match['name'];
if ($names && !in_array($name, $names)) {
return $match[0];
} else {
return "%{$name}%";
}
}, $message);
}
}