Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 <[email protected]>
  • Loading branch information
ChristophWurst authored and backportbot[bot] committed Nov 29, 2021
commit 96aaa5dfa2973156ff21263fec5ff9b78e7858e1
27 changes: 19 additions & 8 deletions lib/private/Log.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@
use OCP\Log\IFileBased;
use OCP\Log\IWriter;
use OCP\Support\CrashReport\IRegistry;
use function strtr;

/**
* logging utilities
Expand Down Expand Up @@ -206,13 +207,7 @@ public function log(int $level, string $message, array $context = []) {
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) {
Expand Down Expand Up @@ -315,7 +310,7 @@ public function logException(\Throwable $exception, array $context = []) {

$serializer = new ExceptionSerializer($this->config);
$data = $serializer->serializeException($exception);
$data['CustomMessage'] = $context['message'] ?? '--';
$data['CustomMessage'] = $this->interpolateMessage($context, $context['message'] ?? '--');

$minLevel = $this->getLogLevel($context);

Expand Down Expand Up @@ -376,4 +371,20 @@ public function getLogPath():string {
}
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);
}
}