Skip to content

Commit 613f608

Browse files
committed
Fix logging data context to file
It was only logged when an exception was provided or when using logData (which is not being much used) Only tested with file write logger, but shouldn't work differently. Crash reporters always had the context. Signed-off-by: Thomas Citharel <tcit@tcit.fr>
1 parent 3197ae8 commit 613f608

File tree

4 files changed

+52
-13
lines changed

4 files changed

+52
-13
lines changed

lib/private/Log.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
* @author Olivier Paroz <github@oparoz.com>
1616
* @author Robin Appelman <robin@icewind.nl>
1717
* @author Roeland Jago Douma <roeland@famdouma.nl>
18+
* @author Thomas Citharel <nextcloud@tcit.fr>
1819
* @author Thomas Müller <thomas.mueller@tmit.eu>
1920
* @author Victor Dubiniuk <dubiniuk@owncloud.com>
2021
*
@@ -211,7 +212,9 @@ public function log(int $level, string $message, array $context = []) {
211212

212213
try {
213214
if ($level >= $minLevel) {
214-
$this->writeLog($app, $message, $level);
215+
$contextWithMessage = $context;
216+
$contextWithMessage['message'] = $message;
217+
$this->writeLog($app, $contextWithMessage, $level);
215218

216219
if ($this->crashReporters !== null) {
217220
$messageContext = array_merge(

lib/private/Log/LogDetails.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
* @author Arthur Schiwon <blizzz@arthur-schiwon.de>
66
* @author Christoph Wurst <christoph@winzerhof-wurst.at>
77
* @author Julius Härtl <jus@bitgrid.net>
8+
* @author Thomas Citharel <nextcloud@tcit.fr>
89
*
910
* @license GNU AGPL version 3 or any later version
1011
*
@@ -90,8 +91,9 @@ public function logDetails(string $app, $message, int $level): array {
9091
$entry['exception'] = $message;
9192
$entry['message'] = $message['CustomMessage'] !== '--' ? $message['CustomMessage'] : $message['Message'];
9293
} else {
93-
$entry['data'] = $message;
9494
$entry['message'] = $message['message'] ?? '(no message provided)';
95+
unset($message['message']);
96+
$entry['data'] = $message;
9597
}
9698
}
9799

tests/lib/Log/FileTest.php

Lines changed: 27 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
<?php
22
/**
3+
*
4+
* @author Thomas Citharel <nextcloud@tcit.fr>
35
*
46
* This library is free software; you can redistribute it and/or
57
* modify it under the terms of the GNU AFFERO GENERAL PUBLIC LICENSE
@@ -18,6 +20,7 @@
1820
namespace Test\Log;
1921

2022
use OC\Log\File;
23+
use OCP\IConfig;
2124
use OCP\ILogger;
2225
use Test\TestCase;
2326

@@ -36,7 +39,7 @@ protected function setUp(): void {
3639
$config = \OC::$server->getSystemConfig();
3740
$this->restore_logfile = $config->getValue("logfile");
3841
$this->restore_logdateformat = $config->getValue('logdateformat');
39-
42+
4043
$config->setValue("logfile", $config->getValue('datadirectory') . "/logtest.log");
4144
$this->logFile = new File($config->getValue('datadirectory') . '/logtest.log', '', $config);
4245
}
@@ -55,7 +58,28 @@ protected function tearDown(): void {
5558
$this->logFile = new File($this->restore_logfile, '', $config);
5659
parent::tearDown();
5760
}
58-
61+
62+
public function testLogging() {
63+
$config = \OC::$server->get(IConfig::class);
64+
# delete old logfile
65+
unlink($config->getSystemValue('logfile'));
66+
67+
# set format & write log line
68+
$config->setSystemValue('logdateformat', 'u');
69+
$this->logFile->write('code', ['something' => 'extra', 'message' => 'Testing logging'], ILogger::ERROR);
70+
71+
# read log line
72+
$handle = @fopen($config->getSystemValue('logfile'), 'r');
73+
$line = fread($handle, 1000);
74+
fclose($handle);
75+
76+
# check log has data content
77+
$values = (array) json_decode($line, true);
78+
$this->assertArrayNotHasKey('message', $values['data']);
79+
$this->assertEquals('extra', $values['data']['something']);
80+
$this->assertEquals('Testing logging', $values['message']);
81+
}
82+
5983
public function testMicrosecondsLogTimestamp() {
6084
$config = \OC::$server->getConfig();
6185
# delete old logfile
@@ -69,7 +93,7 @@ public function testMicrosecondsLogTimestamp() {
6993
$handle = @fopen($config->getSystemValue('logfile'), 'r');
7094
$line = fread($handle, 1000);
7195
fclose($handle);
72-
96+
7397
# check timestamp has microseconds part
7498
$values = (array) json_decode($line);
7599
$microseconds = $values['time'];

tests/lib/LoggerTest.php

Lines changed: 18 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -9,29 +9,32 @@
99
namespace Test;
1010

1111
use OC\Log;
12+
use OC\SystemConfig;
1213
use OCP\ILogger;
1314
use OCP\Log\IWriter;
15+
use OCP\Support\CrashReport\IRegistry;
16+
use PHPUnit\Framework\MockObject\MockObject;
1417

1518
class LoggerTest extends TestCase implements IWriter {
1619

17-
/** @var \OC\SystemConfig|\PHPUnit\Framework\MockObject\MockObject */
20+
/** @var SystemConfig|MockObject */
1821
private $config;
1922

20-
/** @var \OCP\Support\CrashReport\IRegistry|\PHPUnit\Framework\MockObject\MockObject */
23+
/** @var IRegistry|MockObject */
2124
private $registry;
2225

23-
/** @var \OCP\ILogger */
26+
/** @var ILogger */
2427
private $logger;
2528

2629
/** @var array */
27-
private $logs = [];
30+
private array $logs = [];
2831

2932
protected function setUp(): void {
3033
parent::setUp();
3134

3235
$this->logs = [];
33-
$this->config = $this->createMock(\OC\SystemConfig::class);
34-
$this->registry = $this->createMock(\OCP\Support\CrashReport\IRegistry::class);
36+
$this->config = $this->createMock(SystemConfig::class);
37+
$this->registry = $this->createMock(IRegistry::class);
3538
$this->logger = new Log($this, $this->config, null, $this->registry);
3639
}
3740

@@ -63,12 +66,19 @@ public function testAppCondition() {
6366
$this->assertEquals($expected, $this->getLogs());
6467
}
6568

66-
private function getLogs() {
69+
public function testLogging(): void {
70+
$writerMock = $this->createMock(IWriter::class);
71+
$logFile = new Log($writerMock, $this->config);
72+
$writerMock->expects($this->once())->method('write')->with('no app in context', ['something' => 'extra', 'message' => 'Testing logging']);
73+
$logFile->error('Testing logging', ['something' => 'extra']);
74+
}
75+
76+
private function getLogs(): array {
6777
return $this->logs;
6878
}
6979

7080
public function write(string $app, $message, int $level) {
71-
$this->logs[] = "$level $message";
81+
$this->logs[] = $level . " " . $message['message'];
7282
}
7383

7484
public function userAndPasswordData(): array {

0 commit comments

Comments
 (0)