diff options
-rw-r--r-- | apps/dav/tests/unit/Connector/Sabre/ExceptionLoggerPluginTest.php | 3 | ||||
-rw-r--r-- | lib/private/Log/File.php | 17 | ||||
-rw-r--r-- | lib/private/Log/LogFactory.php | 24 | ||||
-rw-r--r-- | lib/private/Server.php | 7 | ||||
-rw-r--r-- | tests/lib/Log/FileTest.php | 20 | ||||
-rw-r--r-- | tests/lib/Log/LogFactoryTest.php | 25 |
6 files changed, 41 insertions, 55 deletions
diff --git a/apps/dav/tests/unit/Connector/Sabre/ExceptionLoggerPluginTest.php b/apps/dav/tests/unit/Connector/Sabre/ExceptionLoggerPluginTest.php index 7bab41ec4ae..1de9333207f 100644 --- a/apps/dav/tests/unit/Connector/Sabre/ExceptionLoggerPluginTest.php +++ b/apps/dav/tests/unit/Connector/Sabre/ExceptionLoggerPluginTest.php @@ -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); } diff --git a/lib/private/Log/File.php b/lib/private/Log/File.php index 86f57f78fda..597cb54e402 100644 --- a/lib/private/Log/File.php +++ b/lib/private/Log/File.php @@ -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) { diff --git a/lib/private/Log/LogFactory.php b/lib/private/Log/LogFactory.php index 63f08a32320..9b9d12abfa8 100644 --- a/lib/private/Log/LogFactory.php +++ b/lib/private/Log/LogFactory.php @@ -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); } } diff --git a/lib/private/Server.php b/lib/private/Server.php index c744ba37ded..a879c65bb9b 100644 --- a/lib/private/Server.php +++ b/lib/private/Server.php @@ -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); } /** diff --git a/tests/lib/Log/FileTest.php b/tests/lib/Log/FileTest.php index 0c50c6c08ba..d5e550a7e8d 100644 --- a/tests/lib/Log/FileTest.php +++ b/tests/lib/Log/FileTest.php @@ -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(); diff --git a/tests/lib/Log/LogFactoryTest.php b/tests/lib/Log/LogFactoryTest.php index 8c1b7f08c74..08139f54df4 100644 --- a/tests/lib/Log/LogFactoryTest.php +++ b/tests/lib/Log/LogFactoryTest.php @@ -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()); |