log to $datadir/audit.log by default and add rotation
Signed-off-by: Arthur Schiwon <blizzz@arthur-schiwon.de>
This commit is contained in:
parent
ab7a4b8693
commit
b841a477c6
10 changed files with 144 additions and 18 deletions
|
@ -17,4 +17,7 @@
|
|||
<dependencies>
|
||||
<nextcloud min-version="14" max-version="14" />
|
||||
</dependencies>
|
||||
<background-jobs>
|
||||
<job>OCA\AdminAudit\BackgroundJobs\Rotate</job>
|
||||
</background-jobs>
|
||||
</info>
|
||||
|
|
|
@ -18,4 +18,5 @@ return array(
|
|||
'OCA\\AdminAudit\\Actions\\UserManagement' => $baseDir . '/../lib/Actions/UserManagement.php',
|
||||
'OCA\\AdminAudit\\Actions\\Versions' => $baseDir . '/../lib/Actions/Versions.php',
|
||||
'OCA\\AdminAudit\\AppInfo\\Application' => $baseDir . '/../lib/AppInfo/Application.php',
|
||||
'OCA\\AdminAudit\\BackgroundJobs\\Rotate' => $baseDir . '/../lib/BackgroundJobs/Rotate.php',
|
||||
);
|
||||
|
|
|
@ -33,6 +33,7 @@ class ComposerStaticInitAdminAudit
|
|||
'OCA\\AdminAudit\\Actions\\UserManagement' => __DIR__ . '/..' . '/../lib/Actions/UserManagement.php',
|
||||
'OCA\\AdminAudit\\Actions\\Versions' => __DIR__ . '/..' . '/../lib/Actions/Versions.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)
|
||||
|
|
|
@ -63,8 +63,10 @@ class Application extends App {
|
|||
|
||||
public function initLogger() {
|
||||
$c = $this->getContainer()->getServer();
|
||||
$config = $c->getConfig();
|
||||
|
||||
$logFile = $c->getConfig()->getAppValue('admin_audit', 'logfile', null);
|
||||
$default = $config->getSystemValue('datadirectory', \OC::$SERVERROOT . '/data') . '/audit.log';
|
||||
$logFile = $config->getAppValue('admin_audit', 'logfile', $default);
|
||||
if($logFile === null) {
|
||||
$this->logger = $c->getLogger();
|
||||
return;
|
||||
|
|
52
apps/admin_audit/lib/BackgroundJobs/Rotate.php
Normal file
52
apps/admin_audit/lib/BackgroundJobs/Rotate.php
Normal 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();
|
||||
}
|
||||
}
|
||||
}
|
|
@ -242,6 +242,7 @@ return array(
|
|||
'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\\IEMailTemplate' => $baseDir . '/lib/public/Mail/IEMailTemplate.php',
|
||||
'OCP\\Mail\\IMailer' => $baseDir . '/lib/public/Mail/IMailer.php',
|
||||
|
|
|
@ -272,6 +272,7 @@ class ComposerStaticInit53792487c5a8370acc0b06b1a864ff4c
|
|||
'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\\IEMailTemplate' => __DIR__ . '/../../..' . '/lib/public/Mail/IEMailTemplate.php',
|
||||
'OCP\\Mail\\IMailer' => __DIR__ . '/../../..' . '/lib/public/Mail/IMailer.php',
|
||||
|
|
|
@ -24,7 +24,7 @@
|
|||
*/
|
||||
|
||||
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
|
||||
|
@ -33,23 +33,17 @@ use OCP\ILogger;
|
|||
* location and manage that with your own tools.
|
||||
*/
|
||||
class Rotate extends \OC\BackgroundJob\Job {
|
||||
private $max_log_size;
|
||||
use RotationTrait;
|
||||
|
||||
public function run($dummy) {
|
||||
$systemConfig = \OC::$server->getSystemConfig();
|
||||
$logFile = $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) {
|
||||
$filesize = @filesize($logFile);
|
||||
if ($filesize >= $this->max_log_size) {
|
||||
$this->rotate($logFile);
|
||||
}
|
||||
$this->filePath = $systemConfig->getValue('logfile', $systemConfig->getValue('datadirectory', \OC::$SERVERROOT . '/data') . '/nextcloud.log');
|
||||
|
||||
$this->maxSize = \OC::$server->getConfig()->getSystemValue('log_rotate_size', 100 * 1024 * 1024);
|
||||
if($this->shouldRotateBySize()) {
|
||||
$rotatedFile = $this->rotate();
|
||||
$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);
|
||||
}
|
||||
}
|
||||
|
|
71
lib/public/Log/RotationTrait.php
Normal file
71
lib/public/Log/RotationTrait.php
Normal 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;
|
||||
}
|
||||
|
||||
}
|
|
@ -39,7 +39,7 @@ use OCP\IRequest;
|
|||
*/
|
||||
class LogSettingsController extends Controller {
|
||||
|
||||
/** @var Log */
|
||||
/** @var ILogger */
|
||||
private $log;
|
||||
|
||||
public function __construct(string $appName, IRequest $request, ILogger $logger) {
|
||||
|
|
Loading…
Reference in a new issue