Merge pull request #9293 from nextcloud/feature/9166/custom-auditlogfile

option for a seperate audit log file
This commit is contained in:
Morris Jobke 2018-05-02 16:16:28 +02:00 committed by GitHub
commit 2aa108000c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
24 changed files with 683 additions and 194 deletions

View File

@ -17,4 +17,7 @@
<dependencies> <dependencies>
<nextcloud min-version="14" max-version="14" /> <nextcloud min-version="14" max-version="14" />
</dependencies> </dependencies>
<background-jobs>
<job>OCA\AdminAudit\BackgroundJobs\Rotate</job>
</background-jobs>
</info> </info>

View File

@ -18,4 +18,5 @@ return array(
'OCA\\AdminAudit\\Actions\\UserManagement' => $baseDir . '/../lib/Actions/UserManagement.php', 'OCA\\AdminAudit\\Actions\\UserManagement' => $baseDir . '/../lib/Actions/UserManagement.php',
'OCA\\AdminAudit\\Actions\\Versions' => $baseDir . '/../lib/Actions/Versions.php', 'OCA\\AdminAudit\\Actions\\Versions' => $baseDir . '/../lib/Actions/Versions.php',
'OCA\\AdminAudit\\AppInfo\\Application' => $baseDir . '/../lib/AppInfo/Application.php', 'OCA\\AdminAudit\\AppInfo\\Application' => $baseDir . '/../lib/AppInfo/Application.php',
'OCA\\AdminAudit\\BackgroundJobs\\Rotate' => $baseDir . '/../lib/BackgroundJobs/Rotate.php',
); );

View File

@ -33,6 +33,7 @@ class ComposerStaticInitAdminAudit
'OCA\\AdminAudit\\Actions\\UserManagement' => __DIR__ . '/..' . '/../lib/Actions/UserManagement.php', 'OCA\\AdminAudit\\Actions\\UserManagement' => __DIR__ . '/..' . '/../lib/Actions/UserManagement.php',
'OCA\\AdminAudit\\Actions\\Versions' => __DIR__ . '/..' . '/../lib/Actions/Versions.php', 'OCA\\AdminAudit\\Actions\\Versions' => __DIR__ . '/..' . '/../lib/Actions/Versions.php',
'OCA\\AdminAudit\\AppInfo\\Application' => __DIR__ . '/..' . '/../lib/AppInfo/Application.php', 'OCA\\AdminAudit\\AppInfo\\Application' => __DIR__ . '/..' . '/../lib/AppInfo/Application.php',
'OCA\\AdminAudit\\BackgroundJobs\\Rotate' => __DIR__ . '/..' . '/../lib/BackgroundJobs/Rotate.php',
); );
public static function getInitializer(ClassLoader $loader) public static function getInitializer(ClassLoader $loader)

View File

