diff options
author | Arthur Schiwon <blizzz@arthur-schiwon.de> | 2018-04-25 02:27:43 +0200 |
---|---|---|
committer | Morris Jobke <hey@morrisjobke.de> | 2018-04-26 12:10:52 +0200 |
commit | cfc3ab0119d23a4c8ccccefb13d2271b0c0675ae (patch) | |
tree | dfab8e9dd20412ce46d21eeb42861854193ca56f /lib | |
parent | 5fbf184134f34633bc150b2e0210c4a97ec285a9 (diff) | |
download | nextcloud-server-cfc3ab0119d23a4c8ccccefb13d2271b0c0675ae.tar.gz nextcloud-server-cfc3ab0119d23a4c8ccccefb13d2271b0c0675ae.zip |
offer API to create own File log. admin_audit makes use of it
Signed-off-by: Arthur Schiwon <blizzz@arthur-schiwon.de>
Diffstat (limited to 'lib')
-rw-r--r-- | lib/composer/composer/autoload_classmap.php | 3 | ||||
-rw-r--r-- | lib/composer/composer/autoload_static.php | 3 | ||||
-rw-r--r-- | lib/private/Log.php | 19 | ||||
-rw-r--r-- | lib/private/Log/Errorlog.php | 6 | ||||
-rw-r--r-- | lib/private/Log/File.php | 23 | ||||
-rw-r--r-- | lib/private/Log/LogFactory.php | 23 | ||||
-rw-r--r-- | lib/private/Log/Syslog.php | 5 | ||||
-rw-r--r-- | lib/private/Server.php | 16 | ||||
-rw-r--r-- | lib/public/IServerContainer.php | 9 | ||||
-rw-r--r-- | lib/public/Log/ILogFactory.php | 48 | ||||
-rw-r--r-- | lib/public/Log/IWriter.php (renamed from lib/private/Log/IWritable.php) | 15 |
11 files changed, 135 insertions, 35 deletions
diff --git a/lib/composer/composer/autoload_classmap.php b/lib/composer/composer/autoload_classmap.php index f706fea2cbe..2c079a2c295 100644 --- a/lib/composer/composer/autoload_classmap.php +++ b/lib/composer/composer/autoload_classmap.php @@ -239,6 +239,8 @@ 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\\ILogFactory' => $baseDir . '/lib/public/Log/ILogFactory.php', + 'OCP\\Log\\IWriter' => $baseDir . '/lib/public/Log/IWriter.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', @@ -751,7 +753,6 @@ return array( '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', diff --git a/lib/composer/composer/autoload_static.php b/lib/composer/composer/autoload_static.php index b879dc5a214..f76c626a069 100644 --- a/lib/composer/composer/autoload_static.php +++ b/lib/composer/composer/autoload_static.php @@ -269,6 +269,8 @@ 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\\ILogFactory' => __DIR__ . '/../../..' . '/lib/public/Log/ILogFactory.php', + 'OCP\\Log\\IWriter' => __DIR__ . '/../../..' . '/lib/public/Log/IWriter.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', @@ -781,7 +783,6 @@ class ComposerStaticInit53792487c5a8370acc0b06b1a864ff4c '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', diff --git a/lib/private/Log.php b/lib/private/Log.php index c6f676db588..313201460a2 100644 --- a/lib/private/Log.php +++ b/lib/private/Log.php @@ -39,7 +39,8 @@ use InterfaSys\LogNormalizer\Normalizer; use OC\Log\ExceptionSerializer; use OC\Log\IFileBased; -use OC\Log\IWritable; +use OCP\IConfig; +use OCP\Log\IWriter; use OCP\ILogger; use OCP\Support\CrashReport\IRegistry; use OCP\Util; @@ -55,10 +56,10 @@ use OCP\Util; */ class Log implements ILogger { - /** @var IWritable */ + /** @var IWriter */ private $logger; - /** @var SystemConfig */ + /** @var IConfig */ private $config; /** @var boolean|null cache the result of the log condition check for the request */ @@ -71,15 +72,15 @@ class Log implements ILogger { private $crashReporters; /** - * @param IWritable $logger The logger that should be used - * @param SystemConfig $config the system config object + * @param IWriter $logger The logger that should be used + * @param IConfig $config the system config object * @param Normalizer|null $normalizer * @param IRegistry|null $registry */ - public function __construct(IWritable $logger, SystemConfig $config = null, $normalizer = null, IRegistry $registry = null) { + public function __construct(IWriter $logger, IConfig $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(); + $config = \OC::$server->getConfig(); } $this->config = $config; @@ -257,7 +258,7 @@ class Log implements ILogger { } if (isset($context['app'])) { - $logCondition = $this->config->getValue('log.condition', []); + $logCondition = $this->config->getSystemValue('log.condition', []); $app = $context['app']; /** @@ -271,7 +272,7 @@ class Log implements ILogger { } } - return min($this->config->getValue('loglevel', ILogger::WARN), ILogger::FATAL); + return min($this->config->getSystemValue('loglevel', ILogger::WARN), ILogger::FATAL); } /** diff --git a/lib/private/Log/Errorlog.php b/lib/private/Log/Errorlog.php index 1302a31fe5c..9dc8b2cc49c 100644 --- a/lib/private/Log/Errorlog.php +++ b/lib/private/Log/Errorlog.php @@ -25,7 +25,9 @@ namespace OC\Log; -class Errorlog implements IWritable { +use OCP\Log\IWriter; + +class Errorlog implements IWriter { /** * write a message in the log @@ -33,7 +35,7 @@ class Errorlog implements IWritable { * @param string $message * @param int $level */ - public 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 639e4de8ac7..6e95de229cd 100644 --- a/lib/private/Log/File.php +++ b/lib/private/Log/File.php @@ -36,6 +36,8 @@ */ namespace OC\Log; +use OCP\IConfig; +use OCP\Log\IWriter; use OCP\ILogger; @@ -45,11 +47,13 @@ use OCP\ILogger; * Log is saved at data/nextcloud.log (on default) */ -class File implements IWritable, IFileBased { +class File implements IWriter, IFileBased { /** @var string */ protected $logFile; + /** @var IConfig */ + private $config; - public function __construct(string $path, string $fallbackPath = '') { + public function __construct(string $path, string $fallbackPath = '', IConfig $config) { $this->logFile = $path; if (!file_exists($this->logFile)) { if( @@ -62,6 +66,7 @@ class File implements IWritable, IFileBased { $this->logFile = $fallbackPath; } } + $this->config = $config; } /** @@ -70,12 +75,10 @@ class File implements IWritable, IFileBased { * @param string|array $message * @param int $level */ - public 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->getSystemValue('logdateformat', \DateTime::ATOM); + $logTimeZone = $this->config->getSystemValue('logtimezone', 'UTC'); try { $timezone = new \DateTimeZone($logTimeZone); } catch (\Exception $e) { @@ -95,7 +98,7 @@ class File implements IWritable, IFileBased { $time = $time->format($format); $url = ($request->getRequestUri() !== '') ? $request->getRequestUri() : '--'; $method = is_string($request->getMethod()) ? $request->getMethod() : '--'; - if($config->getValue('installed', false)) { + if($this->config->getSystemValue('installed', false)) { $user = \OC_User::getUser() ? \OC_User::getUser() : '--'; } else { $user = '--'; @@ -104,7 +107,7 @@ class File implements IWritable, IFileBased { if ($userAgent === '') { $userAgent = '--'; } - $version = $config->getValue('version', ''); + $version = $this->config->getSystemValue('version', ''); $entry = compact( 'reqId', 'level', @@ -153,7 +156,7 @@ class File implements IWritable, IFileBased { * @return array */ public function getEntries($limit=50, $offset=0) { - $minLevel = \OC::$server->getSystemConfig()->getValue("loglevel", ILogger::WARN); + $minLevel = $this->config->getSystemValue("loglevel", ILogger::WARN); $entries = array(); $handle = @fopen($this->logFile, 'rb'); if ($handle) { diff --git a/lib/private/Log/LogFactory.php b/lib/private/Log/LogFactory.php index 13049083fee..c1a4d00ceaa 100644 --- a/lib/private/Log/LogFactory.php +++ b/lib/private/Log/LogFactory.php @@ -23,9 +23,13 @@ namespace OC\Log; +use OC\Log; +use OCP\ILogger; use OCP\IServerContainer; +use OCP\Log\ILogFactory; +use OCP\Log\IWriter; -class LogFactory { +class LogFactory implements ILogFactory { /** @var IServerContainer */ private $c; @@ -38,7 +42,7 @@ class LogFactory { * @return \OC\Log\Errorlog|File|\stdClass * @throws \OCP\AppFramework\QueryException */ - public function get($type) { + public function get(string $type):IWriter { switch (strtolower($type)) { case 'errorlog': return new Errorlog(); @@ -51,16 +55,23 @@ class LogFactory { case 'owncloud': case 'nextcloud': default: - return $this->buildLogFile(); + return $this->buildLogFile(); } } - protected function buildLogFile() { + public function getCustomLogger(string $path):ILogger { + $log = $this->buildLogFile($path); + return new Log($log, $this->c->getConfig()); + } + + protected function buildLogFile(string $logFile = ''):File { $config = $this->c->getConfig(); $defaultLogFile = $config->getSystemValue('datadirectory', \OC::$SERVERROOT.'/data').'/nextcloud.log'; - $logFile = $config->getSystemValue('logfile', $defaultLogFile); + if($logFile === '') { + $logFile = $config->getSystemValue('logfile', $defaultLogFile); + } $fallback = $defaultLogFile !== $logFile ? $defaultLogFile : ''; - return new File($logFile, $fallback); + return new File($logFile, $fallback, $config); } } diff --git a/lib/private/Log/Syslog.php b/lib/private/Log/Syslog.php index be8ecdebb2e..7bd7467f5c4 100644 --- a/lib/private/Log/Syslog.php +++ b/lib/private/Log/Syslog.php @@ -27,8 +27,9 @@ namespace OC\Log; use OCP\ILogger; use OCP\IConfig; +use OCP\Log\IWriter; -class Syslog implements IWritable { +class Syslog implements IWriter { static protected $levels = [ ILogger::DEBUG => LOG_DEBUG, ILogger::INFO => LOG_INFO, @@ -48,7 +49,7 @@ class Syslog implements IWritable { * @param string $message * @param int $level */ - public function write($app, $message, $level) { + public function write(string $app, $message, int $level) { $syslog_level = self::$levels[$level]; syslog($syslog_level, '{'.$app.'} '.$message); } diff --git a/lib/private/Server.php b/lib/private/Server.php index a4608bf7a8f..05772555a03 100644 --- a/lib/private/Server.php +++ b/lib/private/Server.php @@ -135,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; @@ -549,13 +550,18 @@ class Server extends ServerContainer implements IServerContainer { $logType = $c->query('AllConfig')->getSystemValue('log_type', 'file'); $factory = new LogFactory($c); $logger = $factory->get($logType); - $config = $this->getSystemConfig(); + $config = $this->getConfig(); $registry = $c->query(\OCP\Support\CrashReport\IRegistry::class); return new Log($logger, $config, null, $registry); }); $this->registerAlias('Logger', \OCP\ILogger::class); + $this->registerService(ILogFactory::class, function (Server $c) { + return new LogFactory($c); + }); + $this->registerAlias('LogFactory', ILogFactory::class); + $this->registerService(\OCP\BackgroundJob\IJobList::class, function (Server $c) { $config = $c->getConfig(); return new \OC\BackgroundJob\JobList( @@ -1530,6 +1536,14 @@ class Server extends ServerContainer implements IServerContainer { } /** + * @return ILogFactory + * @throws \OCP\AppFramework\QueryException + */ + public function getLogFactory() { + return $this->query('LogFactory'); + } + + /** * 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/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/private/Log/IWritable.php b/lib/public/Log/IWriter.php index ff9bb4e71a8..c9b906bf4a3 100644 --- a/lib/private/Log/IWritable.php +++ b/lib/public/Log/IWriter.php @@ -21,8 +21,17 @@ * */ -namespace OC\Log; +namespace OCP\Log; -interface IWritable { - public function write($app, $message, $level); +/** + * Interface IWriter + * + * @package OCP\Log + * @since 14.0.0 + */ +interface IWriter { + /** + * @since 14.0.0 + */ + public function write(string $app, $message, int $level); } |