Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix logging when logger has no writeExtra method #37454

Merged
merged 1 commit into from
May 27, 2020
Merged
Show file tree
Hide file tree
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
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]);
}
}