Skip to content

Commit

Permalink
Merge pull request #37454 from owncloud/fix-logger-writeExtra-37453
Browse files Browse the repository at this point in the history
Fix logging when logger has no writeExtra method
  • Loading branch information
phil-davis authored May 27, 2020
2 parents dbeef32 + a3da7dd commit ba8dbac
Show file tree
Hide file tree
Showing 3 changed files with 82 additions and 1 deletion.
10 changes: 10 additions & 0 deletions changelog/unreleased/37453
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
Bugfix: Logging of extra fields when logger does not have a writeExtra method

If a logger in use does not have a writeExtra method then an error message would
be generated when a log entry with extra data happens.

This problem has been corrected. In this case the basic log information will be
written without the extra data.

https://github.com/owncloud/core/issues/37453
https://github.com/owncloud/core/pull/37454
2 changes: 1 addition & 1 deletion lib/private/Log.php
Original file line number Diff line number Diff line change
Expand Up @@ -394,7 +394,7 @@ public function log($level, $message, array $context = []) {
if ($level >= $minLevel) {
$logger = $this->logger;
// check if logger supports extra fields
if (!empty($extraFields) && \is_callable($logger, 'writeExtra')) {
if (!empty($extraFields) && \is_callable([$logger, 'writeExtra'])) {
\call_user_func([$logger, 'writeExtra'], $app, $formattedMessage, $level, $logConditionFile, $extraFields);
} else {
\call_user_func([$logger, 'write'], $app, $formattedMessage, $level, $logConditionFile);
Expand Down
71 changes: 71 additions & 0 deletions tests/lib/LoggerWithoutWriteExtraTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
<?php
/**
* Copyright (c) 2020 Phil Davis <[email protected]>
* This file is licensed under the Affero General Public License version 3 or
* later.
* See the COPYING-README file.
*/

namespace Test;

use OC\Log;
use OCP\IConfig;
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
use Symfony\Component\EventDispatcher\EventDispatcher;

class LoggerWithoutWriteExtraTest extends TestCase {
/** @var \OCP\ILogger */
private $logger;
private static $logs = [];

/** @var IConfig | \PHPUnit\Framework\MockObject\MockObject */
private $config;

protected function setUp(): void {
parent::setUp();

self::$logs = [];
$this->config = $this->getMockBuilder(
'\OC\SystemConfig')
->disableOriginalConstructor()
->getMock();
$this->logger = new Log(
'Test\LoggerWithoutWriteExtraTest',
$this->config,
null,
new EventDispatcher()
);
}

private function getLogs() {
return self::$logs;
}

public static function write($app, $message, $level) {
self::$logs[]= "$level $message";
}

public function testExtraFields() {
$extraFields = [
'one' => 'un',
'two' => 'deux',
'three' => 'trois',
];

// with extra fields but logger class has no "writeExtra" method, calls "write"
// the extra fields to not end up anywhere (ignored)
$this->logger->info(
'extra fields test', [
'extraFields' => $extraFields
]
);

// without fields calls "write" always
$this->logger->info('no fields');

$logLines = $this->getLogs();

$this->assertEquals('1 extra fields test', $logLines[0]);
$this->assertEquals('1 no fields', $logLines[1]);
}
}

0 comments on commit ba8dbac

Please sign in to comment.