diff options
author | Morris Jobke <hey@morrisjobke.de> | 2018-05-02 16:16:28 +0200 |
---|---|---|
committer | GitHub <noreply@github.com> | 2018-05-02 16:16:28 +0200 |
commit | 2aa108000cd5bb29b7be5d7226185b5c1be59e29 (patch) | |
tree | 46d6e45c3e88bfa46bd868f6120896b2a52a2948 /lib | |
parent | a56ec1062af36c02666277f7f97f6a407a87d1e1 (diff) | |
parent | aff5fe68b31c3663be2a114666650d2f8723a22b (diff) | |
download | nextcloud-server-2aa108000cd5bb29b7be5d7226185b5c1be59e29.tar.gz nextcloud-server-2aa108000cd5bb29b7be5d7226185b5c1be59e29.zip |
Merge pull request #9293 from nextcloud/feature/9166/custom-auditlogfile
option for a seperate audit log file
Diffstat (limited to 'lib')
-rw-r--r-- | lib/composer/composer/autoload_classmap.php | 5 | ||||
-rw-r--r-- | lib/composer/composer/autoload_static.php | 5 | ||||
-rw-r--r-- | lib/private/Log.php | 46 | ||||
-rw-r--r-- | lib/private/Log/Errorlog.php | 11 | ||||
-rw-r--r-- | lib/private/Log/File.php | 71 | ||||
-rw-r--r-- | lib/private/Log/LogFactory.php | 78 | ||||
-rw-r--r-- | lib/private/Log/Rotate.php | 26 | ||||
-rw-r--r-- | lib/private/Log/Syslog.php | 25 | ||||
-rw-r--r-- | lib/private/Server.php | 21 | ||||
-rw-r--r-- | lib/public/IServerContainer.php | 9 | ||||
-rw-r--r-- | lib/public/Log/IFileBased.php | 43 | ||||
-rw-r--r-- | lib/public/Log/ILogFactory.php | 48 | ||||
-rw-r--r-- | lib/public/Log/IWriter.php | 37 | ||||
-rw-r--r-- | lib/public/Log/RotationTrait.php | 71 |
14 files changed, 384 insertions, 112 deletions
diff --git a/lib/composer/composer/autoload_classmap.php b/lib/composer/composer/autoload_classmap.php index 19cb583cc93..6f8c4131546 100644 --- a/lib/composer/composer/autoload_classmap.php +++ b/lib/composer/composer/autoload_classmap.php @@ -239,6 +239,10 @@ return array( 'OCP\\Lock\\ILockingProvider' => $baseDir . '/lib/public/Lock/ILockingProvider.php', 'OCP\\Lock\\LockedException' => $baseDir . '/lib/public/Lock/LockedException.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\\IEMailTemplate' => $baseDir . '/lib/public/Mail/IEMailTemplate.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\\ExceptionSerializer' => $baseDir . '/lib/private/Log/ExceptionSerializer.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\\Syslog' => $baseDir . '/lib/private/Log/Syslog.php', 'OC\\Mail\\Attachment' => $baseDir . '/lib/private/Mail/Attachment.php', diff --git a/lib/composer/composer/autoload_static.php b/lib/composer/composer/autoload_static.php index a913b0498b9..92726f7789d 100644 --- a/lib/composer/composer/autoload_static.php +++ b/lib/composer/composer/autoload_static.php @@ -269,6 +269,10 @@ class ComposerStaticInit53792487c5a8370acc0b06b1a864ff4c 'OCP\\Lock\\ILockingProvider' => __DIR__ . '/../../..' . '/lib/public/Lock/ILockingProvider.php', 'OCP\\Lock\\LockedException' => __DIR__ . '/../../..' . '/lib/public/Lock/LockedException.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\\IEMailTemplate' => __DIR__ . '/../../..' . '/lib/public/Mail/IEMailTemplate.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\\ExceptionSerializer' => __DIR__ . '/../../..' . '/lib/private/Log/ExceptionSerializer.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\\Syslog' => __DIR__ . '/../../..' . '/lib/private/Log/Syslog.php', 'OC\\Mail\\Attachment' => __DIR__ . '/../../..' . '/lib/private/Mail/Attachment.php', diff --git a/lib/private/Log.php b/lib/private/Log.php index ffe8c665c6f..69705c49e87 100644 --- a/lib/private/Log.php +++ b/lib/private/Log.php @@ -38,7 +38,8 @@ namespace OC; use InterfaSys\LogNormalizer\Normalizer; use OC\Log\ExceptionSerializer; -use OC\Log\File; +use OCP\Log\IFileBased; +use OCP\Log\IWriter; 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 IWriter */ private $logger; /** @var SystemConfig */ @@ -70,27 +71,19 @@ class Log implements ILogger { 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 Normalizer|null $normalizer * @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 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'); } } diff --git a/lib/private/Log/Errorlog.php b/lib/private/Log/Errorlog.php index 37498c36aba..9dc8b2cc49c 100644 --- a/lib/private/Log/Errorlog.php +++ b/lib/private/Log/Errorlog.php @@ -25,14 +25,9 @@ namespace OC\Log; -class Errorlog { +use OCP\Log\IWriter; - - /** - * Init class data - */ - public static function init() { - } +class Errorlog implements IWriter { /** * write a message in the log @@ -40,7 +35,7 @@ class Errorlog { * @param string $message * @param int $level */ - public static function write($app, $message, $level) { + public function write(string $app, $message, int $level) { error_log('[owncloud]['.$app.']['.$level.'] '.$message); } } diff --git a/lib/private/Log/File.php b/lib/private/Log/File.php index 755c4729c7a..597cb54e402 100644 --- a/lib/private/Log/File.php +++ b/lib/private/Log/File.php @@ -36,7 +36,9 @@ */ namespace OC\Log; - +use OC\SystemConfig; +use OCP\Log\IFileBased; +use OCP\Log\IWriter; use OCP\ILogger; /** @@ -45,30 +47,26 @@ use OCP\ILogger; * Log is saved at data/nextcloud.log (on default) */ -class File { - static 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); +class File implements IWriter, IFileBased { + /** @var string */ + protected $logFile; + /** @var SystemConfig */ + private $config; - /** - * 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 = '', SystemConfig $config) { + $this->logFile = $path; + if (!file_exists($this->logFile)) { + if( + ( + !is_writable(dirname($this->logFile)) + || !touch($this->logFile) + ) + && $fallbackPath !== '' + ) { + $this->logFile = $fallbackPath; } } + $this->config = $config; } /** @@ -77,12 +75,10 @@ class File { * @param string|array $message * @param int $level */ - public static function write($app, $message, $level) { - $config = \OC::$server->getSystemConfig(); - + public function write(string $app, $message, int $level) { // default to ISO8601 - $format = $config->getValue('logdateformat', \DateTime::ATOM); - $logTimeZone = $config->getValue('logtimezone', 'UTC'); + $format = $this->config->getValue('logdateformat', \DateTime::ATOM); + $logTimeZone = $this->config->getValue('logtimezone', 'UTC'); try { $timezone = new \DateTimeZone($logTimeZone); } catch (\Exception $e) { @@ -102,7 +98,7 @@ class File { $time = $time->format($format); $url = ($request->getRequestUri() !== '') ? $request->getRequestUri() : '--'; $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() : '--'; } else { $user = '--'; @@ -111,7 +107,7 @@ class File { if ($userAgent === '') { $userAgent = '--'; } - $version = $config->getValue('version', ''); + $version = $this->config->getValue('version', ''); $entry = compact( 'reqId', 'level', @@ -137,9 +133,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 +155,10 @@ class File { * @param int $offset * @return array */ - public static function getEntries($limit=50, $offset=0) { - self::init(); - $minLevel = \OC::$server->getSystemConfig()->getValue("loglevel", ILogger::WARN); + public function getEntries(int $limit=50, int $offset=0):array { + $minLevel = $this->config->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 +200,7 @@ class File { /** * @return string */ - public static function getLogFilePath() { - return self::$logFile; + public function getLogFilePath():string { + return $this->logFile; } } diff --git a/lib/private/Log/LogFactory.php b/lib/private/Log/LogFactory.php new file mode 100644 index 00000000000..9b9d12abfa8 --- /dev/null +++ b/lib/private/Log/LogFactory.php @@ -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); + } +} diff --git a/lib/private/Log/Rotate.php b/lib/private/Log/Rotate.php index 48b96c98a8d..cc41c804ad3 100644 --- a/lib/private/Log/Rotate.php +++ b/lib/private/Log/Rotate.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'); - 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); + $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]); + } } } diff --git a/lib/private/Log/Syslog.php b/lib/private/Log/Syslog.php index 7b3d931ef31..90a20026f0e 100644 --- a/lib/private/Log/Syslog.php +++ b/lib/private/Log/Syslog.php @@ -26,23 +26,24 @@ namespace OC\Log; use OCP\ILogger; +use OCP\IConfig; +use OCP\Log\IWriter; -class Syslog { - static protected $levels = array( +class Syslog implements IWriter { + 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 - register_shutdown_function('closelog'); + public function __construct(IConfig $config) { + openlog($config->getSystemValue('syslog_tag', 'ownCloud'), LOG_PID | LOG_CONS, LOG_USER); + } + + public function __destruct() { + closelog(); } /** @@ -51,8 +52,8 @@ class Syslog { * @param string $message * @param int $level */ - public static function write($app, $message, $level) { - $syslog_level = self::$levels[$level]; + public function write(string $app, $message, int $level) { + $syslog_level = $this->levels[$level]; syslog($syslog_level, '{'.$app.'} '.$message); } } diff --git a/lib/private/Server.php b/lib/private/Server.php index 3786486c2b2..a879c65bb9b 100644 --- a/lib/private/Server.php +++ b/lib/private/Server.php @@ -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; @@ -134,6 +135,7 @@ use OCP\ITempManager; use OCP\Contacts\ContactsMenu\IActionFactory; use OCP\IUser; use OCP\Lock\ILockingProvider; +use OCP\Log\ILogFactory; use OCP\Remote\Api\IApiFactory; use OCP\Remote\IInstanceFactory; use OCP\RichObjectStrings\IValidator; @@ -546,15 +548,18 @@ 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')); - $config = $this->getSystemConfig(); + $factory = new LogFactory($c, $this->getSystemConfig()); + $logger = $factory->get($logType); $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->registerService(ILogFactory::class, function (Server $c) { + return new LogFactory($c, $this->getSystemConfig()); + }); + $this->registerService(\OCP\BackgroundJob\IJobList::class, function (Server $c) { $config = $c->getConfig(); return new \OC\BackgroundJob\JobList( @@ -1529,6 +1534,14 @@ class Server extends ServerContainer implements IServerContainer { } /** + * @return ILogFactory + * @throws \OCP\AppFramework\QueryException + */ + public function getLogFactory() { + return $this->query(ILogFactory::class); + } + + /** * Returns a router for generating and matching urls * * @return \OCP\Route\IRouter diff --git a/lib/public/IServerContainer.php b/lib/public/IServerContainer.php index 19c9578ee23..c38aaf9f2cb 100644 --- a/lib/public/IServerContainer.php +++ b/lib/public/IServerContainer.php @@ -44,6 +44,7 @@ // 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 namespace OCP; +use OCP\Log\ILogFactory; use OCP\Security\IContentSecurityPolicyManager; use Symfony\Component\EventDispatcher\EventDispatcherInterface; @@ -315,6 +316,14 @@ interface IServerContainer extends IContainer { 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 * * @return \OCP\Route\IRouter diff --git a/lib/public/Log/IFileBased.php b/lib/public/Log/IFileBased.php new file mode 100644 index 00000000000..c0eef472975 --- /dev/null +++ b/lib/public/Log/IFileBased.php @@ -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; +} diff --git a/lib/public/Log/ILogFactory.php b/lib/public/Log/ILogFactory.php new file mode 100644 index 00000000000..d8e1ab4ee76 --- /dev/null +++ b/lib/public/Log/ILogFactory.php @@ -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; +} diff --git a/lib/public/Log/IWriter.php b/lib/public/Log/IWriter.php new file mode 100644 index 00000000000..c9b906bf4a3 --- /dev/null +++ b/lib/public/Log/IWriter.php @@ -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); +} diff --git a/lib/public/Log/RotationTrait.php b/lib/public/Log/RotationTrait.php new file mode 100644 index 00000000000..df42bfeff1f --- /dev/null +++ b/lib/public/Log/RotationTrait.php @@ -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; + } + +} |