From b41999a2c0628f3241b07afcae0b29bae682583c Mon Sep 17 00:00:00 2001 From: Bart Visscher Date: Sat, 9 Mar 2013 21:00:48 +0100 Subject: [PATCH 01/10] Implement OC\Log as proxy to OC_Log OC\Log implements the Psr\Log\LoggerInterface interface. See https://github.com/php-fig/fig-standards/blob/master/accepted/PSR-3-logger-interface.md for the full interface specification. --- lib/legacy/log.php | 72 +++++++++++++++++++ lib/log.php | 171 ++++++++++++++++++++++++++++++++++----------- 2 files changed, 202 insertions(+), 41 deletions(-) create mode 100644 lib/legacy/log.php diff --git a/lib/legacy/log.php b/lib/legacy/log.php new file mode 100644 index 00000000000..4e6642b6a2f --- /dev/null +++ b/lib/legacy/log.php @@ -0,0 +1,72 @@ + + * This file is licensed under the Affero General Public License version 3 or + * later. + * See the COPYING-README file. + */ + +/** + * logging utilities + * + * Log is saved by default at data/owncloud.log using OC_Log_Owncloud. + * Selecting other backend is done with a config option 'log_type'. + */ + +OC_Log::$object = new \OC\Log(); +class OC_Log { + public static $object; + + const DEBUG=0; + const INFO=1; + const WARN=2; + const ERROR=3; + const FATAL=4; + + static public $enabled = true; + static protected $class = null; + + /** + * write a message in the log + * @param string $app + * @param string $message + * @param int level + */ + public static function write($app, $message, $level) { + if (self::$enabled) { + if (!self::$class) { + self::$class = 'OC_Log_'.ucfirst(OC_Config::getValue('log_type', 'owncloud')); + call_user_func(array(self::$class, 'init')); + } + $log_class=self::$class; + $log_class::write($app, $message, $level); + } + } + + //Fatal errors handler + public static function onShutdown() { + $error = error_get_last(); + if($error) { + //ob_end_clean(); + self::write('PHP', $error['message'] . ' at ' . $error['file'] . '#' . $error['line'], self::FATAL); + } else { + return true; + } + } + + // Uncaught exception handler + public static function onException($exception) { + self::write('PHP', + $exception->getMessage() . ' at ' . $exception->getFile() . '#' . $exception->getLine(), + self::FATAL); + } + + //Recoverable errors handler + public static function onError($number, $message, $file, $line) { + if (error_reporting() === 0) { + return; + } + self::write('PHP', $message . ' at ' . $file . '#' . $line, self::WARN); + + } +} diff --git a/lib/log.php b/lib/log.php index 3f3334801e5..f7a68c30680 100644 --- a/lib/log.php +++ b/lib/log.php @@ -1,69 +1,158 @@ + * Copyright (c) 2013 Bart Visscher * This file is licensed under the Affero General Public License version 3 or * later. * See the COPYING-README file. */ +namespace OC; + /** * logging utilities * - * Log is saved by default at data/owncloud.log using OC_Log_Owncloud. - * Selecting other backend is done with a config option 'log_type'. + * This is a stand in, this should be replaced by a Psr\Log\LoggerInterface + * compatible logger. See https://github.com/php-fig/fig-standards/blob/master/accepted/PSR-3-logger-interface.md + * for the full interface specification. + * + * MonoLog is an example implementing this interface. */ -class OC_Log { +class Log { const DEBUG=0; const INFO=1; const WARN=2; const ERROR=3; const FATAL=4; - static public $enabled = true; - static protected $class = null; + const NOTICE=5; + const CRITICAL=6; + const ALERT=7; /** - * write a message in the log - * @param string $app + * System is unusable. + * * @param string $message - * @param int level + * @param array $context + * @return null */ - public static function write($app, $message, $level) { - if (self::$enabled) { - if (!self::$class) { - self::$class = 'OC_Log_'.ucfirst(OC_Config::getValue('log_type', 'owncloud')); - call_user_func(array(self::$class, 'init')); - } - $log_class=self::$class; - $log_class::write($app, $message, $level); - } + public function emergency($message, array $context = array()) + { + $this->log(OC_Log::FATAL, $message, $context); } - //Fatal errors handler - public static function onShutdown() { - $error = error_get_last(); - if($error) { - //ob_end_clean(); - self::write('PHP', $error['message'] . ' at ' . $error['file'] . '#' . $error['line'], self::FATAL); + /** + * Action must be taken immediately. + * + * Example: Entire website down, database unavailable, etc. This should + * trigger the SMS alerts and wake you up. + * + * @param string $message + * @param array $context + * @return null + */ + public function alert($message, array $context = array()) + { + $this->log(self::ALERT, $message, $context); + } + + /** + * Critical conditions. + * + * Example: Application component unavailable, unexpected exception. + * + * @param string $message + * @param array $context + * @return null + */ + public function critical($message, array $context = array()) + { + $this->log(self::CRITICAL, $message, $context); + } + + /** + * Runtime errors that do not require immediate action but should typically + * be logged and monitored. + * + * @param string $message + * @param array $context + * @return null + */ + public function error($message, array $context = array()) + { + $this->log(OC_Log::ERROR, $message, $context); + } + + /** + * Exceptional occurrences that are not errors. + * + * Example: Use of deprecated APIs, poor use of an API, undesirable things + * that are not necessarily wrong. + * + * @param string $message + * @param array $context + * @return null + */ + public function warning($message, array $context = array()) + { + $this->log(OC_Log::WARN, $message, $context); + } + + /** + * Normal but significant events. + * + * @param string $message + * @param array $context + * @return null + */ + public function notice($message, array $context = array()) + { + $this->log(self::NOTICE, $message, $context); + } + + /** + * Interesting events. + * + * Example: User logs in, SQL logs. + * + * @param string $message + * @param array $context + * @return null + */ + public function info($message, array $context = array()) + { + $this->log(OC_Log::INFO, $message, $context); + } + + /** + * Detailed debug information. + * + * @param string $message + * @param array $context + * @return null + */ + public function debug($message, array $context = array()) + { + $this->log(OC_Log::DEBUG, $message, $context); + } + + /** + * Logs with an arbitrary level. + * + * @param mixed $level + * @param string $message + * @param array $context + * @return null + */ + protected function log($level, $message, array $context = array()) + { + if (isset($context['app'])) { + $app = $context['app']; } else { - return true; + $app = 'no app in context'; } - } - - // Uncaught exception handler - public static function onException($exception) { - self::write('PHP', - $exception->getMessage() . ' at ' . $exception->getFile() . '#' . $exception->getLine(), - self::FATAL); - } - - //Recoverable errors handler - public static function onError($number, $message, $file, $line) { - if (error_reporting() === 0) { - return; - } - self::write('PHP', $message . ' at ' . $file . '#' . $line, self::WARN); - + OC_Log::write($app, $message, $level); } } + +require_once __DIR__.'/legacy/'.basename(__FILE__); From 7e5bb96027ee4409d8883afffa2cad50cdc3c782 Mon Sep 17 00:00:00 2001 From: Bart Visscher Date: Wed, 20 Mar 2013 17:36:57 +0100 Subject: [PATCH 02/10] Fix OC\Log with OC_Log in wrong namespace --- lib/log.php | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/lib/log.php b/lib/log.php index f7a68c30680..6353693a289 100644 --- a/lib/log.php +++ b/lib/log.php @@ -38,7 +38,7 @@ class Log { */ public function emergency($message, array $context = array()) { - $this->log(OC_Log::FATAL, $message, $context); + $this->log(self::FATAL, $message, $context); } /** @@ -80,7 +80,7 @@ class Log { */ public function error($message, array $context = array()) { - $this->log(OC_Log::ERROR, $message, $context); + $this->log(self::ERROR, $message, $context); } /** @@ -95,7 +95,7 @@ class Log { */ public function warning($message, array $context = array()) { - $this->log(OC_Log::WARN, $message, $context); + $this->log(self::WARN, $message, $context); } /** @@ -121,7 +121,7 @@ class Log { */ public function info($message, array $context = array()) { - $this->log(OC_Log::INFO, $message, $context); + $this->log(self::INFO, $message, $context); } /** @@ -133,7 +133,7 @@ class Log { */ public function debug($message, array $context = array()) { - $this->log(OC_Log::DEBUG, $message, $context); + $this->log(self::DEBUG, $message, $context); } /** @@ -151,7 +151,7 @@ class Log { } else { $app = 'no app in context'; } - OC_Log::write($app, $message, $level); + \OC_Log::write($app, $message, $level); } } From 0b4018e7ff5f2660850338e8ea0aaab57c48209e Mon Sep 17 00:00:00 2001 From: Bart Visscher Date: Wed, 8 May 2013 18:21:07 +0200 Subject: [PATCH 03/10] Remove include for loading legacy class --- lib/log.php | 2 -- 1 file changed, 2 deletions(-) diff --git a/lib/log.php b/lib/log.php index 6353693a289..d83f88c1df8 100644 --- a/lib/log.php +++ b/lib/log.php @@ -154,5 +154,3 @@ class Log { \OC_Log::write($app, $message, $level); } } - -require_once __DIR__.'/legacy/'.basename(__FILE__); From 31ad43f9221899b2379b3b72e43270b17fa9f881 Mon Sep 17 00:00:00 2001 From: Bart Visscher Date: Mon, 13 May 2013 08:05:53 +0200 Subject: [PATCH 04/10] Code style --- lib/log.php | 27 +++++++++------------------ 1 file changed, 9 insertions(+), 18 deletions(-) diff --git a/lib/log.php b/lib/log.php index d83f88c1df8..3bedcd4ed36 100644 --- a/lib/log.php +++ b/lib/log.php @@ -36,8 +36,7 @@ class Log { * @param array $context * @return null */ - public function emergency($message, array $context = array()) - { + public function emergency($message, array $context = array()) { $this->log(self::FATAL, $message, $context); } @@ -51,8 +50,7 @@ class Log { * @param array $context * @return null */ - public function alert($message, array $context = array()) - { + public function alert($message, array $context = array()) { $this->log(self::ALERT, $message, $context); } @@ -65,8 +63,7 @@ class Log { * @param array $context * @return null */ - public function critical($message, array $context = array()) - { + public function critical($message, array $context = array()) { $this->log(self::CRITICAL, $message, $context); } @@ -78,8 +75,7 @@ class Log { * @param array $context * @return null */ - public function error($message, array $context = array()) - { + public function error($message, array $context = array()) { $this->log(self::ERROR, $message, $context); } @@ -93,8 +89,7 @@ class Log { * @param array $context * @return null */ - public function warning($message, array $context = array()) - { + public function warning($message, array $context = array()) { $this->log(self::WARN, $message, $context); } @@ -105,8 +100,7 @@ class Log { * @param array $context * @return null */ - public function notice($message, array $context = array()) - { + public function notice($message, array $context = array()) { $this->log(self::NOTICE, $message, $context); } @@ -119,8 +113,7 @@ class Log { * @param array $context * @return null */ - public function info($message, array $context = array()) - { + public function info($message, array $context = array()) { $this->log(self::INFO, $message, $context); } @@ -131,8 +124,7 @@ class Log { * @param array $context * @return null */ - public function debug($message, array $context = array()) - { + public function debug($message, array $context = array()) { $this->log(self::DEBUG, $message, $context); } @@ -144,8 +136,7 @@ class Log { * @param array $context * @return null */ - protected function log($level, $message, array $context = array()) - { + protected function log($level, $message, array $context = array()) { if (isset($context['app'])) { $app = $context['app']; } else { From 061fb79e5c26ec1cf3432dda8c439a167bfc4e1d Mon Sep 17 00:00:00 2001 From: Bart Visscher Date: Mon, 13 May 2013 08:13:31 +0200 Subject: [PATCH 05/10] Use the constants from OC_Log --- lib/log.php | 16 +++++----------- 1 file changed, 5 insertions(+), 11 deletions(-) diff --git a/lib/log.php b/lib/log.php index 3bedcd4ed36..fc190f1378b 100644 --- a/lib/log.php +++ b/lib/log.php @@ -19,12 +19,6 @@ namespace OC; */ class Log { - const DEBUG=0; - const INFO=1; - const WARN=2; - const ERROR=3; - const FATAL=4; - const NOTICE=5; const CRITICAL=6; const ALERT=7; @@ -37,7 +31,7 @@ class Log { * @return null */ public function emergency($message, array $context = array()) { - $this->log(self::FATAL, $message, $context); + $this->log(\OC_Log::FATAL, $message, $context); } /** @@ -76,7 +70,7 @@ class Log { * @return null */ public function error($message, array $context = array()) { - $this->log(self::ERROR, $message, $context); + $this->log(\OC_Log::ERROR, $message, $context); } /** @@ -90,7 +84,7 @@ class Log { * @return null */ public function warning($message, array $context = array()) { - $this->log(self::WARN, $message, $context); + $this->log(\OC_Log::WARN, $message, $context); } /** @@ -114,7 +108,7 @@ class Log { * @return null */ public function info($message, array $context = array()) { - $this->log(self::INFO, $message, $context); + $this->log(\OC_Log::INFO, $message, $context); } /** @@ -125,7 +119,7 @@ class Log { * @return null */ public function debug($message, array $context = array()) { - $this->log(self::DEBUG, $message, $context); + $this->log(\OC_Log::DEBUG, $message, $context); } /** From 009e9559f3a12d7275ab242ececaa7f9452165c1 Mon Sep 17 00:00:00 2001 From: Bart Visscher Date: Mon, 13 May 2013 08:16:41 +0200 Subject: [PATCH 06/10] Remove default return hint --- lib/log.php | 9 --------- 1 file changed, 9 deletions(-) diff --git a/lib/log.php b/lib/log.php index fc190f1378b..442872af9c4 100644 --- a/lib/log.php +++ b/lib/log.php @@ -28,7 +28,6 @@ class Log { * * @param string $message * @param array $context - * @return null */ public function emergency($message, array $context = array()) { $this->log(\OC_Log::FATAL, $message, $context); @@ -42,7 +41,6 @@ class Log { * * @param string $message * @param array $context - * @return null */ public function alert($message, array $context = array()) { $this->log(self::ALERT, $message, $context); @@ -55,7 +53,6 @@ class Log { * * @param string $message * @param array $context - * @return null */ public function critical($message, array $context = array()) { $this->log(self::CRITICAL, $message, $context); @@ -67,7 +64,6 @@ class Log { * * @param string $message * @param array $context - * @return null */ public function error($message, array $context = array()) { $this->log(\OC_Log::ERROR, $message, $context); @@ -81,7 +77,6 @@ class Log { * * @param string $message * @param array $context - * @return null */ public function warning($message, array $context = array()) { $this->log(\OC_Log::WARN, $message, $context); @@ -92,7 +87,6 @@ class Log { * * @param string $message * @param array $context - * @return null */ public function notice($message, array $context = array()) { $this->log(self::NOTICE, $message, $context); @@ -105,7 +99,6 @@ class Log { * * @param string $message * @param array $context - * @return null */ public function info($message, array $context = array()) { $this->log(\OC_Log::INFO, $message, $context); @@ -116,7 +109,6 @@ class Log { * * @param string $message * @param array $context - * @return null */ public function debug($message, array $context = array()) { $this->log(\OC_Log::DEBUG, $message, $context); @@ -128,7 +120,6 @@ class Log { * @param mixed $level * @param string $message * @param array $context - * @return null */ protected function log($level, $message, array $context = array()) { if (isset($context['app'])) { From 2eaad589221a9a189cece52390873f9bd27f113b Mon Sep 17 00:00:00 2001 From: Bart Visscher Date: Thu, 27 Jun 2013 20:10:45 +0200 Subject: [PATCH 07/10] Make the object drive the logging backend This is the other way around then it was. --- lib/legacy/log.php | 17 +++++++++++------ lib/log.php | 12 +++++++----- 2 files changed, 18 insertions(+), 11 deletions(-) diff --git a/lib/legacy/log.php b/lib/legacy/log.php index 4e6642b6a2f..a316051d5af 100644 --- a/lib/legacy/log.php +++ b/lib/legacy/log.php @@ -23,6 +23,14 @@ class OC_Log { const ERROR=3; const FATAL=4; + static private $level_funcs = array( + self::DEBUG => 'debug', + self::INFO => 'info', + self::WARN => 'warning', + self::ERROR => 'error', + self::FATAL => 'emergency', + ); + static public $enabled = true; static protected $class = null; @@ -34,12 +42,9 @@ class OC_Log { */ public static function write($app, $message, $level) { if (self::$enabled) { - if (!self::$class) { - self::$class = 'OC_Log_'.ucfirst(OC_Config::getValue('log_type', 'owncloud')); - call_user_func(array(self::$class, 'init')); - } - $log_class=self::$class; - $log_class::write($app, $message, $level); + $context = array('app' => $app); + $func = array(self::$object, self::$level_funcs[$level]); + call_user_func($func, $message, $context); } } diff --git a/lib/log.php b/lib/log.php index 442872af9c4..2e26ccad2da 100644 --- a/lib/log.php +++ b/lib/log.php @@ -19,10 +19,6 @@ namespace OC; */ class Log { - const NOTICE=5; - const CRITICAL=6; - const ALERT=7; - /** * System is unusable. * @@ -114,6 +110,11 @@ class Log { $this->log(\OC_Log::DEBUG, $message, $context); } + public function __construct() { + $this->log_class = 'OC_Log_'.ucfirst(\OC_Config::getValue('log_type', 'owncloud')); + call_user_func(array($this->log_class, 'init')); + } + /** * Logs with an arbitrary level. * @@ -127,6 +128,7 @@ class Log { } else { $app = 'no app in context'; } - \OC_Log::write($app, $message, $level); + $log_class=$this->log_class; + $log_class::write($app, $message, $level); } } From fb80cbd4997625c2d87457d5ba0b908206474d4d Mon Sep 17 00:00:00 2001 From: Bart Visscher Date: Thu, 27 Jun 2013 22:01:52 +0200 Subject: [PATCH 08/10] Fix review points --- lib/log.php | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/lib/log.php b/lib/log.php index 2e26ccad2da..8c157d003cf 100644 --- a/lib/log.php +++ b/lib/log.php @@ -19,6 +19,8 @@ namespace OC; */ class Log { + private $logClass; + /** * System is unusable. * @@ -111,8 +113,8 @@ class Log { } public function __construct() { - $this->log_class = 'OC_Log_'.ucfirst(\OC_Config::getValue('log_type', 'owncloud')); - call_user_func(array($this->log_class, 'init')); + $this->logClass = 'OC_Log_'.ucfirst(\OC_Config::getValue('log_type', 'owncloud')); + call_user_func(array($this->logClass, 'init')); } /** @@ -122,13 +124,13 @@ class Log { * @param string $message * @param array $context */ - protected function log($level, $message, array $context = array()) { + public function log($level, $message, array $context = array()) { if (isset($context['app'])) { $app = $context['app']; } else { $app = 'no app in context'; } - $log_class=$this->log_class; - $log_class::write($app, $message, $level); + $logClass=$this->logClass; + $logClass::write($app, $message, $level); } } From de93b21505e1281c529cc2d1dc350b62bb387cca Mon Sep 17 00:00:00 2001 From: Thomas Mueller Date: Thu, 27 Jun 2013 23:14:32 +0200 Subject: [PATCH 09/10] missing $ --- lib/legacy/log.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/legacy/log.php b/lib/legacy/log.php index a316051d5af..7802ead2412 100644 --- a/lib/legacy/log.php +++ b/lib/legacy/log.php @@ -38,7 +38,7 @@ class OC_Log { * write a message in the log * @param string $app * @param string $message - * @param int level + * @param int $level */ public static function write($app, $message, $level) { if (self::$enabled) { From 1edf01d09f12843528be3137d6b6595edb9e0ec4 Mon Sep 17 00:00:00 2001 From: Bart Visscher Date: Fri, 28 Jun 2013 11:15:08 +0200 Subject: [PATCH 10/10] Fix usage of non existent consts --- lib/log.php | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/lib/log.php b/lib/log.php index 8c157d003cf..e0b9fe3c696 100644 --- a/lib/log.php +++ b/lib/log.php @@ -41,7 +41,7 @@ class Log { * @param array $context */ public function alert($message, array $context = array()) { - $this->log(self::ALERT, $message, $context); + $this->log(\OC_Log::ERROR, $message, $context); } /** @@ -53,7 +53,7 @@ class Log { * @param array $context */ public function critical($message, array $context = array()) { - $this->log(self::CRITICAL, $message, $context); + $this->log(\OC_Log::ERROR, $message, $context); } /** @@ -87,7 +87,7 @@ class Log { * @param array $context */ public function notice($message, array $context = array()) { - $this->log(self::NOTICE, $message, $context); + $this->log(\OC_Log::INFO, $message, $context); } /**