From 3e492b83a49b96805aff4f46882773f2bcf1fad9 Mon Sep 17 00:00:00 2001 From: =?utf8?q?Roland=20H=C3=A4der?= Date: Thu, 2 Mar 2023 05:19:20 +0100 Subject: [PATCH] Continued: - introduced parameter for logging level/severity to outputMessage() - introduced logging level constants (sadly PHP doesn't know enumerations) --- .../main/interfaces/logging/class_Logger.php | 123 ++++++++++++++++++ .../debug/class_DebugMiddleware.php | 38 +++--- 2 files changed, 145 insertions(+), 16 deletions(-) create mode 100644 framework/main/interfaces/logging/class_Logger.php diff --git a/framework/main/interfaces/logging/class_Logger.php b/framework/main/interfaces/logging/class_Logger.php new file mode 100644 index 00000000..2e7a2cf8 --- /dev/null +++ b/framework/main/interfaces/logging/class_Logger.php @@ -0,0 +1,123 @@ + + * @version 0.0.0 + * @copyright Copyright (c) 2007, 2008 Roland Haeder, 2009 - 2023 Core Developer Team + * @license GNU GPL 3.0 or any newer version + * @link http://www.ship-simu.org + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU 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 General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ +interface Logger extends FrameworkInterface { + + // Logger levels + const LOGGER_LEVEL_DEBUG = 'DEBUG'; + const LOGGER_LEVEL_DEPRECATED = 'DEPRECATED'; + const LOGGER_LEVEL_INFO = 'INFO'; + const LOGGER_LEVEL_TRACE = 'TRACE'; + const LOGGER_LEVEL_WARNING = 'WARNING'; + + /** + * Outputs a trace message whether to debug instance (should be set!) or + * dies with or ptints the message. Do NEVER EVER rewrite the exit() call to + * ApplicationEntryPoint::app_exit(), this would cause an endless loop. + * + * @param $message Message we shall send out... + * @param $doPrint Whether print or die here (default: print) + * @paran $stripTags Whether to strip tags (default: false) + * @return void + * @throws InvalidArgumentException If a parameter has an invalid value + * @throws NullPointerException If this->outputInstance is NULL + * @todo Remove $doPrint parameter + */ + public function traceMessage (string $message, bool $doPrint = true, bool $stripTags = false); + + /** + * Outputs a debug message whether to debug instance (should be set!) or + * dies with or ptints the message. Do NEVER EVER rewrite the exit() call to + * ApplicationEntryPoint::app_exit(), this would cause an endless loop. + * + * @param $message Message we shall send out... + * @param $doPrint Whether print or die here (default: print) + * @paran $stripTags Whether to strip tags (default: false) + * @return void + * @throws InvalidArgumentException If a parameter has an invalid value + * @throws NullPointerException If this->outputInstance is NULL + * @todo Remove $doPrint parameter + */ + public function debugMessage (string $message, bool $doPrint = true, bool $stripTags = false); + + /** + * Outputs an informational message whether to debug instance (should be set!) or + * dies with or ptints the message. Do NEVER EVER rewrite the exit() call to + * ApplicationEntryPoint::app_exit(), this would cause an endless loop. + * + * @param $message Message we shall send out... + * @param $doPrint Whether print or die here (default: print) + * @paran $stripTags Whether to strip tags (default: false) + * @return void + * @throws InvalidArgumentException If a parameter has an invalid value + * @throws NullPointerException If this->outputInstance is NULL + * @todo Remove $doPrint parameter + */ + public function infoMessage (string $message, bool $doPrint = true, bool $stripTags = false); + + /** + * Outputs a warning message whether to debug instance (should be set!) or + * dies with or ptints the message. Do NEVER EVER rewrite the exit() call to + * ApplicationEntryPoint::app_exit(), this would cause an endless loop. + * + * @param $message Message we shall send out... + * @param $doPrint Whether print or die here (default: print) + * @paran $stripTags Whether to strip tags (default: false) + * @return void + * @throws InvalidArgumentException If a parameter has an invalid value + * @throws NullPointerException If this->outputInstance is NULL + * @todo Remove $doPrint parameter + */ + public function warningMessage (string $message, bool $doPrint = true, bool $stripTags = false); + + /** + * Output a partial stub message for the caller method + * + * @param $message An optional message to display + * @return void + */ + public function partialStub (string $message = ''); + + /** + * Outputs a deprecated message whether to debug instance (should be set!) or + * dies with or ptints the message. Do NEVER EVER rewrite the exit() call to + * ApplicationEntryPoint::app_exit(), this would cause an endless loop. + * + * @param $message Message we shall send out... + * @param $doPrint Whether print or die here (default: print) + * @paran $stripTags Whether to strip tags (default: false) + * @return void + * @throws InvalidArgumentException If a parameter has an invalid value + * @throws NullPointerException If this->outputInstance is NULL + * @todo Remove $doPrint parameter + * @todo When all old method invocations are fixed, renamed this do deprecatedMessage + */ + public function debugOutput (string $message, bool $doPrint = true, bool $stripTags = false); + +} diff --git a/framework/main/middleware/debug/class_DebugMiddleware.php b/framework/main/middleware/debug/class_DebugMiddleware.php index eda4d5f9..c83389f0 100644 --- a/framework/main/middleware/debug/class_DebugMiddleware.php +++ b/framework/main/middleware/debug/class_DebugMiddleware.php @@ -6,6 +6,7 @@ namespace Org\Mxchange\CoreFramework\Middleware\Debug; use Org\Mxchange\CoreFramework\Factory\Object\ObjectFactory; use Org\Mxchange\CoreFramework\Generic\FrameworkInterface; use Org\Mxchange\CoreFramework\Generic\NullPointerException; +use Org\Mxchange\CoreFramework\Logging\Logger; use Org\Mxchange\CoreFramework\Middleware\BaseMiddleware; use Org\Mxchange\CoreFramework\Registry\Registerable; use Org\Mxchange\CoreFramework\Stream\Output\OutputStreamer; @@ -38,7 +39,7 @@ use \InvalidArgumentException; * You should have received a copy of the GNU General Public License * along with this program. If not, see . */ -class DebugMiddleware extends BaseMiddleware implements Registerable { +class DebugMiddleware extends BaseMiddleware implements Registerable, Logger { /** * An instance of this class */ @@ -122,37 +123,40 @@ class DebugMiddleware extends BaseMiddleware implements Registerable { * @return void * @throws NullPointerException If this->outputInstance is NULL */ - private function outputMessage (string $message, bool $stripTags = false) { + private function outputMessage (string $logLevel, string $message, bool $stripTags = false) { // Get backtrace - //* NOISY-DEBUG: */ printf('[%s:%d]: message=%s,stripTags=%d - CALLED!' . PHP_EOL, __METHOD__, __LINE__, $message, intval($stripTags)); + //* NOISY-DEBUG: */ printf('[%s:%d]: logLevel=%s,message=%s,stripTags=%d - CALLED!' . PHP_EOL, __METHOD__, __LINE__, $logLevel, $message, intval($stripTags)); $backtrace = debug_backtrace(!DEBUG_BACKTRACE_PROVIDE_OBJECT); // Is the deprecated debugOutput() or partialStub() invoked before? if (isset($backtrace[4]) && $backtrace[3]['function'] == 'partialStub') { // Prepend class::function:line from 2nd element //* NOISY-DEBUG: */ printf('[%s:%d]: partialStub() was invoked ...' . PHP_EOL, __METHOD__, __LINE__); - $message = sprintf('[%s::%s:%d]: %s', + $message = sprintf('[%s::%s:%d]: [%s] %s', $backtrace[4]['class'], $backtrace[4]['function'], (isset($backtrace[4]['line']) ? $backtrace[4]['line'] : '0'), + $logLevel, $message ); } elseif (isset($backtrace[3]) && $backtrace[2]['function'] == 'debugOutput') { // Prepend class::function:line from 2nd element //* NOISY-DEBUG: */ printf('[%s:%d]: debugOutput() was invoked ...' . PHP_EOL, __METHOD__, __LINE__); - $message = sprintf('[%s::%s:%d]: DEPRECATED: %s', + $message = sprintf('[%s::%s:%d]: [%s] %s', $backtrace[3]['class'], $backtrace[3]['function'], (isset($backtrace[3]['line']) ? $backtrace[3]['line'] : '0'), + $logLevel, $message ); } else { // Prepend class::function:line from 2nd element //* NOISY-DEBUG: */ printf('[%s:%d]: Ordinary invocation ...' . PHP_EOL, __METHOD__, __LINE__); - $message = sprintf('[%s::%s:%d]: %s', + $message = sprintf('[%s::%s:%d]: [%s] %s', $backtrace[2]['class'], $backtrace[2]['function'], (isset($backtrace[2]['line']) ? $backtrace[2]['line'] : '0'), + $logLevel, $message ); } @@ -198,8 +202,8 @@ class DebugMiddleware extends BaseMiddleware implements Registerable { } // Use debug output handler - //* NOISY-DEBUG: */ printf('[%s:%d]: Invoking this->outputMessage(%s,%d) ...' . PHP_EOL, __METHOD__, __LINE__, $message, intval($stripTags)); - $this->outputMessage(sprintf('TRACE: %s', $message), $stripTags); + //* NOISY-DEBUG: */ printf('[%s:%d]: Invoking this->outputMessage(%s,%s,%d) ...' . PHP_EOL, __METHOD__, __LINE__, Logger::LOGGER_LEVEL_TRACE, $message, intval($stripTags)); + $this->outputMessage(Logger::LOGGER_LEVEL_TRACE, $message, $stripTags); // Trace message //* NOISY-DEBUG: */ printf('[%s:%d]: EXIT!' . PHP_EOL, __METHOD__, __LINE__); @@ -230,8 +234,8 @@ class DebugMiddleware extends BaseMiddleware implements Registerable { } // Use debug output handler - //* NOISY-DEBUG: */ printf('[%s:%d]: Invoking this->outputMessage(%s,%d) ...' . PHP_EOL, __METHOD__, __LINE__, $message, intval($stripTags)); - $this->outputMessage(sprintf('DEBUG: %s', $message), $stripTags); + //* NOISY-DEBUG: */ printf('[%s:%d]: Invoking this->outputMessage(%s,%s,%d) ...' . PHP_EOL, __METHOD__, __LINE__, $message, intval($stripTags)); + $this->outputMessage(Logger::LOGGER_LEVEL_DEBUG, $message, $stripTags); // Trace message //* NOISY-DEBUG: */ printf('[%s:%d]: EXIT!' . PHP_EOL, __METHOD__, __LINE__); @@ -262,8 +266,8 @@ class DebugMiddleware extends BaseMiddleware implements Registerable { } // Use debug output handler - //* NOISY-DEBUG: */ printf('[%s:%d]: Invoking this->outputMessage(%s,%d) ...' . PHP_EOL, __METHOD__, __LINE__, $message, intval($stripTags)); - $this->outputMessage(sprintf('INFO: %s', $message), $stripTags); + //* NOISY-DEBUG: */ printf('[%s:%d]: Invoking this->outputMessage(%s,%s,%d) ...' . PHP_EOL, __METHOD__, __LINE__, Logger::LOGGER_LEVEL_INFO, $message, intval($stripTags)); + $this->outputMessage(Logger::LOGGER_LEVEL_INFO, $message, $stripTags); // Trace message //* NOISY-DEBUG: */ printf('[%s:%d]: EXIT!' . PHP_EOL, __METHOD__, __LINE__); @@ -294,8 +298,8 @@ class DebugMiddleware extends BaseMiddleware implements Registerable { } // Use debug output handler - //* NOISY-DEBUG: */ printf('[%s:%d]: Invoking this->outputMessage(%s,%d) ...' . PHP_EOL, __METHOD__, __LINE__, $message, intval($stripTags)); - $this->outputMessage(sprintf('WARNING: %s', $message), $stripTags); + //* NOISY-DEBUG: */ printf('[%s:%d]: Invoking this->outputMessage(%s,%s,%d) ...' . PHP_EOL, __METHOD__, __LINE__, Logger::LOGGER_LEVEL_WARNING, $message, intval($stripTags)); + $this->outputMessage(Logger::LOGGER_LEVEL_WARNING, $message, $stripTags); // Trace message //* NOISY-DEBUG: */ printf('[%s:%d]: EXIT!' . PHP_EOL, __METHOD__, __LINE__); @@ -319,7 +323,8 @@ class DebugMiddleware extends BaseMiddleware implements Registerable { } // Output stub message - $this->outputMessage($stubMessage); + //* NOISY-DEBUG: */ printf('[%s:%d]: Invoking this->outputMessage(%s,%s) ...' . PHP_EOL, __METHOD__, __LINE__, Logger::LOGGER_LEVEL_WARNING, $subMessage); + $this->outputMessage(Logger::LOGGER_LEVEL_WARNING, $stubMessage); // Trace message //* NOISY-DEBUG: */ printf('[%s:%d]: EXIT!' . PHP_EOL, __METHOD__, __LINE__); @@ -351,7 +356,8 @@ class DebugMiddleware extends BaseMiddleware implements Registerable { } // Invoke Inner method - $this->outputMessage(sprintf('DEPRECATED: %s', $message), $doPrint, $stripTags); + //* NOISY-DEBUG: */ printf('[%s:%d]: Invoking this->outputMessage(%s,%s) ...' . PHP_EOL, __METHOD__, __LINE__, Logger::LOGGER_LEVEL_DEPRECATED, $subMessage); + $this->outputMessage(Logger::LOGGER_LEVEL_DEPRECATED, $message, $doPrint, $stripTags); // Trace message //* NOISY-DEBUG: */ printf('[%s:%d]: EXIT!' . PHP_EOL, __METHOD__, __LINE__); -- 2.39.5