@ -53,8 +53,26 @@ use OCP\Share;
class Application extends App { class Application extends App {
/** @var ILogger */
protected $logger;
public function __construct() { public function __construct() {
parent::__construct('admin_audit'); parent::__construct('admin_audit');
$this->initLogger();
}
public function initLogger() {
$c = $this->getContainer()->getServer();
$config = $c->getConfig();
$default = $config->getSystemValue('datadirectory', \OC::$SERVERROOT . '/data') . '/audit.log';
$logFile = $config->getAppValue('admin_audit', 'logfile', $default);
if($logFile === null) {
$this->logger = $c->getLogger();
return;
}
$this->logger = $c->getLogFactory()->getCustomLogger($logFile);
} }
public function register() { public function register() {
@ -65,26 +83,24 @@ class Application extends App {
* Register hooks in order to log them * Register hooks in order to log them
*/ */
protected function registerHooks() { protected function registerHooks() {
$logger = $this->getContainer()->getServer()->getLogger(); $this->userManagementHooks();
$this->groupHooks();
$this->authHooks();
$this->userManagementHooks($logger); $this->consoleHooks();
$this->groupHooks($logger); $this->appHooks();
$this->authHooks($logger);
$this->consoleHooks($logger); $this->sharingHooks();
$this->appHooks($logger);
$this->sharingHooks($logger); $this->fileHooks();
$this->trashbinHooks();
$this->versionsHooks();
$this->fileHooks($logger); $this->securityHooks();
$this->trashbinHooks($logger);
$this->versionsHooks($logger);
$this->securityHooks($logger);
} }
protected function userManagementHooks(ILogger $logger) { protected function userManagementHooks() {
$userActions = new UserManagement($logger); $userActions = new UserManagement($this->logger);
Util::connectHook('OC_User', 'post_createUser', $userActions, 'create'); Util::connectHook('OC_User', 'post_createUser', $userActions, 'create');
Util::connectHook('OC_User', 'post_deleteUser', $userActions, 'delete'); Util::connectHook('OC_User', 'post_deleteUser', $userActions, 'delete');
@ -97,8 +113,8 @@ class Application extends App {
$userSession->listen('\OC\User', 'postUnassignedUserId', [$userActions, 'unassign']); $userSession->listen('\OC\User', 'postUnassignedUserId', [$userActions, 'unassign']);
} }
protected function groupHooks(ILogger $logger) { protected function groupHooks() {
$groupActions = new GroupManagement($logger); $groupActions = new GroupManagement($this->logger);
/** @var IGroupManager|Manager $groupManager */ /** @var IGroupManager|Manager $groupManager */
$groupManager = $this->getContainer()->getServer()->getGroupManager(); $groupManager = $this->getContainer()->getServer()->getGroupManager();
@ -108,8 +124,8 @@ class Application extends App {
$groupManager->listen('\OC\Group', 'postCreate', [$groupActions, 'createGroup']); $groupManager->listen('\OC\Group', 'postCreate', [$groupActions, 'createGroup']);
} }
protected function sharingHooks(ILogger $logger) { protected function sharingHooks() {
$shareActions = new Sharing($logger); $shareActions = new Sharing($this->logger);
Util::connectHook(Share::class, 'post_shared', $shareActions, 'shared'); Util::connectHook(Share::class, 'post_shared', $shareActions, 'shared');
Util::connectHook(Share::class, 'post_unshare', $shareActions, 'unshare'); Util::connectHook(Share::class, 'post_unshare', $shareActions, 'unshare');
@ -119,42 +135,42 @@ class Application extends App {
Util::connectHook(Share::class, 'share_link_access', $shareActions, 'shareAccessed'); Util::connectHook(Share::class, 'share_link_access', $shareActions, 'shareAccessed');
} }
protected function authHooks(ILogger $logger) { protected function authHooks() {
$authActions = new Auth($logger); $authActions = new Auth($this->logger);
Util::connectHook('OC_User', 'pre_login', $authActions, 'loginAttempt'); Util::connectHook('OC_User', 'pre_login', $authActions, 'loginAttempt');
Util::connectHook('OC_User', 'post_login', $authActions, 'loginSuccessful'); Util::connectHook('OC_User', 'post_login', $authActions, 'loginSuccessful');
Util::connectHook('OC_User', 'logout', $authActions, 'logout'); Util::connectHook('OC_User', 'logout', $authActions, 'logout');
} }
protected function appHooks(ILogger $logger) { protected function appHooks() {
$eventDispatcher = $this->getContainer()->getServer()->getEventDispatcher(); $eventDispatcher = $this->getContainer()->getServer()->getEventDispatcher();
$eventDispatcher->addListener(ManagerEvent::EVENT_APP_ENABLE, function(ManagerEvent $event) use ($logger) { $eventDispatcher->addListener(ManagerEvent::EVENT_APP_ENABLE, function(ManagerEvent $event) {
$appActions = new AppManagement($logger); $appActions = new AppManagement($this->logger);
$appActions->enableApp($event->getAppID()); $appActions->enableApp($event->getAppID());
}); });
$eventDispatcher->addListener(ManagerEvent::EVENT_APP_ENABLE_FOR_GROUPS, function(ManagerEvent $event) use ($logger) { $eventDispatcher->addListener(ManagerEvent::EVENT_APP_ENABLE_FOR_GROUPS, function(ManagerEvent $event) {
$appActions = new AppManagement($logger); $appActions = new AppManagement($this->logger);
$appActions->enableAppForGroups($event->getAppID(), $event->getGroups()); $appActions->enableAppForGroups($event->getAppID(), $event->getGroups());
}); });
$eventDispatcher->addListener(ManagerEvent::EVENT_APP_DISABLE, function(ManagerEvent $event) use ($logger) { $eventDispatcher->addListener(ManagerEvent::EVENT_APP_DISABLE, function(ManagerEvent $event) {
$appActions = new AppManagement($logger); $appActions = new AppManagement($this->logger);
$appActions->disableApp($event->getAppID()); $appActions->disableApp($event->getAppID());
}); });
} }
protected function consoleHooks(ILogger $logger) { protected function consoleHooks() {
$eventDispatcher = $this->getContainer()->getServer()->getEventDispatcher(); $eventDispatcher = $this->getContainer()->getServer()->getEventDispatcher();
$eventDispatcher->addListener(ConsoleEvent::EVENT_RUN, function(ConsoleEvent $event) use ($logger) { $eventDispatcher->addListener(ConsoleEvent::EVENT_RUN, function(ConsoleEvent $event) {
$appActions = new Console($logger); $appActions = new Console($this->logger);
$appActions->runCommand($event->getArguments()); $appActions->runCommand($event->getArguments());
}); });
} }
protected function fileHooks(ILogger $logger) { protected function fileHooks() {
$fileActions = new Files($logger); $fileActions = new Files($this->logger);
$eventDispatcher = $this->getContainer()->getServer()->getEventDispatcher(); $eventDispatcher = $this->getContainer()->getServer()->getEventDispatcher();
$eventDispatcher->addListener( $eventDispatcher->addListener(
IPreview::EVENT, IPreview::EVENT,
@ -215,26 +231,26 @@ class Application extends App {
); );
} }
protected function versionsHooks(ILogger $logger) { protected function versionsHooks() {
$versionsActions = new Versions($logger); $versionsActions = new Versions($this->logger);
Util::connectHook('\OCP\Versions', 'rollback', $versionsActions, 'rollback'); Util::connectHook('\OCP\Versions', 'rollback', $versionsActions, 'rollback');
Util::connectHook('\OCP\Versions', 'delete',$versionsActions, 'delete'); Util::connectHook('\OCP\Versions', 'delete',$versionsActions, 'delete');
} }
protected function trashbinHooks(ILogger $logger) { protected function trashbinHooks() {
$trashActions = new Trashbin($logger); $trashActions = new Trashbin($this->logger);
Util::connectHook('\OCP\Trashbin', 'preDelete', $trashActions, 'delete'); Util::connectHook('\OCP\Trashbin', 'preDelete', $trashActions, 'delete');
Util::connectHook('\OCA\Files_Trashbin\Trashbin', 'post_restore', $trashActions, 'restore'); Util::connectHook('\OCA\Files_Trashbin\Trashbin', 'post_restore', $trashActions, 'restore');
} }
protected function securityHooks(ILogger $logger) { protected function securityHooks() {
$eventDispatcher = $this->getContainer()->getServer()->getEventDispatcher(); $eventDispatcher = $this->getContainer()->getServer()->getEventDispatcher();
$eventDispatcher->addListener(IProvider::EVENT_SUCCESS, function(GenericEvent $event) use ($logger) { $eventDispatcher->addListener(IProvider::EVENT_SUCCESS, function(GenericEvent $event) {
$security = new Security($logger); $security = new Security($this->logger);
$security->twofactorSuccess($event->getSubject(), $event->getArguments()); $security->twofactorSuccess($event->getSubject(), $event->getArguments());
}); });
$eventDispatcher->addListener(IProvider::EVENT_FAILED, function(GenericEvent $event) use ($logger) { $eventDispatcher->addListener(IProvider::EVENT_FAILED, function(GenericEvent $event) {
$security = new Security($logger); $security = new Security($this->logger);
$security->twofactorFailed($event->getSubject(), $event->getArguments()); $security->twofactorFailed($event->getSubject(), $event->getArguments());
}); });
} }

View File

@ -0,0 +1,52 @@
<?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 OCA\AdminAudit\BackgroundJobs;
use OC\BackgroundJob\TimedJob;
use OCP\Log\RotationTrait;
class Rotate extends TimedJob {
use RotationTrait;
public function __construct() {
$this->setInterval(60*60*3);
}
protected function run($argument) {
$config = \OC::$server->getConfig();
$default = $config->getSystemValue('datadirectory', \OC::$SERVERROOT . '/data') . '/audit.log';
$this->filePath = $config->getAppValue('admin_audit', 'logfile', $default);
if($this->filePath === '') {
// default log file, nothing to do
return;
}
$this->maxSize = $config->getSystemValue('log_rotate_size', 100 * 1024 * 1024);
if($this->shouldRotateBySize()) {
$this->rotate();
}
}
}

View File

@ -69,7 +69,7 @@ class ExceptionLoggerPluginTest extends TestCase {
}); });
$this->server = new Server(); $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), $config);
$this->plugin = new PluginToTest('unit-test', $this->logger); $this->plugin = new PluginToTest('unit-test', $this->logger);
$this->plugin->initialize($this->server); $this->plugin->initialize($this->server);
} }

View File

@ -239,6 +239,10 @@ return array(
'OCP\\Lock\\ILockingProvider' => $baseDir . '/lib/public/Lock/ILockingProvider.php', 'OCP\\Lock\\ILockingProvider' => $baseDir . '/lib/public/Lock/ILockingProvider.php',
'OCP\\Lock\\LockedException' => $baseDir . '/lib/public/Lock/LockedException.php', 'OCP\\Lock\\LockedException' => $baseDir . '/lib/public/Lock/LockedException.php',
'OCP\\Lockdown\\ILockdownManager' => $baseDir . '/lib/public/Lockdown/ILockdownManager.php', 'OCP\\Lockdown\\ILockdownManager' => $baseDir . '/lib/public/Lockdown/ILockdownManager.php',
'OCP\\Log\\IFileBased' => $baseDir . '/lib/public/Log/IFileBased.php',
'OCP\\Log\\ILogFactory' => $baseDir . '/lib/public/Log/ILogFactory.php',
'OCP\\Log\\IWriter' => $baseDir . '/lib/public/Log/IWriter.php',
'OCP\\Log\\RotationTrait' => $baseDir . '/lib/public/Log/RotationTrait.php',
'OCP\\Mail\\IAttachment' => $baseDir . '/lib/public/Mail/IAttachment.php', 'OCP\\Mail\\IAttachment' => $baseDir . '/lib/public/Mail/IAttachment.php',
'OCP\\Mail\\IEMailTemplate' => $baseDir . '/lib/public/Mail/IEMailTemplate.php', 'OCP\\Mail\\IEMailTemplate' => $baseDir . '/lib/public/Mail/IEMailTemplate.php',
'OCP\\Mail\\IMailer' => $baseDir . '/lib/public/Mail/IMailer.php', 'OCP\\Mail\\IMailer' => $baseDir . '/lib/public/Mail/IMailer.php',
@ -750,6 +754,7 @@ return array(
'OC\\Log\\Errorlog' => $baseDir . '/lib/private/Log/Errorlog.php', 'OC\\Log\\Errorlog' => $baseDir . '/lib/private/Log/Errorlog.php',
'OC\\Log\\ExceptionSerializer' => $baseDir . '/lib/private/Log/ExceptionSerializer.php', 'OC\\Log\\ExceptionSerializer' => $baseDir . '/lib/private/Log/ExceptionSerializer.php',
'OC\\Log\\File' => $baseDir . '/lib/private/Log/File.php', 'OC\\Log\\File' => $baseDir . '/lib/private/Log/File.php',
'OC\\Log\\LogFactory' => $baseDir . '/lib/private/Log/LogFactory.php',
'OC\\Log\\Rotate' => $baseDir . '/lib/private/Log/Rotate.php', 'OC\\Log\\Rotate' => $baseDir . '/lib/private/Log/Rotate.php',
'OC\\Log\\Syslog' => $baseDir . '/lib/private/Log/Syslog.php', 'OC\\Log\\Syslog' => $baseDir . '/lib/private/Log/Syslog.php',
'OC\\Mail\\Attachment' => $baseDir . '/lib/private/Mail/Attachment.php', 'OC\\Mail\\Attachment' => $baseDir . '/lib/private/Mail/Attachment.php',

View File

@ -269,6 +269,10 @@ class ComposerStaticInit53792487c5a8370acc0b06b1a864ff4c
'OCP\\Lock\\ILockingProvider' => __DIR__ . '/../../..' . '/lib/public/Lock/ILockingProvider.php', 'OCP\\Lock\\ILockingProvider' => __DIR__ . '/../../..' . '/lib/public/Lock/ILockingProvider.php',
'OCP\\Lock\\LockedException' => __DIR__ . '/../../..' . '/lib/public/Lock/LockedException.php', 'OCP\\Lock\\LockedException' => __DIR__ . '/../../..' . '/lib/public/Lock/LockedException.php',
'OCP\\Lockdown\\ILockdownManager' => __DIR__ . '/../../..' . '/lib/public/Lockdown/ILockdownManager.php', 'OCP\\Lockdown\\ILockdownManager' => __DIR__ . '/../../..' . '/lib/public/Lockdown/ILockdownManager.php',
'OCP\\Log\\IFileBased' => __DIR__ . '/../../..' . '/lib/public/Log/IFileBased.php',
'OCP\\Log\\ILogFactory' => __DIR__ . '/../../..' . '/lib/public/Log/ILogFactory.php',
'OCP\\Log\\IWriter' => __DIR__ . '/../../..' . '/lib/public/Log/IWriter.php',
'OCP\\Log\\RotationTrait' => __DIR__ . '/../../..' . '/lib/public/Log/RotationTrait.php',
'OCP\\Mail\\IAttachment' => __DIR__ . '/../../..' . '/lib/public/Mail/IAttachment.php', 'OCP\\Mail\\IAttachment' => __DIR__ . '/../../..' . '/lib/public/Mail/IAttachment.php',
'OCP\\Mail\\IEMailTemplate' => __DIR__ . '/../../..' . '/lib/public/Mail/IEMailTemplate.php', 'OCP\\Mail\\IEMailTemplate' => __DIR__ . '/../../..' . '/lib/public/Mail/IEMailTemplate.php',
'OCP\\Mail\\IMailer' => __DIR__ . '/../../..' . '/lib/public/Mail/IMailer.php', 'OCP\\Mail\\IMailer' => __DIR__ . '/../../..' . '/lib/public/Mail/IMailer.php',
@ -780,6 +784,7 @@ class ComposerStaticInit53792487c5a8370acc0b06b1a864ff4c
'OC\\Log\\Errorlog' => __DIR__ . '/../../..' . '/lib/private/Log/Errorlog.php', 'OC\\Log\\Errorlog' => __DIR__ . '/../../..' . '/lib/private/Log/Errorlog.php',
'OC\\Log\\ExceptionSerializer' => __DIR__ . '/../../..' . '/lib/private/Log/ExceptionSerializer.php', 'OC\\Log\\ExceptionSerializer' => __DIR__ . '/../../..' . '/lib/private/Log/ExceptionSerializer.php',
'OC\\Log\\File' => __DIR__ . '/../../..' . '/lib/private/Log/File.php', 'OC\\Log\\File' => __DIR__ . '/../../..' . '/lib/private/Log/File.php',
'OC\\Log\\LogFactory' => __DIR__ . '/../../..' . '/lib/private/Log/LogFactory.php',
'OC\\Log\\Rotate' => __DIR__ . '/../../..' . '/lib/private/Log/Rotate.php', 'OC\\Log\\Rotate' => __DIR__ . '/../../..' . '/lib/private/Log/Rotate.php',
'OC\\Log\\Syslog' => __DIR__ . '/../../..' . '/lib/private/Log/Syslog.php', 'OC\\Log\\Syslog' => __DIR__ . '/../../..' . '/lib/private/Log/Syslog.php',
'OC\\Mail\\Attachment' => __DIR__ . '/../../..' . '/lib/private/Mail/Attachment.php', 'OC\\Mail\\Attachment' => __DIR__ . '/../../..' . '/lib/private/Mail/Attachment.php',

View File

@ -38,7 +38,8 @@ namespace OC;
use InterfaSys\LogNormalizer\Normalizer; use InterfaSys\LogNormalizer\Normalizer;
use OC\Log\ExceptionSerializer; use OC\Log\ExceptionSerializer;
use OC\Log\File; use OCP\Log\IFileBased;
use OCP\Log\IWriter;
use OCP\ILogger; use OCP\ILogger;
use OCP\Support\CrashReport\IRegistry; use OCP\Support\CrashReport\IRegistry;
use OCP\Util; use OCP\Util;
@ -54,7 +55,7 @@ use OCP\Util;
*/ */
class Log implements ILogger { class Log implements ILogger {
/** @var string */ /** @var IWriter */
private $logger; private $logger;
/** @var SystemConfig */ /** @var SystemConfig */
@ -70,27 +71,19 @@ class Log implements ILogger {
private $crashReporters; private $crashReporters;
/** /**
* @param string $logger The logger that should be used * @param IWriter $logger The logger that should be used
* @param SystemConfig $config the system config object * @param SystemConfig $config the system config object
* @param Normalizer|null $normalizer * @param Normalizer|null $normalizer
* @param IRegistry|null $registry * @param IRegistry|null $registry
*/ */
public function __construct($logger = null, SystemConfig $config = null, $normalizer = null, IRegistry $registry = null) { public function __construct(IWriter $logger, SystemConfig $config = null, $normalizer = null, IRegistry $registry = null) {
// FIXME: Add this for backwards compatibility, should be fixed at some point probably // FIXME: Add this for backwards compatibility, should be fixed at some point probably
if ($config === null) { if ($config === null) {
$config = \OC::$server->getSystemConfig(); $config = \OC::$server->getSystemConfig();
} }
$this->config = $config; $this->config = $config;
$this->logger = $logger;
// 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;
}
if ($normalizer === null) { if ($normalizer === null) {
$this->normalizer = new Normalizer(); $this->normalizer = new Normalizer();
} else { } else {
@ -302,7 +295,7 @@ class Log implements ILogger {
array_walk($context, [$this->normalizer, 'format']); array_walk($context, [$this->normalizer, 'format']);
if ($level >= $minLevel) { if ($level >= $minLevel) {
if ($this->logger !== File::class) { if (!$this->logger instanceof IFileBased) {
$data = json_encode($data, JSON_PARTIAL_OUTPUT_ON_ERROR); $data = json_encode($data, JSON_PARTIAL_OUTPUT_ON_ERROR);
} }
$this->writeLog($app, $data, $level); $this->writeLog($app, $data, $level);
@ -320,28 +313,13 @@ class Log implements ILogger {
* @param int $level * @param int $level
*/ */
protected function writeLog(string $app, $entry, 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);
} }
/** public function getLogPath():string {
* @param string $logType if($this->logger instanceof IFileBased) {
* @return string return $this->logger->getLogFilePath();
* @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;
} }
throw new \RuntimeException('Log implementation has no path');
} }
} }

View File

@ -25,14 +25,9 @@
namespace OC\Log; namespace OC\Log;
class Errorlog { use OCP\Log\IWriter;
class Errorlog implements IWriter {
/**
* Init class data
*/
public static function init() {
}
/** /**
* write a message in the log * write a message in the log
@ -40,7 +35,7 @@ class Errorlog {
* @param string $message * @param string $message
* @param int $level * @param int $level
*/ */
public static function write($app, $message, $level) { public function write(string $app, $message, int $level) {
error_log('[owncloud]['.$app.']['.$level.'] '.$message); error_log('[owncloud]['.$app.']['.$level.'] '.$message);
} }
} }

View File

@ -36,7 +36,9 @@
*/ */
namespace OC\Log; namespace OC\Log;
use OC\SystemConfig;
use OCP\Log\IFileBased;
use OCP\Log\IWriter;
use OCP\ILogger; use OCP\ILogger;
/** /**
@ -45,30 +47,26 @@ use OCP\ILogger;
* Log is saved at data/nextcloud.log (on default) * Log is saved at data/nextcloud.log (on default)
*/ */
class File { class File implements IWriter, IFileBased {
static protected $logFile; /** @var string */
protected $logFile;
/** @var SystemConfig */
private $config;
/** public function __construct(string $path, string $fallbackPath = '', SystemConfig $config) {
* Init class data $this->logFile = $path;
*/ if (!file_exists($this->logFile)) {
public static function init() { if(
$systemConfig = \OC::$server->getSystemConfig(); (
$defaultLogFile = $systemConfig->getValue("datadirectory", \OC::$SERVERROOT.'/data').'/nextcloud.log'; !is_writable(dirname($this->logFile))
self::$logFile = $systemConfig->getValue("logfile", $defaultLogFile); || !touch($this->logFile)
)
/** && $fallbackPath !== ''
* Fall back to default log file if specified logfile does not exist ) {
* and can not be created. $this->logFile = $fallbackPath;
*/
if (!file_exists(self::$logFile)) {
if(!is_writable(dirname(self::$logFile))) {
self::$logFile = $defaultLogFile;
} else {
if(!touch(self::$logFile)) {
self::$logFile = $defaultLogFile;
}
} }
} }
$this->config = $config;
} }
/** /**
@ -77,12 +75,10 @@ class File {
* @param string|array $message * @param string|array $message
* @param int $level * @param int $level
*/ */
public static function write($app, $message, $level) { public function write(string $app, $message, int $level) {
$config = \OC::$server->getSystemConfig();
// default to ISO8601 // default to ISO8601
$format = $config->getValue('logdateformat', \DateTime::ATOM); $format = $this->config->getValue('logdateformat', \DateTime::ATOM);
$logTimeZone = $config->getValue('logtimezone', 'UTC'); $logTimeZone = $this->config->getValue('logtimezone', 'UTC');
try { try {
$timezone = new \DateTimeZone($logTimeZone); $timezone = new \DateTimeZone($logTimeZone);
} catch (\Exception $e) { } catch (\Exception $e) {
@ -102,7 +98,7 @@ class File {
$time = $time->format($format); $time = $time->format($format);
$url = ($request->getRequestUri() !== '') ? $request->getRequestUri() : '--'; $url = ($request->getRequestUri() !== '') ? $request->getRequestUri() : '--';
$method = is_string($request->getMethod()) ? $request->getMethod() : '--'; $method = is_string($request->getMethod()) ? $request->getMethod() : '--';
if($config->getValue('installed', false)) { if($this->config->getValue('installed', false)) {
$user = \OC_User::getUser() ? \OC_User::getUser() : '--'; $user = \OC_User::getUser() ? \OC_User::getUser() : '--';
} else { } else {
$user = '--'; $user = '--';
@ -111,7 +107,7 @@ class File {
if ($userAgent === '') { if ($userAgent === '') {
$userAgent = '--'; $userAgent = '--';
} }
$version = $config->getValue('version', ''); $version = $this->config->getValue('version', '');
$entry = compact( $entry = compact(
'reqId', 'reqId',
'level', 'level',
@ -137,9 +133,9 @@ class File {
} }
} }
$entry = json_encode($entry, JSON_PARTIAL_OUTPUT_ON_ERROR); $entry = json_encode($entry, JSON_PARTIAL_OUTPUT_ON_ERROR);
$handle = @fopen(self::$logFile, 'a'); $handle = @fopen($this->logFile, 'a');
if ((fileperms(self::$logFile) & 0777) != 0640) { if ((fileperms($this->logFile) & 0777) != 0640) {
@chmod(self::$logFile, 0640); @chmod($this->logFile, 0640);
} }
if ($handle) { if ($handle) {
fwrite($handle, $entry."\n"); fwrite($handle, $entry."\n");
@ -159,11 +155,10 @@ class File {
* @param int $offset * @param int $offset
* @return array * @return array
*/ */
public static function getEntries($limit=50, $offset=0) { public function getEntries(int $limit=50, int $offset=0):array {
self::init(); $minLevel = $this->config->getValue("loglevel", ILogger::WARN);
$minLevel = \OC::$server->getSystemConfig()->getValue("loglevel", ILogger::WARN);
$entries = array(); $entries = array();
$handle = @fopen(self::$logFile, 'rb'); $handle = @fopen($this->logFile, 'rb');
if ($handle) { if ($handle) {
fseek($handle, 0, SEEK_END); fseek($handle, 0, SEEK_END);
$pos = ftell($handle); $pos = ftell($handle);
@ -205,7 +200,7 @@ class File {
/** /**
* @return string * @return string
*/ */
public static function getLogFilePath() { public function getLogFilePath():string {
return self::$logFile; return $this->logFile;
} }
} }

View File

@ -0,0 +1,78 @@
<?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 OC\Log;
use OC\SystemConfig;
use OCP\ILogger;
use OCP\IServerContainer;
use OCP\Log\ILogFactory;
use OCP\Log\IWriter;
class LogFactory implements ILogFactory {
/** @var IServerContainer */
private $c;
/** @var SystemConfig */
private $systemConfig;
public function __construct(IServerContainer $c, SystemConfig $systemConfig) {
$this->c = $c;
$this->systemConfig = $systemConfig;
}
/**
* @throws \OCP\AppFramework\QueryException
*/
public function get(string $type):IWriter {
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();
}
}
public function getCustomLogger(string $path):ILogger {
$log = $this->buildLogFile($path);
return new Log($log, $this->systemConfig);
}
protected function buildLogFile(string $logFile = ''):File {
$defaultLogFile = $this->systemConfig->getValue('datadirectory', \OC::$SERVERROOT.'/data').'/nextcloud.log';
if($logFile === '') {
$logFile = $this->systemConfig->getValue('logfile', $defaultLogFile);
}
$fallback = $defaultLogFile !== $logFile ? $defaultLogFile : '';
return new File($logFile, $fallback, $this->systemConfig);
}
}

View File

@ -24,7 +24,7 @@
*/ */
namespace OC\Log; namespace OC\Log;
use OCP\ILogger; use OCP\Log\RotationTrait;
/** /**
* This rotates the current logfile to a new name, this way the total log usage * This rotates the current logfile to a new name, this way the total log usage
@ -33,23 +33,17 @@ use OCP\ILogger;
* location and manage that with your own tools. * location and manage that with your own tools.
*/ */
class Rotate extends \OC\BackgroundJob\Job { class Rotate extends \OC\BackgroundJob\Job {
private $max_log_size; use RotationTrait;
public function run($dummy) { public function run($dummy) {
$systemConfig = \OC::$server->getSystemConfig(); $systemConfig = \OC::$server->getSystemConfig();
$logFile = $systemConfig->getValue('logfile', $systemConfig->getValue('datadirectory', \OC::$SERVERROOT . '/data') . '/nextcloud.log'); $this->filePath = $systemConfig->getValue('logfile', $systemConfig->getValue('datadirectory', \OC::$SERVERROOT . '/data') . '/nextcloud.log');
$this->max_log_size = \OC::$server->getConfig()->getSystemValue('log_rotate_size', 100 * 1024 * 1024);
if ($this->max_log_size) { $this->maxSize = \OC::$server->getConfig()->getSystemValue('log_rotate_size', 100 * 1024 * 1024);
$filesize = @filesize($logFile); if($this->shouldRotateBySize()) {
if ($filesize >= $this->max_log_size) { $rotatedFile = $this->rotate();
$this->rotate($logFile); $msg = 'Log file "'.$this->filePath.'" was over '.$this->maxSize.' bytes, moved to "'.$rotatedFile.'"';
} \OC::$server->getLogger()->warning($msg, ['app' => Rotate::class]);
} }
} }
protected function rotate($logfile) {
$rotatedLogfile = $logfile.'.1';
rename($logfile, $rotatedLogfile);
$msg = 'Log file "'.$logfile.'" was over '.$this->max_log_size.' bytes, moved to "'.$rotatedLogfile.'"';
\OCP\Util::writeLog(Rotate::class, $msg, ILogger::WARN);
}
} }

View File

@ -26,23 +26,24 @@
namespace OC\Log; namespace OC\Log;
use OCP\ILogger; use OCP\ILogger;
use OCP\IConfig;
use OCP\Log\IWriter;
class Syslog { class Syslog implements IWriter {
static protected $levels = array( protected $levels = [
ILogger::DEBUG => LOG_DEBUG, ILogger::DEBUG => LOG_DEBUG,
ILogger::INFO => LOG_INFO, ILogger::INFO => LOG_INFO,
ILogger::WARN => LOG_WARNING, ILogger::WARN => LOG_WARNING,
ILogger::ERROR => LOG_ERR, ILogger::ERROR => LOG_ERR,
ILogger::FATAL => LOG_CRIT, ILogger::FATAL => LOG_CRIT,
); ];
/** public function __construct(IConfig $config) {
* Init class data openlog($config->getSystemValue('syslog_tag', 'ownCloud'), LOG_PID | LOG_CONS, LOG_USER);
*/ }
public static function init() {
openlog(\OC::$server->getSystemConfig()->getValue("syslog_tag", "ownCloud"), LOG_PID | LOG_CONS, LOG_USER); public function __destruct() {
// Close at shutdown closelog();
register_shutdown_function('closelog');
} }
/** /**
@ -51,8 +52,8 @@ class Syslog {
* @param string $message * @param string $message
* @param int $level * @param int $level
*/ */
public static function write($app, $message, $level) { public function write(string $app, $message, int $level) {
$syslog_level = self::$levels[$level]; $syslog_level = $this->levels[$level];
syslog($syslog_level, '{'.$app.'} '.$message); syslog($syslog_level, '{'.$app.'} '.$message);
} }
} }

View File

@ -87,6 +87,7 @@ use OC\Lock\DBLockingProvider;
use OC\Lock\MemcacheLockingProvider; use OC\Lock\MemcacheLockingProvider;
use OC\Lock\NoopLockingProvider; use OC\Lock\NoopLockingProvider;
use OC\Lockdown\LockdownManager; use OC\Lockdown\LockdownManager;
use OC\Log\LogFactory;
use OC\Mail\Mailer; use OC\Mail\Mailer;
use OC\Memcache\ArrayCache; use OC\Memcache\ArrayCache;
use OC\Memcache\Factory; use OC\Memcache\Factory;
@ -134,6 +135,7 @@ use OCP\ITempManager;
use OCP\Contacts\ContactsMenu\IActionFactory; use OCP\Contacts\ContactsMenu\IActionFactory;
use OCP\IUser; use OCP\IUser;
use OCP\Lock\ILockingProvider; use OCP\Lock\ILockingProvider;
use OCP\Log\ILogFactory;
use OCP\Remote\Api\IApiFactory; use OCP\Remote\Api\IApiFactory;
use OCP\Remote\IInstanceFactory; use OCP\Remote\IInstanceFactory;
use OCP\RichObjectStrings\IValidator; use OCP\RichObjectStrings\IValidator;
@ -546,15 +548,18 @@ class Server extends ServerContainer implements IServerContainer {
$this->registerService(\OCP\ILogger::class, function (Server $c) { $this->registerService(\OCP\ILogger::class, function (Server $c) {
$logType = $c->query('AllConfig')->getSystemValue('log_type', 'file'); $logType = $c->query('AllConfig')->getSystemValue('log_type', 'file');
$logger = Log::getLogClass($logType); $factory = new LogFactory($c, $this->getSystemConfig());
call_user_func(array($logger, 'init')); $logger = $factory->get($logType);
$config = $this->getSystemConfig();
$registry = $c->query(\OCP\Support\CrashReport\IRegistry::class); $registry = $c->query(\OCP\Support\CrashReport\IRegistry::class);
return new Log($logger, $config, null, $registry); return new Log($logger, $this->getSystemConfig(), null, $registry);
}); });
$this->registerAlias('Logger', \OCP\ILogger::class); $this->registerAlias('Logger', \OCP\ILogger::class);
$this->registerService(ILogFactory::class, function (Server $c) {
return new LogFactory($c, $this->getSystemConfig());
});
$this->registerService(\OCP\BackgroundJob\IJobList::class, function (Server $c) { $this->registerService(\OCP\BackgroundJob\IJobList::class, function (Server $c) {
$config = $c->getConfig(); $config = $c->getConfig();
return new \OC\BackgroundJob\JobList( return new \OC\BackgroundJob\JobList(
@ -1528,6 +1533,14 @@ class Server extends ServerContainer implements IServerContainer {
return $this->query('Logger'); return $this->query('Logger');
} }
/**
* @return ILogFactory
* @throws \OCP\AppFramework\QueryException
*/
public function getLogFactory() {
return $this->query(ILogFactory::class);
}
/** /**
* Returns a router for generating and matching urls * Returns a router for generating and matching urls
* *

View File

@ -44,6 +44,7 @@
// use OCP namespace for all classes that are considered public. // use OCP namespace for all classes that are considered public.
// This means that they should be used by apps instead of the internal ownCloud classes // This means that they should be used by apps instead of the internal ownCloud classes
namespace OCP; namespace OCP;
use OCP\Log\ILogFactory;
use OCP\Security\IContentSecurityPolicyManager; use OCP\Security\IContentSecurityPolicyManager;
use Symfony\Component\EventDispatcher\EventDispatcherInterface; use Symfony\Component\EventDispatcher\EventDispatcherInterface;
@ -314,6 +315,14 @@ interface IServerContainer extends IContainer {
*/ */
public function getLogger(); public function getLogger();
/**
* returns a log factory instance
*
* @return ILogFactory
* @since 14.0.0
*/
public function getLogFactory();
/** /**
* Returns a router for generating and matching urls * Returns a router for generating and matching urls
* *

View File

@ -0,0 +1,43 @@
<?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 OCP\Log;
/**
* Interface IFileBased
*
* @package OCP\Log
*
* @since 14.0.0
*/
interface IFileBased {
/**
* @since 14.0.0
*/
public function getLogFilePath():string;
/**
* @since 14.0.0
*/
public function getEntries(int $limit=50, int $offset=0): array;
}

View File

@ -0,0 +1,48 @@
<?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 OCP\Log;
use OCP\ILogger;
/**
* Interface ILogFactory
*
* @package OCP\Log
* @since 14.0.0
*/
interface ILogFactory {
/**
* @param string $type - one of: file, errorlog, syslog
* @return IWriter
* @since 14.0.0
*/
public function get(string $type): IWriter;
/**
* @param string $path
* @return ILogger
* @since 14.0.0
*/
public function getCustomLogger(string $path): ILogger;
}

View File

@ -0,0 +1,37 @@
<?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 OCP\Log;
/**
* Interface IWriter
*
* @package OCP\Log
* @since 14.0.0
*/
interface IWriter {
/**
* @since 14.0.0
*/
public function write(string $app, $message, int $level);
}

View File

@ -0,0 +1,71 @@
<?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 OCP\Log;
/**
* Trait RotationTrait
*
* @package OCP\Log
*
* @since 14.0.0
*/
trait RotationTrait {
/**
* @var string
* @since 14.0.0
*/
protected $filePath;
/**
* @var int
* @since 14.0.0
*/
protected $maxSize;
/**
* @return string the resulting new filepath
* @since 14.0.0
*/
protected function rotate():string {
$rotatedFile = $this->filePath.'.1';
rename($this->filePath, $rotatedFile);
return $rotatedFile;
}
/**
* @return bool
* @since 14.0.0
*/
protected function shouldRotateBySize():bool {
if ((int)$this->maxSize > 0) {
$filesize = @filesize($this->filePath);
if ($filesize >= (int)$this->maxSize) {
return true;
}
}
return false;
}
}

View File

@ -26,8 +26,11 @@
namespace OC\Settings\Controller; namespace OC\Settings\Controller;
use OC\Log;
use OCP\AppFramework\Controller; use OCP\AppFramework\Controller;
use OCP\AppFramework\Http\StreamResponse; use OCP\AppFramework\Http\StreamResponse;
use OCP\ILogger;
use OCP\IRequest;
/** /**
* Class LogSettingsController * Class LogSettingsController
@ -35,6 +38,15 @@ use OCP\AppFramework\Http\StreamResponse;
* @package OC\Settings\Controller * @package OC\Settings\Controller
*/ */
class LogSettingsController extends Controller { class LogSettingsController extends Controller {
/** @var ILogger */
private $log;
public function __construct(string $appName, IRequest $request, ILogger $logger) {
parent::__construct($appName, $request);
$this->log = $logger;
}
/** /**
* download logfile * download logfile
* *
@ -43,7 +55,10 @@ class LogSettingsController extends Controller {
* @return StreamResponse * @return StreamResponse
*/ */
public function download() { 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-Type', 'application/octet-stream');
$resp->addHeader('Content-Disposition', 'attachment; filename="nextcloud.log"'); $resp->addHeader('Content-Disposition', 'attachment; filename="nextcloud.log"');
return $resp; return $resp;

View File

@ -31,28 +31,31 @@ class FileTest extends TestCase
private $restore_logfile; private $restore_logfile;
private $restore_logdateformat; private $restore_logdateformat;
/** @var File */
protected $logFile;
protected function setUp() { protected function setUp() {
parent::setUp(); parent::setUp();
$config = \OC::$server->getConfig(); $config = \OC::$server->getSystemConfig();
$this->restore_logfile = $config->getSystemValue("logfile"); $this->restore_logfile = $config->getValue("logfile");
$this->restore_logdateformat = $config->getSystemValue('logdateformat'); $this->restore_logdateformat = $config->getValue('logdateformat');
$config->setSystemValue("logfile", $config->getSystemValue('datadirectory') . "/logtest"); $config->setValue("logfile", $config->getValue('datadirectory') . "/logtest.log");
File::init(); $this->logFile = new File($config->getValue('datadirectory') . '/logtest.log', '', $config);
} }
protected function tearDown() { protected function tearDown() {
$config = \OC::$server->getConfig(); $config = \OC::$server->getSystemConfig();
if (isset($this->restore_logfile)) { if (isset($this->restore_logfile)) {
$config->getSystemValue("logfile", $this->restore_logfile); $config->getValue("logfile", $this->restore_logfile);
} else { } else {
$config->deleteSystemValue("logfile"); $config->deleteValue("logfile");
} }
if (isset($this->restore_logdateformat)) { if (isset($this->restore_logdateformat)) {
$config->getSystemValue("logdateformat", $this->restore_logdateformat); $config->getValue("logdateformat", $this->restore_logdateformat);
} else { } else {
$config->deleteSystemValue("logdateformat"); $config->deleteValue("logdateformat");
} }
File::init(); $this->logFile = new File($this->restore_logfile, '', $config);
parent::tearDown(); parent::tearDown();
} }
@ -63,7 +66,7 @@ class FileTest extends TestCase
# set format & write log line # set format & write log line
$config->setSystemValue('logdateformat', 'u'); $config->setSystemValue('logdateformat', 'u');
File::write('test', 'message', ILogger::ERROR); $this->logFile->write('test', 'message', ILogger::ERROR);
# read log line # read log line
$handle = @fopen($config->getSystemValue('logfile'), 'r'); $handle = @fopen($config->getSystemValue('logfile'), 'r');

View File

@ -0,0 +1,144 @@
<?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 OC\SystemConfig;
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;
/** @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->systemConfig);
}
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';
$this->systemConfig->expects($this->exactly(2))
->method('getValue')
->withConsecutive(['datadirectory', $datadir], ['logfile', $defaultLog])
->willReturnOnConsecutiveCalls($datadir, $defaultLog);
$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';
$this->systemConfig->expects($this->exactly(2))
->method('getValue')
->withConsecutive(['datadirectory', $datadir], ['logfile', $defaultLog])
->willReturnOnConsecutiveCalls($datadir, $path);
$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);
}
}

View File

@ -10,8 +10,9 @@ namespace Test;
use OC\Log; use OC\Log;
use OCP\ILogger; use OCP\ILogger;
use OCP\Log\IWriter;
class LoggerTest extends TestCase { class LoggerTest extends TestCase implements IWriter {
/** @var \OC\SystemConfig|\PHPUnit_Framework_MockObject_MockObject */ /** @var \OC\SystemConfig|\PHPUnit_Framework_MockObject_MockObject */
private $config; private $config;
@ -23,15 +24,15 @@ class LoggerTest extends TestCase {
private $logger; private $logger;
/** @var array */ /** @var array */
static private $logs = array(); private $logs = [];
protected function setUp() { protected function setUp() {
parent::setUp(); parent::setUp();
self::$logs = array(); $this->logs = [];
$this->config = $this->createMock(\OC\SystemConfig::class); $this->config = $this->createMock(\OC\SystemConfig::class);
$this->registry = $this->createMock(\OCP\Support\CrashReport\IRegistry::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() { public function testInterpolation() {
@ -63,11 +64,11 @@ class LoggerTest extends TestCase {
} }
private function getLogs() { private function getLogs() {
return self::$logs; return $this->logs;
} }
public static function write($app, $message, $level) { public function write(string $app, $message, int $level) {
self::$logs[]= "$level $message"; $this->logs[]= "$level $message";
} }
public function userAndPasswordData() { public function userAndPasswordData() {
@ -202,23 +203,4 @@ class LoggerTest extends TestCase {
$this->assertContains('*** sensitive parameters replaced ***', $logLine); $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));
}
} }