mirror of
https://github.com/nextcloud/server.git
synced 2026-05-28 04:32:30 -04:00
use SystemConfig, less dependencies, and not publicly needed
Signed-off-by: Arthur Schiwon <blizzz@arthur-schiwon.de>
This commit is contained in:
parent
b841a477c6
commit
aff5fe68b3
6 changed files with 41 additions and 55 deletions
|
|
@ -28,7 +28,6 @@ use OC\SystemConfig;
|
|||
use OCA\DAV\Connector\Sabre\Exception\InvalidPath;
|
||||
use OCA\DAV\Connector\Sabre\ExceptionLoggerPlugin as PluginToTest;
|
||||
use OC\Log;
|
||||
use OCP\IConfig;
|
||||
use PHPUnit_Framework_MockObject_MockObject;
|
||||
use Sabre\DAV\Exception\NotFound;
|
||||
use Sabre\DAV\Exception\ServiceUnavailable;
|
||||
|
|
@ -70,7 +69,7 @@ class ExceptionLoggerPluginTest extends TestCase {
|
|||
});
|
||||
|
||||
$this->server = new Server();
|
||||
$this->logger = new TestLogger(new Log\File(\OC::$SERVERROOT.'/data/nextcloud.log', '', $this->createMock(IConfig::class)), $config);
|
||||
$this->logger = new TestLogger(new Log\File(\OC::$SERVERROOT.'/data/nextcloud.log', '', $config), $config);
|
||||
$this->plugin = new PluginToTest('unit-test', $this->logger);
|
||||
$this->plugin->initialize($this->server);
|
||||
}
|
||||
|
|
|
|||
|
|
@ -36,10 +36,9 @@
|
|||
*/
|
||||
|
||||
namespace OC\Log;
|
||||
use OCP\IConfig;
|
||||
use OC\SystemConfig;
|
||||
use OCP\Log\IFileBased;
|
||||
use OCP\Log\IWriter;
|
||||
|
||||
use OCP\ILogger;
|
||||
|
||||
/**
|
||||
|
|
@ -51,10 +50,10 @@ use OCP\ILogger;
|
|||
class File implements IWriter, IFileBased {
|
||||
/** @var string */
|
||||
protected $logFile;
|
||||
/** @var IConfig */
|
||||
/** @var SystemConfig */
|
||||
private $config;
|
||||
|
||||
public function __construct(string $path, string $fallbackPath = '', IConfig $config) {
|
||||
public function __construct(string $path, string $fallbackPath = '', SystemConfig $config) {
|
||||
$this->logFile = $path;
|
||||
if (!file_exists($this->logFile)) {
|
||||
if(
|
||||
|
|
@ -78,8 +77,8 @@ class File implements IWriter, IFileBased {
|
|||
*/
|
||||
public function write(string $app, $message, int $level) {
|
||||
// default to ISO8601
|
||||
$format = $this->config->getSystemValue('logdateformat', \DateTime::ATOM);
|
||||
$logTimeZone = $this->config->getSystemValue('logtimezone', 'UTC');
|
||||
$format = $this->config->getValue('logdateformat', \DateTime::ATOM);
|
||||
$logTimeZone = $this->config->getValue('logtimezone', 'UTC');
|
||||
try {
|
||||
$timezone = new \DateTimeZone($logTimeZone);
|
||||
} catch (\Exception $e) {
|
||||
|
|
@ -99,7 +98,7 @@ class File implements IWriter, IFileBased {
|
|||
$time = $time->format($format);
|
||||
$url = ($request->getRequestUri() !== '') ? $request->getRequestUri() : '--';
|
||||
$method = is_string($request->getMethod()) ? $request->getMethod() : '--';
|
||||
if($this->config->getSystemValue('installed', false)) {
|
||||
if($this->config->getValue('installed', false)) {
|
||||
$user = \OC_User::getUser() ? \OC_User::getUser() : '--';
|
||||
} else {
|
||||
$user = '--';
|
||||
|
|
@ -108,7 +107,7 @@ class File implements IWriter, IFileBased {
|
|||
if ($userAgent === '') {
|
||||
$userAgent = '--';
|
||||
}
|
||||
$version = $this->config->getSystemValue('version', '');
|
||||
$version = $this->config->getValue('version', '');
|
||||
$entry = compact(
|
||||
'reqId',
|
||||
'level',
|
||||
|
|
@ -157,7 +156,7 @@ class File implements IWriter, IFileBased {
|
|||
* @return array
|
||||
*/
|
||||
public function getEntries(int $limit=50, int $offset=0):array {
|
||||
$minLevel = $this->config->getSystemValue("loglevel", ILogger::WARN);
|
||||
$minLevel = $this->config->getValue("loglevel", ILogger::WARN);
|
||||
$entries = array();
|
||||
$handle = @fopen($this->logFile, 'rb');
|
||||
if ($handle) {
|
||||
|
|
|
|||
|
|
@ -23,8 +23,8 @@
|
|||
|
||||
namespace OC\Log;
|
||||
|
||||
use OC\AllConfig;
|
||||
use OC\Log;
|
||||
use OC\SystemConfig;
|
||||
use OCP\ILogger;
|
||||
use OCP\IServerContainer;
|
||||
use OCP\Log\ILogFactory;
|
||||
|
|
@ -33,14 +33,15 @@ use OCP\Log\IWriter;
|
|||
class LogFactory implements ILogFactory {
|
||||
/** @var IServerContainer */
|
||||
private $c;
|
||||
/** @var SystemConfig */
|
||||
private $systemConfig;
|
||||
|
||||
public function __construct(IServerContainer $c) {
|
||||
public function __construct(IServerContainer $c, SystemConfig $systemConfig) {
|
||||
$this->c = $c;
|
||||
$this->systemConfig = $systemConfig;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param $type
|
||||
* @return \OC\Log\Errorlog|File|\stdClass
|
||||
* @throws \OCP\AppFramework\QueryException
|
||||
*/
|
||||
public function get(string $type):IWriter {
|
||||
|
|
@ -61,24 +62,17 @@ class LogFactory implements ILogFactory {
|
|||
}
|
||||
|
||||
public function getCustomLogger(string $path):ILogger {
|
||||
$systemConfig = null;
|
||||
$iconfig = $this->c->getConfig();
|
||||
if($iconfig instanceof AllConfig) {
|
||||
// Log is bound to SystemConfig, but fetches it from \OC::$server if not supplied
|
||||
$systemConfig = $iconfig->getSystemConfig();
|
||||
}
|
||||
$log = $this->buildLogFile($path);
|
||||
return new Log($log, $systemConfig);
|
||||
return new Log($log, $this->systemConfig);
|
||||
}
|
||||
|
||||
protected function buildLogFile(string $logFile = ''):File {
|
||||
$config = $this->c->getConfig();
|
||||
$defaultLogFile = $config->getSystemValue('datadirectory', \OC::$SERVERROOT.'/data').'/nextcloud.log';
|
||||
$defaultLogFile = $this->systemConfig->getValue('datadirectory', \OC::$SERVERROOT.'/data').'/nextcloud.log';
|
||||
if($logFile === '') {
|
||||
$logFile = $config->getSystemValue('logfile', $defaultLogFile);
|
||||
$logFile = $this->systemConfig->getValue('logfile', $defaultLogFile);
|
||||
}
|
||||
$fallback = $defaultLogFile !== $logFile ? $defaultLogFile : '';
|
||||
|
||||
return new File($logFile, $fallback, $config);
|
||||
return new File($logFile, $fallback, $this->systemConfig);
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -548,7 +548,7 @@ class Server extends ServerContainer implements IServerContainer {
|
|||
|
||||
$this->registerService(\OCP\ILogger::class, function (Server $c) {
|
||||
$logType = $c->query('AllConfig')->getSystemValue('log_type', 'file');
|
||||
$factory = new LogFactory($c);
|
||||
$factory = new LogFactory($c, $this->getSystemConfig());
|
||||
$logger = $factory->get($logType);
|
||||
$registry = $c->query(\OCP\Support\CrashReport\IRegistry::class);
|
||||
|
||||
|
|
@ -557,9 +557,8 @@ class Server extends ServerContainer implements IServerContainer {
|
|||
$this->registerAlias('Logger', \OCP\ILogger::class);
|
||||
|
||||
$this->registerService(ILogFactory::class, function (Server $c) {
|
||||
return new LogFactory($c);
|
||||
return new LogFactory($c, $this->getSystemConfig());
|
||||
});
|
||||
$this->registerAlias('LogFactory', ILogFactory::class);
|
||||
|
||||
$this->registerService(\OCP\BackgroundJob\IJobList::class, function (Server $c) {
|
||||
$config = $c->getConfig();
|
||||
|
|
@ -1539,7 +1538,7 @@ class Server extends ServerContainer implements IServerContainer {
|
|||
* @throws \OCP\AppFramework\QueryException
|
||||
*/
|
||||
public function getLogFactory() {
|
||||
return $this->query('LogFactory');
|
||||
return $this->query(ILogFactory::class);
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
|
|||
|
|
@ -36,24 +36,24 @@ class FileTest extends TestCase
|
|||
|
||||
protected function setUp() {
|
||||
parent::setUp();
|
||||
$config = \OC::$server->getConfig();
|
||||
$this->restore_logfile = $config->getSystemValue("logfile");
|
||||
$this->restore_logdateformat = $config->getSystemValue('logdateformat');
|
||||
$config = \OC::$server->getSystemConfig();
|
||||
$this->restore_logfile = $config->getValue("logfile");
|
||||
$this->restore_logdateformat = $config->getValue('logdateformat');
|
||||
|
||||
$config->setSystemValue("logfile", $config->getSystemValue('datadirectory') . "/logtest");
|
||||
$this->logFile = new File($config->getSystemValue('datadirectory') . '/logtest', '', $config);
|
||||
$config->setValue("logfile", $config->getValue('datadirectory') . "/logtest.log");
|
||||
$this->logFile = new File($config->getValue('datadirectory') . '/logtest.log', '', $config);
|
||||
}
|
||||
protected function tearDown() {
|
||||
$config = \OC::$server->getConfig();
|
||||
$config = \OC::$server->getSystemConfig();
|
||||
if (isset($this->restore_logfile)) {
|
||||
$config->getSystemValue("logfile", $this->restore_logfile);
|
||||
$config->getValue("logfile", $this->restore_logfile);
|
||||
} else {
|
||||
$config->deleteSystemValue("logfile");
|
||||
$config->deleteValue("logfile");
|
||||
}
|
||||
if (isset($this->restore_logdateformat)) {
|
||||
$config->getSystemValue("logdateformat", $this->restore_logdateformat);
|
||||
$config->getValue("logdateformat", $this->restore_logdateformat);
|
||||
} else {
|
||||
$config->deleteSystemValue("logdateformat");
|
||||
$config->deleteValue("logdateformat");
|
||||
}
|
||||
$this->logFile = new File($this->restore_logfile, '', $config);
|
||||
parent::tearDown();
|
||||
|
|
|
|||
|
|
@ -26,6 +26,7 @@ use OC\Log\Errorlog;
|
|||
use OC\Log\File;
|
||||
use OC\Log\LogFactory;
|
||||
use OC\Log\Syslog;
|
||||
use OC\SystemConfig;
|
||||
use OCP\IConfig;
|
||||
use OCP\IServerContainer;
|
||||
use Test\TestCase;
|
||||
|
|
@ -42,12 +43,16 @@ class LogFactoryTest extends TestCase {
|
|||
/** @var LogFactory */
|
||||
protected $factory;
|
||||
|
||||
/** @var SystemConfig|\PHPUnit_Framework_MockObject_MockObject */
|
||||
protected $systemConfig;
|
||||
|
||||
protected function setUp() {
|
||||
parent::setUp();
|
||||
|
||||
$this->c = $this->createMock(IServerContainer::class);
|
||||
$this->systemConfig = $this->createMock(SystemConfig::class);
|
||||
|
||||
$this->factory = new LogFactory($this->c);
|
||||
$this->factory = new LogFactory($this->c, $this->systemConfig);
|
||||
}
|
||||
|
||||
public function fileTypeProvider(): array {
|
||||
|
|
@ -76,16 +81,11 @@ class LogFactoryTest extends TestCase {
|
|||
$datadir = \OC::$SERVERROOT.'/data';
|
||||
$defaultLog = $datadir . '/nextcloud.log';
|
||||
|
||||
$config = $this->createMock(IConfig::class);
|
||||
$config->expects($this->exactly(2))
|
||||
->method('getSystemValue')
|
||||
$this->systemConfig->expects($this->exactly(2))
|
||||
->method('getValue')
|
||||
->withConsecutive(['datadirectory', $datadir], ['logfile', $defaultLog])
|
||||
->willReturnOnConsecutiveCalls($datadir, $defaultLog);
|
||||
|
||||
$this->c->expects($this->any())
|
||||
->method('getConfig')
|
||||
->willReturn($config);
|
||||
|
||||
$log = $this->factory->get($type);
|
||||
$this->assertInstanceOf(File::class, $log);
|
||||
}
|
||||
|
|
@ -111,16 +111,11 @@ class LogFactoryTest extends TestCase {
|
|||
$datadir = \OC::$SERVERROOT.'/data';
|
||||
$defaultLog = $datadir . '/nextcloud.log';
|
||||
|
||||
$config = $this->createMock(IConfig::class);
|
||||
$config->expects($this->exactly(2))
|
||||
->method('getSystemValue')
|
||||
$this->systemConfig->expects($this->exactly(2))
|
||||
->method('getValue')
|
||||
->withConsecutive(['datadirectory', $datadir], ['logfile', $defaultLog])
|
||||
->willReturnOnConsecutiveCalls($datadir, $path);
|
||||
|
||||
$this->c->expects($this->any())
|
||||
->method('getConfig')
|
||||
->willReturn($config);
|
||||
|
||||
$log = $this->factory->get('file');
|
||||
$this->assertInstanceOf(File::class, $log);
|
||||
$this->assertSame($expected, $log->getLogFilePath());
|
||||
|
|
|
|||
Loading…
Reference in a new issue