aboutsummaryrefslogtreecommitdiffstats
path: root/lib/private
diff options
context:
space:
mode:
authorArthur Schiwon <blizzz@arthur-schiwon.de>2018-04-25 02:27:43 +0200
committerMorris Jobke <hey@morrisjobke.de>2018-04-26 12:10:52 +0200
commitcfc3ab0119d23a4c8ccccefb13d2271b0c0675ae (patch)
treedfab8e9dd20412ce46d21eeb42861854193ca56f /lib/private
parent5fbf184134f34633bc150b2e0210c4a97ec285a9 (diff)
downloadnextcloud-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/private')
-rw-r--r--lib/private/Log.php19
-rw-r--r--lib/private/Log/Errorlog.php6
-rw-r--r--lib/private/Log/File.php23
-rw-r--r--lib/private/Log/IWritable.php28
-rw-r--r--lib/private/Log/LogFactory.php23
-rw-r--r--lib/private/Log/Syslog.php5
-rw-r--r--lib/private/Server.php16
7 files changed, 62 insertions, 58 deletions
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/IWritable.php b/lib/private/Log/IWritable.php
deleted file mode 100644
index ff9bb4e71a8..00000000000
--- a/lib/private/Log/IWritable.php
+++ /dev/null
@@ -1,28 +0,0 @@
-<?php
-/**
- * @copyright Copyright (c) 2018 Arthur Schiwon <blizzz@arthur-schiwon.de>
- *
- * @author Arthur Schiwon <blizzz@arthur-schiwon.de>
- *
- * @license GNU AGPL version 3 or any later version
- *
- * This program is free software: you can redistribute it and/or modify
- * it under the terms of the GNU Affero General Public License as
- * published by the Free Software Foundation, either version 3 of the
- * License, or (at your option) any later version.
- *
- * This program is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- * GNU Affero General Public License for more details.
- *
- * You should have received a copy of the GNU Affero General Public License
- * along with this program. If not, see <http://www.gnu.org/licenses/>.
- *
- */
-
-namespace OC\Log;
-
-interface IWritable {
- public function write($app, $message, $level);
-}
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