From 03f1a270cc4476f9396f256db1967ee3a18d6e83 Mon Sep 17 00:00:00 2001 From: Roeland Jago Douma Date: Mon, 2 May 2016 14:06:20 +0200 Subject: Move \OC\Log to PSR-4 --- lib/private/Log/ErrorHandler.php | 100 +++++++++++++++++++++ lib/private/Log/Errorlog.php | 47 ++++++++++ lib/private/Log/Owncloud.php | 182 +++++++++++++++++++++++++++++++++++++++ lib/private/Log/Rotate.php | 50 +++++++++++ lib/private/Log/Syslog.php | 54 ++++++++++++ 5 files changed, 433 insertions(+) create mode 100644 lib/private/Log/ErrorHandler.php create mode 100644 lib/private/Log/Errorlog.php create mode 100644 lib/private/Log/Owncloud.php create mode 100644 lib/private/Log/Rotate.php create mode 100644 lib/private/Log/Syslog.php (limited to 'lib/private/Log') diff --git a/lib/private/Log/ErrorHandler.php b/lib/private/Log/ErrorHandler.php new file mode 100644 index 00000000000..8899bcfcb03 --- /dev/null +++ b/lib/private/Log/ErrorHandler.php @@ -0,0 +1,100 @@ + + * @author Björn Schießle + * @author Morris Jobke + * @author Thomas Müller + * + * @copyright Copyright (c) 2016, ownCloud, Inc. + * @license AGPL-3.0 + * + * This code is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License, version 3, + * as published by the Free Software Foundation. + * + * 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, version 3, + * along with this program. If not, see + * + */ + +namespace OC\Log; + +use OCP\ILogger; + +class ErrorHandler { + /** @var ILogger */ + private static $logger; + + /** + * remove password in URLs + * @param string $msg + * @return string + */ + protected static function removePassword($msg) { + return preg_replace('/\/\/(.*):(.*)@/', '//xxx:xxx@', $msg); + } + + public static function register($debug=false) { + $handler = new ErrorHandler(); + + if ($debug) { + set_error_handler(array($handler, 'onAll'), E_ALL); + if (\OC::$CLI) { + set_exception_handler(array('OC_Template', 'printExceptionErrorPage')); + } + } else { + set_error_handler(array($handler, 'onError')); + } + register_shutdown_function(array($handler, 'onShutdown')); + set_exception_handler(array($handler, 'onException')); + } + + public static function setLogger(ILogger $logger) { + self::$logger = $logger; + } + + //Fatal errors handler + public static function onShutdown() { + $error = error_get_last(); + if($error && self::$logger) { + //ob_end_clean(); + $msg = $error['message'] . ' at ' . $error['file'] . '#' . $error['line']; + self::$logger->critical(self::removePassword($msg), array('app' => 'PHP')); + } + } + + /** + * Uncaught exception handler + * + * @param \Exception $exception + */ + public static function onException($exception) { + $class = get_class($exception); + $msg = $exception->getMessage(); + $msg = "$class: $msg at " . $exception->getFile() . '#' . $exception->getLine(); + self::$logger->critical(self::removePassword($msg), ['app' => 'PHP']); + } + + //Recoverable errors handler + public static function onError($number, $message, $file, $line) { + if (error_reporting() === 0) { + return; + } + $msg = $message . ' at ' . $file . '#' . $line; + self::$logger->error(self::removePassword($msg), array('app' => 'PHP')); + + } + + //Recoverable handler which catch all errors, warnings and notices + public static function onAll($number, $message, $file, $line) { + $msg = $message . ' at ' . $file . '#' . $line; + self::$logger->debug(self::removePassword($msg), array('app' => 'PHP')); + + } + +} diff --git a/lib/private/Log/Errorlog.php b/lib/private/Log/Errorlog.php new file mode 100644 index 00000000000..37498c36aba --- /dev/null +++ b/lib/private/Log/Errorlog.php @@ -0,0 +1,47 @@ + + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + * THE SOFTWARE. + */ + +namespace OC\Log; + +class Errorlog { + + + /** + * Init class data + */ + public static function init() { + } + + /** + * write a message in the log + * @param string $app + * @param string $message + * @param int $level + */ + public static function write($app, $message, $level) { + error_log('[owncloud]['.$app.']['.$level.'] '.$message); + } +} + diff --git a/lib/private/Log/Owncloud.php b/lib/private/Log/Owncloud.php new file mode 100644 index 00000000000..13997a0d552 --- /dev/null +++ b/lib/private/Log/Owncloud.php @@ -0,0 +1,182 @@ + + * @author Bart Visscher + * @author Christian Schnidrig + * @author Georg Ehrke + * @author Lukas Reschke + * @author Michael Gapczynski + * @author Morris Jobke + * @author Robin Appelman + * @author Thomas Müller + * + * @copyright Copyright (c) 2016, ownCloud, Inc. + * @license AGPL-3.0 + * + * This code is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License, version 3, + * as published by the Free Software Foundation. + * + * 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, version 3, + * along with this program. If not, see + * + */ + +namespace OC\Log; + +/** + * logging utilities + * + * Log is saved at data/owncloud.log (on default) + */ + +class Owncloud { + static protected $logFile; + + /** + * Init class data + */ + public static function init() { + $systemConfig = \OC::$server->getSystemConfig(); + $defaultLogFile = $systemConfig->getValue("datadirectory", \OC::$SERVERROOT.'/data').'/owncloud.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; + } + } + } + } + + /** + * write a message in the log + * @param string $app + * @param string $message + * @param int $level + */ + public static function write($app, $message, $level) { + $config = \OC::$server->getSystemConfig(); + + // default to ISO8601 + $format = $config->getValue('logdateformat', 'c'); + $logTimeZone = $config->getValue( "logtimezone", 'UTC' ); + try { + $timezone = new \DateTimeZone($logTimeZone); + } catch (\Exception $e) { + $timezone = new \DateTimeZone('UTC'); + } + $time = \DateTime::createFromFormat("U.u", number_format(microtime(true), 4, ".", "")); + if ($time === false) { + $time = new \DateTime(null, $timezone); + } else { + // apply timezone if $time is created from UNIX timestamp + $time->setTimezone($timezone); + } + $request = \OC::$server->getRequest(); + $reqId = $request->getId(); + $remoteAddr = $request->getRemoteAddress(); + // remove username/passwords from URLs before writing the to the log file + $time = $time->format($format); + $url = ($request->getRequestUri() !== '') ? $request->getRequestUri() : '--'; + $method = is_string($request->getMethod()) ? $request->getMethod() : '--'; + if(\OC::$server->getConfig()->getSystemValue('installed', false)) { + $userObj = \OC::$server->getUserSession()->getUser(); + } else { + $userObj = null; + } + $user = !is_null($userObj) ? $userObj->getUID() : '--'; + $entry = compact( + 'reqId', + 'remoteAddr', + 'app', + 'message', + 'level', + 'time', + 'method', + 'url', + 'user' + ); + $entry = json_encode($entry); + $handle = @fopen(self::$logFile, 'a'); + @chmod(self::$logFile, 0640); + if ($handle) { + fwrite($handle, $entry."\n"); + fclose($handle); + } else { + // Fall back to error_log + error_log($entry); + } + if (php_sapi_name() === 'cli-server') { + error_log($message, 4); + } + } + + /** + * get entries from the log in reverse chronological order + * @param int $limit + * @param int $offset + * @return array + */ + public static function getEntries($limit=50, $offset=0) { + self::init(); + $minLevel = \OC::$server->getSystemConfig()->getValue("loglevel", \OCP\Util::WARN); + $entries = array(); + $handle = @fopen(self::$logFile, 'rb'); + if ($handle) { + fseek($handle, 0, SEEK_END); + $pos = ftell($handle); + $line = ''; + $entriesCount = 0; + $lines = 0; + // Loop through each character of the file looking for new lines + while ($pos >= 0 && ($limit === null ||$entriesCount < $limit)) { + fseek($handle, $pos); + $ch = fgetc($handle); + if ($ch == "\n" || $pos == 0) { + if ($line != '') { + // Add the first character if at the start of the file, + // because it doesn't hit the else in the loop + if ($pos == 0) { + $line = $ch.$line; + } + $entry = json_decode($line); + // Add the line as an entry if it is passed the offset and is equal or above the log level + if ($entry->level >= $minLevel) { + $lines++; + if ($lines > $offset) { + $entries[] = $entry; + $entriesCount++; + } + } + $line = ''; + } + } else { + $line = $ch.$line; + } + $pos--; + } + fclose($handle); + } + return $entries; + } + + /** + * @return string + */ + public static function getLogFilePath() { + return self::$logFile; + } +} diff --git a/lib/private/Log/Rotate.php b/lib/private/Log/Rotate.php new file mode 100644 index 00000000000..458661c82d0 --- /dev/null +++ b/lib/private/Log/Rotate.php @@ -0,0 +1,50 @@ + + * @author Morris Jobke + * @author Thomas Müller + * + * @copyright Copyright (c) 2016, ownCloud, Inc. + * @license AGPL-3.0 + * + * This code is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License, version 3, + * as published by the Free Software Foundation. + * + * 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, version 3, + * along with this program. If not, see + * + */ + +namespace OC\Log; + +/** + * This rotates the current logfile to a new name, this way the total log usage + * will stay limited and older entries are available for a while longer. + * For more professional log management set the 'logfile' config to a different + * location and manage that with your own tools. + */ +class Rotate extends \OC\BackgroundJob\Job { + private $max_log_size; + public function run($logFile) { + $this->max_log_size = \OC::$server->getConfig()->getSystemValue('log_rotate_size', false); + if ($this->max_log_size) { + $filesize = @filesize($logFile); + if ($filesize >= $this->max_log_size) { + $this->rotate($logFile); + } + } + } + + 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('OC\Log\Rotate', $msg, \OCP\Util::WARN); + } +} diff --git a/lib/private/Log/Syslog.php b/lib/private/Log/Syslog.php new file mode 100644 index 00000000000..115103f26d6 --- /dev/null +++ b/lib/private/Log/Syslog.php @@ -0,0 +1,54 @@ + + * @author Morris Jobke + * @author Thomas Müller + * + * @copyright Copyright (c) 2016, ownCloud, Inc. + * @license AGPL-3.0 + * + * This code is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License, version 3, + * as published by the Free Software Foundation. + * + * 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, version 3, + * along with this program. If not, see + * + */ + +namespace OC\Log; + +class Syslog { + static protected $levels = array( + \OCP\Util::DEBUG => LOG_DEBUG, + \OCP\Util::INFO => LOG_INFO, + \OCP\Util::WARN => LOG_WARNING, + \OCP\Util::ERROR => LOG_ERR, + \OCP\Util::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'); + } + + /** + * write a message in the log + * @param string $app + * @param string $message + * @param int $level + */ + public static function write($app, $message, $level) { + $syslog_level = self::$levels[$level]; + syslog($syslog_level, '{'.$app.'} '.$message); + } +} -- cgit v1.2.3