diff options
author | Christoph Wurst <christoph@winzerhof-wurst.at> | 2021-11-29 14:29:35 +0100 |
---|---|---|
committer | Christoph Wurst <christoph@winzerhof-wurst.at> | 2021-11-29 14:29:35 +0100 |
commit | 311b3ffa2e8966df883d738f2a741f139ed914da (patch) | |
tree | b7869ce853b7799713a4041934d7d37fd07cdf48 /lib/private/Log.php | |
parent | 541751d42a27b2bd0b126d6366e444829ff59cf7 (diff) | |
download | nextcloud-server-311b3ffa2e8966df883d738f2a741f139ed914da.tar.gz nextcloud-server-311b3ffa2e8966df883d738f2a741f139ed914da.zip |
Interpolate the log message also for logged exceptions
According to PSR-3 the log message can have placeholders that are
replaced from the context object. Our logger implementation did that for
all PSR-3 logger methods. The only exception was our custom `logException`.
Since PsrLoggerAdapter calls logException when an exception key is
present in the context object, log messages were no longer interpolated.
Signed-off-by: Christoph Wurst <christoph@winzerhof-wurst.at>
Diffstat (limited to 'lib/private/Log.php')
-rw-r--r-- | lib/private/Log.php | 27 |
1 files changed, 19 insertions, 8 deletions
diff --git a/lib/private/Log.php b/lib/private/Log.php index 9c6a1b614c6..edbfdea7b9d 100644 --- a/lib/private/Log.php +++ b/lib/private/Log.php @@ -43,6 +43,7 @@ use OCP\ILogger; use OCP\Log\IFileBased; use OCP\Log\IWriter; use OCP\Support\CrashReport\IRegistry; +use function strtr; /** * logging utilities @@ -206,13 +207,7 @@ class Log implements ILogger, IDataLogger { array_walk($context, [$this->normalizer, 'format']); $app = $context['app'] ?? 'no app in context'; - - // interpolate $message as defined in PSR-3 - $replace = []; - foreach ($context as $key => $val) { - $replace['{' . $key . '}'] = $val; - } - $message = strtr($message, $replace); + $message = $this->interpolateMessage($context, $message); try { if ($level >= $minLevel) { @@ -315,7 +310,7 @@ class Log implements ILogger, IDataLogger { $serializer = new ExceptionSerializer($this->config); $data = $serializer->serializeException($exception); - $data['CustomMessage'] = $context['message'] ?? '--'; + $data['CustomMessage'] = $this->interpolateMessage($context, $context['message'] ?? '--'); $minLevel = $this->getLogLevel($context); @@ -376,4 +371,20 @@ class Log implements ILogger, IDataLogger { } throw new \RuntimeException('Log implementation has no path'); } + + /** + * Interpolate $message as defined in PSR-3 + * + * @param array $context + * @param string $message + * + * @return string + */ + private function interpolateMessage(array $context, string $message): string { + $replace = []; + foreach ($context as $key => $val) { + $replace['{' . $key . '}'] = $val; + } + return strtr($message, $replace); + } } |