summaryrefslogtreecommitdiffstats
path: root/lib/private/Log
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/Log
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/Log')
-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
5 files changed, 37 insertions, 48 deletions
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);
}