diff options
author | Arthur Schiwon <blizzz@arthur-schiwon.de> | 2018-04-24 22:14:00 +0200 |
---|---|---|
committer | Morris Jobke <hey@morrisjobke.de> | 2018-04-26 12:00:06 +0200 |
commit | 5fbf184134f34633bc150b2e0210c4a97ec285a9 (patch) | |
tree | bdc4d79196f1268cf8d5df488c9b31705ceb82a0 /lib/private/Log | |
parent | b7e8ab97e731b77ef2ec519bfb98019516b7f682 (diff) | |
download | nextcloud-server-5fbf184134f34633bc150b2e0210c4a97ec285a9.tar.gz nextcloud-server-5fbf184134f34633bc150b2e0210c4a97ec285a9.zip |
destaticfy Log classes
Signed-off-by: Arthur Schiwon <blizzz@arthur-schiwon.de>
Diffstat (limited to 'lib/private/Log')
-rw-r--r-- | lib/private/Log/Errorlog.php | 11 | ||||
-rw-r--r-- | lib/private/Log/File.php | 52 | ||||
-rw-r--r-- | lib/private/Log/IFileBased.php | 30 | ||||
-rw-r--r-- | lib/private/Log/IWritable.php | 28 | ||||
-rw-r--r-- | lib/private/Log/LogFactory.php | 66 | ||||
-rw-r--r-- | lib/private/Log/Syslog.php | 17 |
6 files changed, 155 insertions, 49 deletions
diff --git a/lib/private/Log/Errorlog.php b/lib/private/Log/Errorlog.php index 37498c36aba..1302a31fe5c 100644 --- a/lib/private/Log/Errorlog.php +++ b/lib/private/Log/Errorlog.php @@ -25,14 +25,7 @@ namespace OC\Log; -class Errorlog { - - - /** - * Init class data - */ - public static function init() { - } +class Errorlog implements IWritable { /** * write a message in the log @@ -40,7 +33,7 @@ class Errorlog { * @param string $message * @param int $level */ - public static function write($app, $message, $level) { + public function write($app, $message, $level) { error_log('[owncloud]['.$app.']['.$level.'] '.$message); } } diff --git a/lib/private/Log/File.php b/lib/private/Log/File.php index 755c4729c7a..639e4de8ac7 100644 --- a/lib/private/Log/File.php +++ b/lib/private/Log/File.php @@ -45,28 +45,21 @@ use OCP\ILogger; * Log is saved at data/nextcloud.log (on default) */ -class File { - static protected $logFile; +class File implements IWritable, IFileBased { + /** @var string */ + 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); - - /** - * 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 = '') { + $this->logFile = $path; + if (!file_exists($this->logFile)) { + if( + ( + !is_writable(dirname($this->logFile)) + || !touch($this->logFile) + ) + && $fallbackPath !== '' + ) { + $this->logFile = $fallbackPath; } } } @@ -77,7 +70,7 @@ class File { * @param string|array $message * @param int $level */ - public static function write($app, $message, $level) { + public function write($app, $message, $level) { $config = \OC::$server->getSystemConfig(); // default to ISO8601 @@ -137,9 +130,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 +152,10 @@ class File { * @param int $offset * @return array */ - public static function getEntries($limit=50, $offset=0) { - self::init(); + public function getEntries($limit=50, $offset=0) { $minLevel = \OC::$server->getSystemConfig()->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 +197,7 @@ class File { /** * @return string */ - public static function getLogFilePath() { - return self::$logFile; + public function getLogFilePath() { + return $this->logFile; } } diff --git a/lib/private/Log/IFileBased.php b/lib/private/Log/IFileBased.php new file mode 100644 index 00000000000..ae083f670e2 --- /dev/null +++ b/lib/private/Log/IFileBased.php @@ -0,0 +1,30 @@ +<?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 IFileBased { + public function getLogFilePath(); + + public function getEntries($limit=50, $offset=0); +} diff --git a/lib/private/Log/IWritable.php b/lib/private/Log/IWritable.php new file mode 100644 index 00000000000..ff9bb4e71a8 --- /dev/null +++ b/lib/private/Log/IWritable.php @@ -0,0 +1,28 @@ +<?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 new file mode 100644 index 00000000000..13049083fee --- /dev/null +++ b/lib/private/Log/LogFactory.php @@ -0,0 +1,66 @@ +<?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 OCP\IServerContainer; + +class LogFactory { + /** @var IServerContainer */ + private $c; + + public function __construct(IServerContainer $c) { + $this->c = $c; + } + + /** + * @param $type + * @return \OC\Log\Errorlog|File|\stdClass + * @throws \OCP\AppFramework\QueryException + */ + public function get($type) { + 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(); + } + } + + protected function buildLogFile() { + $config = $this->c->getConfig(); + $defaultLogFile = $config->getSystemValue('datadirectory', \OC::$SERVERROOT.'/data').'/nextcloud.log'; + $logFile = $config->getSystemValue('logfile', $defaultLogFile); + $fallback = $defaultLogFile !== $logFile ? $defaultLogFile : ''; + + return new File($logFile, $fallback); + } +} diff --git a/lib/private/Log/Syslog.php b/lib/private/Log/Syslog.php index 7b3d931ef31..be8ecdebb2e 100644 --- a/lib/private/Log/Syslog.php +++ b/lib/private/Log/Syslog.php @@ -26,22 +26,19 @@ namespace OC\Log; use OCP\ILogger; +use OCP\IConfig; -class Syslog { - static protected $levels = array( +class Syslog implements IWritable { + static 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 + public function __construct(IConfig $config) { + openlog($config->getSystemValue('syslog_tag', 'ownCloud'), LOG_PID | LOG_CONS, LOG_USER); register_shutdown_function('closelog'); } @@ -51,7 +48,7 @@ class Syslog { * @param string $message * @param int $level */ - public static function write($app, $message, $level) { + public function write($app, $message, $level) { $syslog_level = self::$levels[$level]; syslog($syslog_level, '{'.$app.'} '.$message); } |