destaticfy Log classes
Signed-off-by: Arthur Schiwon <blizzz@arthur-schiwon.de>
This commit is contained in:
parent
b7e8ab97e7
commit
5fbf184134
|
@ -69,7 +69,7 @@ class ExceptionLoggerPluginTest extends TestCase {
|
|||
});
|
||||
|
||||
$this->server = new Server();
|
||||
$this->logger = new TestLogger(Log\File::class, $config);
|
||||
$this->logger = new TestLogger(new Log\File(\OC::$SERVERROOT.'/data/nextcloud.log'), $config);
|
||||
$this->plugin = new PluginToTest('unit-test', $this->logger);
|
||||
$this->plugin->initialize($this->server);
|
||||
}
|
||||
|
|
|
@ -750,6 +750,9 @@ return array(
|
|||
'OC\\Log\\Errorlog' => $baseDir . '/lib/private/Log/Errorlog.php',
|
||||
'OC\\Log\\ExceptionSerializer' => $baseDir . '/lib/private/Log/ExceptionSerializer.php',
|
||||
'OC\\Log\\File' => $baseDir . '/lib/private/Log/File.php',
|
||||
'OC\\Log\\IFileBased' => $baseDir . '/lib/private/Log/IFileBased.php',
|
||||
'OC\\Log\\IWritable' => $baseDir . '/lib/private/Log/IWritable.php',
|
||||
'OC\\Log\\LogFactory' => $baseDir . '/lib/private/Log/LogFactory.php',
|
||||
'OC\\Log\\Rotate' => $baseDir . '/lib/private/Log/Rotate.php',
|
||||
'OC\\Log\\Syslog' => $baseDir . '/lib/private/Log/Syslog.php',
|
||||
'OC\\Mail\\Attachment' => $baseDir . '/lib/private/Mail/Attachment.php',
|
||||
|
|
|
@ -780,6 +780,9 @@ class ComposerStaticInit53792487c5a8370acc0b06b1a864ff4c
|
|||
'OC\\Log\\Errorlog' => __DIR__ . '/../../..' . '/lib/private/Log/Errorlog.php',
|
||||
'OC\\Log\\ExceptionSerializer' => __DIR__ . '/../../..' . '/lib/private/Log/ExceptionSerializer.php',
|
||||
'OC\\Log\\File' => __DIR__ . '/../../..' . '/lib/private/Log/File.php',
|
||||
'OC\\Log\\IFileBased' => __DIR__ . '/../../..' . '/lib/private/Log/IFileBased.php',
|
||||
'OC\\Log\\IWritable' => __DIR__ . '/../../..' . '/lib/private/Log/IWritable.php',
|
||||
'OC\\Log\\LogFactory' => __DIR__ . '/../../..' . '/lib/private/Log/LogFactory.php',
|
||||
'OC\\Log\\Rotate' => __DIR__ . '/../../..' . '/lib/private/Log/Rotate.php',
|
||||
'OC\\Log\\Syslog' => __DIR__ . '/../../..' . '/lib/private/Log/Syslog.php',
|
||||
'OC\\Mail\\Attachment' => __DIR__ . '/../../..' . '/lib/private/Mail/Attachment.php',
|
||||
|
|
|
@ -38,7 +38,8 @@ namespace OC;
|
|||
use InterfaSys\LogNormalizer\Normalizer;
|
||||
|
||||
use OC\Log\ExceptionSerializer;
|
||||
use OC\Log\File;
|
||||
use OC\Log\IFileBased;
|
||||
use OC\Log\IWritable;
|
||||
use OCP\ILogger;
|
||||
use OCP\Support\CrashReport\IRegistry;
|
||||
use OCP\Util;
|
||||
|
@ -54,7 +55,7 @@ use OCP\Util;
|
|||
*/
|
||||
class Log implements ILogger {
|
||||
|
||||
/** @var string */
|
||||
/** @var IWritable */
|
||||
private $logger;
|
||||
|
||||
/** @var SystemConfig */
|
||||
|
@ -70,27 +71,19 @@ class Log implements ILogger {
|
|||
private $crashReporters;
|
||||
|
||||
/**
|
||||
* @param string $logger The logger that should be used
|
||||
* @param IWritable $logger The logger that should be used
|
||||
* @param SystemConfig $config the system config object
|
||||
* @param Normalizer|null $normalizer
|
||||
* @param IRegistry|null $registry
|
||||
*/
|
||||
public function __construct($logger = null, SystemConfig $config = null, $normalizer = null, IRegistry $registry = null) {
|
||||
public function __construct(IWritable $logger, SystemConfig $config = null, $normalizer = null, IRegistry $registry = null) {
|
||||
// FIXME: Add this for backwards compatibility, should be fixed at some point probably
|
||||
if ($config === null) {
|
||||
$config = \OC::$server->getSystemConfig();
|
||||
}
|
||||
|
||||
$this->config = $config;
|
||||
|
||||
// FIXME: Add this for backwards compatibility, should be fixed at some point probably
|
||||
if ($logger === null) {
|
||||
$logType = $this->config->getValue('log_type', 'file');
|
||||
$this->logger = static::getLogClass($logType);
|
||||
call_user_func([$this->logger, 'init']);
|
||||
} else {
|
||||
$this->logger = $logger;
|
||||
}
|
||||
$this->logger = $logger;
|
||||
if ($normalizer === null) {
|
||||
$this->normalizer = new Normalizer();
|
||||
} else {
|
||||
|
@ -302,7 +295,7 @@ class Log implements ILogger {
|
|||
array_walk($context, [$this->normalizer, 'format']);
|
||||
|
||||
if ($level >= $minLevel) {
|
||||
if ($this->logger !== File::class) {
|
||||
if (!$this->logger instanceof IFileBased) {
|
||||
$data = json_encode($data, JSON_PARTIAL_OUTPUT_ON_ERROR);
|
||||
}
|
||||
$this->writeLog($app, $data, $level);
|
||||
|
@ -320,28 +313,13 @@ class Log implements ILogger {
|
|||
* @param int $level
|
||||
*/
|
||||
protected function writeLog(string $app, $entry, int $level) {
|
||||
call_user_func([$this->logger, 'write'], $app, $entry, $level);
|
||||
$this->logger->write($app, $entry, $level);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $logType
|
||||
* @return string
|
||||
* @internal
|
||||
*/
|
||||
public static function getLogClass(string $logType): string {
|
||||
switch (strtolower($logType)) {
|
||||
case 'errorlog':
|
||||
return \OC\Log\Errorlog::class;
|
||||
case 'syslog':
|
||||
return \OC\Log\Syslog::class;
|
||||
case 'file':
|
||||
return \OC\Log\File::class;
|
||||
|
||||
// Backwards compatibility for old and fallback for unknown log types
|
||||
case 'owncloud':
|
||||
case 'nextcloud':
|
||||
default:
|
||||
return \OC\Log\File::class;
|
||||
public function getLogPath():string {
|
||||
if($this->logger instanceof IFileBased) {
|
||||
return $this->logger->getLogFilePath();
|
||||
}
|
||||
throw new \RuntimeException('Log implementation has no path');
|
||||
}
|
||||
}
|
||||
|
|
|
@ -25,14 +25,7 @@
|
|||
|
||||
namespace OC\Log;
|
||||
|
||||
class Errorlog {
|
||||
|
||||
|
||||
/**
|
||||
* Init class data
|
||||
*/
|
||||
public static function init() {
|
||||
}
|
||||
class Errorlog implements IWritable {
|
||||
|
||||
/**
|
||||
* write a message in the log
|
||||
|
@ -40,7 +33,7 @@ class Errorlog {
|
|||
* @param string $message
|
||||
* @param int $level
|
||||
*/
|
||||
public static function write($app, $message, $level) {
|
||||
public function write($app, $message, $level) {
|
||||
error_log('[owncloud]['.$app.']['.$level.'] '.$message);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -45,28 +45,21 @@ use OCP\ILogger;
|
|||
* Log is saved at data/nextcloud.log (on default)
|
||||
*/
|
||||
|
||||
class File {
|
||||
static protected $logFile;
|
||||
class File implements IWritable, IFileBased {
|
||||
/** @var string */
|
||||
protected $logFile;
|
||||
|
||||
/**
|
||||
* Init class data
|
||||
*/
|
||||
public static function init() {
|
||||
$systemConfig = \OC::$server->getSystemConfig();
|
||||
$defaultLogFile = $systemConfig->getValue("datadirectory", \OC::$SERVERROOT.'/data').'/nextcloud.log';
|
||||
self::$logFile = $systemConfig->getValue("logfile", $defaultLogFile);
|
||||
|
||||
/**
|
||||
* Fall back to default log file if specified logfile does not exist
|
||||
* and can not be created.
|
||||
*/
|
||||
if (!file_exists(self::$logFile)) {
|
||||
if(!is_writable(dirname(self::$logFile))) {
|
||||
self::$logFile = $defaultLogFile;
|
||||
} else {
|
||||
if(!touch(self::$logFile)) {
|
||||
self::$logFile = $defaultLogFile;
|
||||
}
|
||||
public function __construct(string $path, string $fallbackPath = '') {
|
||||
$this->logFile = $path;
|
||||
if (!file_exists($this->logFile)) {
|
||||
if(
|
||||
(
|
||||
!is_writable(dirname($this->logFile))
|
||||
|| !touch($this->logFile)
|
||||
)
|
||||
&& $fallbackPath !== ''
|
||||
) {
|
||||
$this->logFile = $fallbackPath;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -77,7 +70,7 @@ class File {
|
|||
* @param string|array $message
|
||||
* @param int $level
|
||||
*/
|
||||
public static function write($app, $message, $level) {
|
||||
public function write($app, $message, $level) {
|
||||
$config = \OC::$server->getSystemConfig();
|
||||
|
||||
// default to ISO8601
|
||||
|
@ -137,9 +130,9 @@ class File {
|
|||
}
|
||||
}
|
||||
$entry = json_encode($entry, JSON_PARTIAL_OUTPUT_ON_ERROR);
|
||||
$handle = @fopen(self::$logFile, 'a');
|
||||
if ((fileperms(self::$logFile) & 0777) != 0640) {
|
||||
@chmod(self::$logFile, 0640);
|
||||
$handle = @fopen($this->logFile, 'a');
|
||||
if ((fileperms($this->logFile) & 0777) != 0640) {
|
||||
@chmod($this->logFile, 0640);
|
||||
}
|
||||
if ($handle) {
|
||||
fwrite($handle, $entry."\n");
|
||||
|
@ -159,11 +152,10 @@ class File {
|
|||
* @param int $offset
|
||||
* @return array
|
||||
*/
|
||||
public static function getEntries($limit=50, $offset=0) {
|
||||
self::init();
|
||||
public function getEntries($limit=50, $offset=0) {
|
||||
$minLevel = \OC::$server->getSystemConfig()->getValue("loglevel", ILogger::WARN);
|
||||
$entries = array();
|
||||
$handle = @fopen(self::$logFile, 'rb');
|
||||
$handle = @fopen($this->logFile, 'rb');
|
||||
if ($handle) {
|
||||
fseek($handle, 0, SEEK_END);
|
||||
$pos = ftell($handle);
|
||||
|
@ -205,7 +197,7 @@ class File {
|
|||
/**
|
||||
* @return string
|
||||
*/
|
||||
public static function getLogFilePath() {
|
||||
return self::$logFile;
|
||||
public function getLogFilePath() {
|
||||
return $this->logFile;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -0,0 +1,30 @@
|
|||
<?php
|
||||
/**
|
||||
* @copyright Copyright (c) 2018 Arthur Schiwon <blizzz@arthur-schiwon.de>
|
||||
*
|
||||
* @author Arthur Schiwon <blizzz@arthur-schiwon.de>
|
||||
*
|
||||
* @license GNU AGPL version 3 or any later version
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU Affero General Public License as
|
||||
* published by the Free Software Foundation, either version 3 of the
|
||||
* License, or (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU Affero General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Affero General Public License
|
||||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*
|
||||
*/
|
||||
|
||||
namespace OC\Log;
|
||||
|
||||
interface IFileBased {
|
||||
public function getLogFilePath();
|
||||
|
||||
public function getEntries($limit=50, $offset=0);
|
||||
}
|
|
@ -0,0 +1,28 @@
|
|||
<?php
|
||||
/**
|
||||
* @copyright Copyright (c) 2018 Arthur Schiwon <blizzz@arthur-schiwon.de>
|
||||
*
|
||||
* @author Arthur Schiwon <blizzz@arthur-schiwon.de>
|
||||
*
|
||||
* @license GNU AGPL version 3 or any later version
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU Affero General Public License as
|
||||
* published by the Free Software Foundation, either version 3 of the
|
||||
* License, or (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU Affero General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Affero General Public License
|
||||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*
|
||||
*/
|
||||
|
||||
namespace OC\Log;
|
||||
|
||||
interface IWritable {
|
||||
public function write($app, $message, $level);
|
||||
}
|
|
@ -0,0 +1,66 @@
|
|||
<?php
|
||||
/**
|
||||
* @copyright Copyright (c) 2018 Arthur Schiwon <blizzz@arthur-schiwon.de>
|
||||
*
|
||||
* @author Arthur Schiwon <blizzz@arthur-schiwon.de>
|
||||
*
|
||||
* @license GNU AGPL version 3 or any later version
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU Affero General Public License as
|
||||
* published by the Free Software Foundation, either version 3 of the
|
||||
* License, or (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU Affero General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Affero General Public License
|
||||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*
|
||||
*/
|
||||
|
||||
namespace OC\Log;
|
||||
|
||||
use OCP\IServerContainer;
|
||||
|
||||
class LogFactory {
|
||||
/** @var IServerContainer */
|
||||
private $c;
|
||||
|
||||
public function __construct(IServerContainer $c) {
|
||||
$this->c = $c;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param $type
|
||||
* @return \OC\Log\Errorlog|File|\stdClass
|
||||
* @throws \OCP\AppFramework\QueryException
|
||||
*/
|
||||
public function get($type) {
|
||||
switch (strtolower($type)) {
|
||||
case 'errorlog':
|
||||
return new Errorlog();
|
||||
case 'syslog':
|
||||
return $this->c->resolve(Syslog::class);
|
||||
case 'file':
|
||||
return $this->buildLogFile();
|
||||
|
||||
// Backwards compatibility for old and fallback for unknown log types
|
||||
case 'owncloud':
|
||||
case 'nextcloud':
|
||||
default:
|
||||
return $this->buildLogFile();
|
||||
}
|
||||
}
|
||||
|
||||
protected function buildLogFile() {
|
||||
$config = $this->c->getConfig();
|
||||
$defaultLogFile = $config->getSystemValue('datadirectory', \OC::$SERVERROOT.'/data').'/nextcloud.log';
|
||||
$logFile = $config->getSystemValue('logfile', $defaultLogFile);
|
||||
$fallback = $defaultLogFile !== $logFile ? $defaultLogFile : '';
|
||||
|
||||
return new File($logFile, $fallback);
|
||||
}
|
||||
}
|
|
@ -26,22 +26,19 @@
|
|||
namespace OC\Log;
|
||||
|
||||
use OCP\ILogger;
|
||||
use OCP\IConfig;
|
||||
|
||||
class Syslog {
|
||||
static protected $levels = array(
|
||||
class Syslog implements IWritable {
|
||||
static protected $levels = [
|
||||
ILogger::DEBUG => LOG_DEBUG,
|
||||
ILogger::INFO => LOG_INFO,
|
||||
ILogger::WARN => LOG_WARNING,
|
||||
ILogger::ERROR => LOG_ERR,
|
||||
ILogger::FATAL => LOG_CRIT,
|
||||
);
|
||||
];
|
||||
|
||||
/**
|
||||
* Init class data
|
||||
*/
|
||||
public static function init() {
|
||||
openlog(\OC::$server->getSystemConfig()->getValue("syslog_tag", "ownCloud"), LOG_PID | LOG_CONS, LOG_USER);
|
||||
// Close at shutdown
|
||||
public function __construct(IConfig $config) {
|
||||
openlog($config->getSystemValue('syslog_tag', 'ownCloud'), LOG_PID | LOG_CONS, LOG_USER);
|
||||
register_shutdown_function('closelog');
|
||||
}
|
||||
|
||||
|
@ -51,7 +48,7 @@ class Syslog {
|
|||
* @param string $message
|
||||
* @param int $level
|
||||
*/
|
||||
public static function write($app, $message, $level) {
|
||||
public function write($app, $message, $level) {
|
||||
$syslog_level = self::$levels[$level];
|
||||
syslog($syslog_level, '{'.$app.'} '.$message);
|
||||
}
|
||||
|
|
|
@ -87,6 +87,7 @@ use OC\Lock\DBLockingProvider;
|
|||
use OC\Lock\MemcacheLockingProvider;
|
||||
use OC\Lock\NoopLockingProvider;
|
||||
use OC\Lockdown\LockdownManager;
|
||||
use OC\Log\LogFactory;
|
||||
use OC\Mail\Mailer;
|
||||
use OC\Memcache\ArrayCache;
|
||||
use OC\Memcache\Factory;
|
||||
|
@ -546,8 +547,8 @@ class Server extends ServerContainer implements IServerContainer {
|
|||
|
||||
$this->registerService(\OCP\ILogger::class, function (Server $c) {
|
||||
$logType = $c->query('AllConfig')->getSystemValue('log_type', 'file');
|
||||
$logger = Log::getLogClass($logType);
|
||||
call_user_func(array($logger, 'init'));
|
||||
$factory = new LogFactory($c);
|
||||
$logger = $factory->get($logType);
|
||||
$config = $this->getSystemConfig();
|
||||
$registry = $c->query(\OCP\Support\CrashReport\IRegistry::class);
|
||||
|
||||
|
|
|
@ -26,8 +26,11 @@
|
|||
|
||||
namespace OC\Settings\Controller;
|
||||
|
||||
use OC\Log;
|
||||
use OCP\AppFramework\Controller;
|
||||
use OCP\AppFramework\Http\StreamResponse;
|
||||
use OCP\ILogger;
|
||||
use OCP\IRequest;
|
||||
|
||||
/**
|
||||
* Class LogSettingsController
|
||||
|
@ -35,6 +38,15 @@ use OCP\AppFramework\Http\StreamResponse;
|
|||
* @package OC\Settings\Controller
|
||||
*/
|
||||
class LogSettingsController extends Controller {
|
||||
|
||||
/** @var Log */
|
||||
private $log;
|
||||
|
||||
public function __construct(string $appName, IRequest $request, ILogger $logger) {
|
||||
parent::__construct($appName, $request);
|
||||
$this->log = $logger;
|
||||
}
|
||||
|
||||
/**
|
||||
* download logfile
|
||||
*
|
||||
|
@ -43,7 +55,10 @@ class LogSettingsController extends Controller {
|
|||
* @return StreamResponse
|
||||
*/
|
||||
public function download() {
|
||||
$resp = new StreamResponse(\OC\Log\File::getLogFilePath());
|
||||
if(!$this->log instanceof Log) {
|
||||
throw new \UnexpectedValueException('Log file not available');
|
||||
}
|
||||
$resp = new StreamResponse($this->log->getLogPath());
|
||||
$resp->addHeader('Content-Type', 'application/octet-stream');
|
||||
$resp->addHeader('Content-Disposition', 'attachment; filename="nextcloud.log"');
|
||||
return $resp;
|
||||
|
|
|
@ -31,6 +31,9 @@ class FileTest extends TestCase
|
|||
private $restore_logfile;
|
||||
private $restore_logdateformat;
|
||||
|
||||
/** @var File */
|
||||
protected $logFile;
|
||||
|
||||
protected function setUp() {
|
||||
parent::setUp();
|
||||
$config = \OC::$server->getConfig();
|
||||
|
@ -38,7 +41,7 @@ class FileTest extends TestCase
|
|||
$this->restore_logdateformat = $config->getSystemValue('logdateformat');
|
||||
|
||||
$config->setSystemValue("logfile", $config->getSystemValue('datadirectory') . "/logtest");
|
||||
File::init();
|
||||
$this->logFile = new File($config->getSystemValue('datadirectory') . '/logtest');
|
||||
}
|
||||
protected function tearDown() {
|
||||
$config = \OC::$server->getConfig();
|
||||
|
@ -51,8 +54,8 @@ class FileTest extends TestCase
|
|||
$config->getSystemValue("logdateformat", $this->restore_logdateformat);
|
||||
} else {
|
||||
$config->deleteSystemValue("logdateformat");
|
||||
}
|
||||
File::init();
|
||||
}
|
||||
$this->logFile = new File($this->restore_logfile);
|
||||
parent::tearDown();
|
||||
}
|
||||
|
||||
|
@ -63,7 +66,7 @@ class FileTest extends TestCase
|
|||
|
||||
# set format & write log line
|
||||
$config->setSystemValue('logdateformat', 'u');
|
||||
File::write('test', 'message', ILogger::ERROR);
|
||||
$this->logFile->write('test', 'message', ILogger::ERROR);
|
||||
|
||||
# read log line
|
||||
$handle = @fopen($config->getSystemValue('logfile'), 'r');
|
||||
|
|
|
@ -0,0 +1,149 @@
|
|||
<?php
|
||||
/**
|
||||
* @copyright Copyright (c) 2018 Arthur Schiwon <blizzz@arthur-schiwon.de>
|
||||
*
|
||||
* @author Arthur Schiwon <blizzz@arthur-schiwon.de>
|
||||
*
|
||||
* @license GNU AGPL version 3 or any later version
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU Affero General Public License as
|
||||
* published by the Free Software Foundation, either version 3 of the
|
||||
* License, or (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU Affero General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Affero General Public License
|
||||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*
|
||||
*/
|
||||
|
||||
namespace Test\Log;
|
||||
use OC\Log\Errorlog;
|
||||
use OC\Log\File;
|
||||
use OC\Log\LogFactory;
|
||||
use OC\Log\Syslog;
|
||||
use OCP\IConfig;
|
||||
use OCP\IServerContainer;
|
||||
use Test\TestCase;
|
||||
|
||||
/**
|
||||
* Class LogFactoryTest
|
||||
*
|
||||
* @package Test\Log
|
||||
*/
|
||||
class LogFactoryTest extends TestCase {
|
||||
/** @var IServerContainer|\PHPUnit_Framework_MockObject_MockObject */
|
||||
protected $c;
|
||||
|
||||
/** @var LogFactory */
|
||||
protected $factory;
|
||||
|
||||
protected function setUp() {
|
||||
parent::setUp();
|
||||
|
||||
$this->c = $this->createMock(IServerContainer::class);
|
||||
|
||||
$this->factory = new LogFactory($this->c);
|
||||
}
|
||||
|
||||
public function fileTypeProvider(): array {
|
||||
return [
|
||||
[
|
||||
'file'
|
||||
],
|
||||
[
|
||||
'nextcloud'
|
||||
],
|
||||
[
|
||||
'owncloud'
|
||||
],
|
||||
[
|
||||
'krzxkyr_default'
|
||||
]
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $type
|
||||
* @dataProvider fileTypeProvider
|
||||
* @throws \OCP\AppFramework\QueryException
|
||||
*/
|
||||
public function testFile(string $type) {
|
||||
$datadir = \OC::$SERVERROOT.'/data';
|
||||
$defaultLog = $datadir . '/nextcloud.log';
|
||||
|
||||
$config = $this->createMock(IConfig::class);
|
||||
$config->expects($this->exactly(2))
|
||||
->method('getSystemValue')
|
||||
->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);
|
||||
}
|
||||
|
||||
public function logFilePathProvider():array {
|
||||
return [
|
||||
[
|
||||
'/dev/null',
|
||||
'/dev/null'
|
||||
],
|
||||
[
|
||||
'/xdev/youshallfallback',
|
||||
\OC::$SERVERROOT.'/data/nextcloud.log'
|
||||
]
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* @dataProvider logFilePathProvider
|
||||
* @throws \OCP\AppFramework\QueryException
|
||||
*/
|
||||
public function testFileCustomPath($path, $expected) {
|
||||
$datadir = \OC::$SERVERROOT.'/data';
|
||||
$defaultLog = $datadir . '/nextcloud.log';
|
||||
|
||||
$config = $this->createMock(IConfig::class);
|
||||
$config->expects($this->exactly(2))
|
||||
->method('getSystemValue')
|
||||
->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());
|
||||
}
|
||||
|
||||
/**
|
||||
* @throws \OCP\AppFramework\QueryException
|
||||
*/
|
||||
public function testErrorLog() {
|
||||
$log = $this->factory->get('errorlog');
|
||||
$this->assertInstanceOf(Errorlog::class, $log);
|
||||
}
|
||||
|
||||
/**
|
||||
* @throws \OCP\AppFramework\QueryException
|
||||
*/
|
||||
public function testSystemLog() {
|
||||
$this->c->expects($this->once())
|
||||
->method('resolve')
|
||||
->with(Syslog::class)
|
||||
->willReturn($this->createMock(Syslog::class));
|
||||
|
||||
$log = $this->factory->get('syslog');
|
||||
$this->assertInstanceOf(Syslog::class, $log);
|
||||
}
|
||||
}
|
|
@ -11,7 +11,7 @@ namespace Test;
|
|||
use OC\Log;
|
||||
use OCP\ILogger;
|
||||
|
||||
class LoggerTest extends TestCase {
|
||||
class LoggerTest extends TestCase implements Log\IWritable {
|
||||
|
||||
/** @var \OC\SystemConfig|\PHPUnit_Framework_MockObject_MockObject */
|
||||
private $config;
|
||||
|
@ -23,15 +23,15 @@ class LoggerTest extends TestCase {
|
|||
private $logger;
|
||||
|
||||
/** @var array */
|
||||
static private $logs = array();
|
||||
private $logs = [];
|
||||
|
||||
protected function setUp() {
|
||||
parent::setUp();
|
||||
|
||||
self::$logs = array();
|
||||
$this->logs = [];
|
||||
$this->config = $this->createMock(\OC\SystemConfig::class);
|
||||
$this->registry = $this->createMock(\OCP\Support\CrashReport\IRegistry::class);
|
||||
$this->logger = new Log('Test\LoggerTest', $this->config, null, $this->registry);
|
||||
$this->logger = new Log($this, $this->config, null, $this->registry);
|
||||
}
|
||||
|
||||
public function testInterpolation() {
|
||||
|
@ -63,11 +63,11 @@ class LoggerTest extends TestCase {
|
|||
}
|
||||
|
||||
private function getLogs() {
|
||||
return self::$logs;
|
||||
return $this->logs;
|
||||
}
|
||||
|
||||
public static function write($app, $message, $level) {
|
||||
self::$logs[]= "$level $message";
|
||||
public function write($app, $message, $level) {
|
||||
$this->logs[]= "$level $message";
|
||||
}
|
||||
|
||||
public function userAndPasswordData() {
|
||||
|
@ -202,23 +202,4 @@ class LoggerTest extends TestCase {
|
|||
$this->assertContains('*** sensitive parameters replaced ***', $logLine);
|
||||
}
|
||||
}
|
||||
|
||||
public function dataGetLogClass() {
|
||||
return [
|
||||
['file', \OC\Log\File::class],
|
||||
['errorlog', \OC\Log\Errorlog::class],
|
||||
['syslog', \OC\Log\Syslog::class],
|
||||
|
||||
['owncloud', \OC\Log\File::class],
|
||||
['nextcloud', \OC\Log\File::class],
|
||||
['foobar', \OC\Log\File::class],
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* @dataProvider dataGetLogClass
|
||||
*/
|
||||
public function testGetLogClass($type, $class) {
|
||||
$this->assertEquals($class, Log::getLogClass($type));
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue