2015-09-10 17:02:28 -04:00
|
|
|
<?php
|
2025-06-30 09:04:05 -04:00
|
|
|
|
2015-09-10 17:02:28 -04:00
|
|
|
/**
|
2022-05-02 11:49:32 -04:00
|
|
|
*
|
2024-05-10 09:09:14 -04:00
|
|
|
* SPDX-FileCopyrightText: 2016-2024 Nextcloud GmbH and Nextcloud contributors
|
|
|
|
|
* SPDX-License-Identifier: AGPL-3.0-or-later
|
2015-09-10 17:02:28 -04:00
|
|
|
*/
|
|
|
|
|
|
2016-05-02 08:04:58 -04:00
|
|
|
namespace Test\Log;
|
|
|
|
|
|
2016-07-22 05:44:19 -04:00
|
|
|
use OC\Log\File;
|
2025-06-12 12:31:58 -04:00
|
|
|
use OC\SystemConfig;
|
2022-05-02 11:49:32 -04:00
|
|
|
use OCP\IConfig;
|
2018-04-25 09:22:28 -04:00
|
|
|
use OCP\ILogger;
|
2025-06-12 12:31:58 -04:00
|
|
|
use OCP\Server;
|
2016-05-02 08:04:58 -04:00
|
|
|
use Test\TestCase;
|
|
|
|
|
|
2015-11-20 05:27:11 -05:00
|
|
|
/**
|
2016-07-22 08:47:50 -04:00
|
|
|
* Class FileTest
|
2015-11-20 05:27:11 -05:00
|
|
|
*/
|
2020-04-10 08:19:56 -04:00
|
|
|
class FileTest extends TestCase {
|
2015-09-10 17:02:28 -04:00
|
|
|
private $restore_logfile;
|
|
|
|
|
private $restore_logdateformat;
|
|
|
|
|
|
2018-04-24 16:14:00 -04:00
|
|
|
/** @var File */
|
|
|
|
|
protected $logFile;
|
|
|
|
|
|
2019-11-21 10:40:38 -05:00
|
|
|
protected function setUp(): void {
|
2015-09-10 17:02:28 -04:00
|
|
|
parent::setUp();
|
2025-06-12 12:31:58 -04:00
|
|
|
$config = Server::get(SystemConfig::class);
|
2024-08-23 09:10:27 -04:00
|
|
|
$this->restore_logfile = $config->getValue('logfile');
|
2018-04-26 17:54:11 -04:00
|
|
|
$this->restore_logdateformat = $config->getValue('logdateformat');
|
2022-05-02 11:49:32 -04:00
|
|
|
|
2024-08-23 09:10:27 -04:00
|
|
|
$config->setValue('logfile', $config->getValue('datadirectory') . '/logtest.log');
|
2018-04-26 17:54:11 -04:00
|
|
|
$this->logFile = new File($config->getValue('datadirectory') . '/logtest.log', '', $config);
|
2015-09-10 17:02:28 -04:00
|
|
|
}
|
2019-11-21 10:40:38 -05:00
|
|
|
protected function tearDown(): void {
|
2025-06-12 12:31:58 -04:00
|
|
|
$config = Server::get(SystemConfig::class);
|
2015-09-10 17:02:28 -04:00
|
|
|
if (isset($this->restore_logfile)) {
|
2024-08-23 09:10:27 -04:00
|
|
|
$config->getValue('logfile', $this->restore_logfile);
|
2015-09-10 17:02:28 -04:00
|
|
|
} else {
|
2024-08-23 09:10:27 -04:00
|
|
|
$config->deleteValue('logfile');
|
2020-04-09 10:07:47 -04:00
|
|
|
}
|
2015-09-10 17:02:28 -04:00
|
|
|
if (isset($this->restore_logdateformat)) {
|
2024-08-23 09:10:27 -04:00
|
|
|
$config->getValue('logdateformat', $this->restore_logdateformat);
|
2015-09-10 17:02:28 -04:00
|
|
|
} else {
|
2024-08-23 09:10:27 -04:00
|
|
|
$config->deleteValue('logdateformat');
|
2018-04-24 16:14:00 -04:00
|
|
|
}
|
2018-04-24 20:27:43 -04:00
|
|
|
$this->logFile = new File($this->restore_logfile, '', $config);
|
2015-09-10 17:02:28 -04:00
|
|
|
parent::tearDown();
|
|
|
|
|
}
|
2022-05-02 11:49:32 -04:00
|
|
|
|
2024-09-15 16:32:31 -04:00
|
|
|
public function testLogging(): void {
|
2025-06-12 12:31:58 -04:00
|
|
|
$config = Server::get(IConfig::class);
|
2022-05-02 11:49:32 -04:00
|
|
|
# delete old logfile
|
|
|
|
|
unlink($config->getSystemValue('logfile'));
|
|
|
|
|
|
|
|
|
|
# set format & write log line
|
|
|
|
|
$config->setSystemValue('logdateformat', 'u');
|
|
|
|
|
$this->logFile->write('code', ['something' => 'extra', 'message' => 'Testing logging'], ILogger::ERROR);
|
|
|
|
|
|
|
|
|
|
# read log line
|
|
|
|
|
$handle = @fopen($config->getSystemValue('logfile'), 'r');
|
|
|
|
|
$line = fread($handle, 1000);
|
|
|
|
|
fclose($handle);
|
|
|
|
|
|
|
|
|
|
# check log has data content
|
2024-08-23 09:10:27 -04:00
|
|
|
$values = (array)json_decode($line, true);
|
2022-05-02 11:49:32 -04:00
|
|
|
$this->assertArrayNotHasKey('message', $values['data']);
|
|
|
|
|
$this->assertEquals('extra', $values['data']['something']);
|
|
|
|
|
$this->assertEquals('Testing logging', $values['message']);
|
|
|
|
|
}
|
|
|
|
|
|
2024-09-15 16:32:31 -04:00
|
|
|
public function testMicrosecondsLogTimestamp(): void {
|
2025-06-12 12:31:58 -04:00
|
|
|
$config = Server::get(IConfig::class);
|
2015-09-10 17:02:28 -04:00
|
|
|
# delete old logfile
|
2015-12-02 10:11:38 -05:00
|
|
|
unlink($config->getSystemValue('logfile'));
|
2015-09-10 17:02:28 -04:00
|
|
|
|
|
|
|
|
# set format & write log line
|
2015-12-02 10:11:38 -05:00
|
|
|
$config->setSystemValue('logdateformat', 'u');
|
2018-04-24 16:14:00 -04:00
|
|
|
$this->logFile->write('test', 'message', ILogger::ERROR);
|
2018-04-25 09:22:28 -04:00
|
|
|
|
2015-09-10 17:02:28 -04:00
|
|
|
# read log line
|
2015-12-02 10:11:38 -05:00
|
|
|
$handle = @fopen($config->getSystemValue('logfile'), 'r');
|
2015-09-10 17:02:28 -04:00
|
|
|
$line = fread($handle, 1000);
|
|
|
|
|
fclose($handle);
|
2022-05-02 11:49:32 -04:00
|
|
|
|
2015-09-10 17:02:28 -04:00
|
|
|
# check timestamp has microseconds part
|
2024-08-23 09:10:27 -04:00
|
|
|
$values = (array)json_decode($line);
|
2015-09-10 17:02:28 -04:00
|
|
|
$microseconds = $values['time'];
|
|
|
|
|
$this->assertNotEquals(0, $microseconds);
|
|
|
|
|
}
|
|
|
|
|
}
|