From 53f597f9478177eea696acef8170fbda10e5f776 Mon Sep 17 00:00:00 2001 From: Abel Hoogeveen Date: Thu, 2 Jun 2016 15:19:31 +0200 Subject: [PATCH] Implemented a new system for Logging templates. This allows the user to install new Logging templates that run in specific circumstances. By default the FuzeWorks default (as you already know it) template is installed, and a CLI one. Closes #88 --- Application/Config/config.error.php | 8 +-- Core/System/class.logger.php | 73 ++++++++++++++--------- Core/Views/view.logger_cli.php | 91 +++++++++++++++++++++++++++++ Core/Views/view.logger_default.php | 27 +++++++++ tests/autoload.php | 5 +- 5 files changed, 172 insertions(+), 32 deletions(-) create mode 100644 Core/Views/view.logger_cli.php create mode 100644 Core/Views/view.logger_default.php diff --git a/Application/Config/config.error.php b/Application/Config/config.error.php index 251acec..843e44f 100644 --- a/Application/Config/config.error.php +++ b/Application/Config/config.error.php @@ -1,6 +1,6 @@ - false, 'error_reporting' => true, -); + 'log_to_file' => false, + 'logger_template' => 'logger_default', +) ; \ No newline at end of file diff --git a/Core/System/class.logger.php b/Core/System/class.logger.php index af5aee4..8247819 100644 --- a/Core/System/class.logger.php +++ b/Core/System/class.logger.php @@ -87,6 +87,20 @@ class Logger { */ private static $print_to_screen = false; + /** + * whether to output the log to a file after FuzeWorks has run. + * + * @var bool + */ + private static $log_to_file = false; + + /** + * The template to use when parsing the debug log + * + * @var string Template name + */ + private static $logger_template = 'logger_default'; + /** * whether to output the log after FuzeWorks has run, regardless of conditions. * @@ -114,6 +128,8 @@ class Logger { error_reporting(false); } self::$debug = Config::get('error')->debug; + self::$log_to_file = Config::get('error')->log_to_file; + self::$logger_template = Config::get('error')->logger_template; self::newLevel('Logger Initiated'); } @@ -145,7 +161,12 @@ class Logger { if (self::$debug == true || self::$print_to_screen) { self::log('Parsing debug log'); - echo self::logToScreen(); + self::logToScreen(); + } + + if (self::$log_to_file == true) + { + self::logToFile(); } } @@ -187,6 +208,16 @@ class Logger { self::logError('Exception thrown: ' . $message . ' | ' . $code, null, $file, $line); } + /** + * Set the template that FuzeWorks should use to parse debug logs + * + * @var string Name of the template file + */ + public static function setLoggerTemplate($templateName) + { + self::$logger_template = $templateName; + } + /** * Output the entire log to the screen. Used for debugging problems with your code. * @@ -199,33 +230,21 @@ class Logger { return false; } - // Otherwise just load it - $string = '

FuzeWorks debug log

'; - $layer = 0; - for ($i = 0; $i < count(self::$Logs); ++$i) { - $log = self::$Logs[$i]; - if ($log['type'] == 'LEVEL_START') { - ++$layer; - $color = 255 - ($layer * 25); - $string .= '
'; - $string .= '
' . $log['message'] . '' . (!empty($log['runtime']) ? '(' . round($log['runtime'] * 1000, 4) . 'ms)' : '') . '
'; - } elseif ($log['type'] == 'LEVEL_STOP') { - --$layer; - $string .= '
'; - } elseif ($log['type'] == 'ERROR') { - $string .= '
[' . $log['type'] . ']' . (!empty($log['context']) && is_string($log['context']) ? '[' . $log['context'] . ']' : '') . ' ' . $log['message'] . ' - ' . (!empty($log['logFile']) ? $log['logFile'] : '') . ' : ' . (!empty($log['logLine']) ? $log['logLine'] : '') . '(' . round($log['runtime'] * 1000, 4) . ' ms)
'; - } elseif ($log['type'] == 'WARNING') { - $string .= '
[' . $log['type'] . ']' . (!empty($log['context']) && is_string($log['context']) ? '[' . $log['context'] . ']' : '') . ' ' . $log['message'] . ' - ' . (!empty($log['logFile']) ? $log['logFile'] : '') . ' : ' . (!empty($log['logLine']) ? $log['logLine'] : '') . '(' . round($log['runtime'] * 1000, 4) . ' ms)
'; - } elseif ($log['type'] == 'INFO') { - $string .= '
' . (!empty($log['context']) ? '[' . $log['context'] . ']' : '') . ' ' . $log['message'] . '(' . round($log['runtime'] * 1000, 4) . ' ms)
'; - } elseif ($log['type'] == 'DEBUG') { - $string .= '
[' . $log['type'] . ']' . (!empty($log['context']) ? '[' . $log['context'] . ']' : '') . ' ' . $log['message'] . '(' . round($log['runtime'] * 1000, 4) . ' ms)
'; - } - } + Layout::reset(); + Layout::assign('Logs', self::$Logs); + Layout::view(self::$logger_template, 'Core'.DS.'Views'); + } - return $string; + public static function logToFile() + { + Layout::reset(); + Layout::assign('Logs', self::$Logs); + $contents = Layout::get('logger_cli', 'Core'.DS.'Views'); + $file = 'Application'.DS.'Logs'.DS.'log_latest.php'; + if (is_writable($file)) + { + file_put_contents($file, 'assigned_variables['Logs'] as $log) { + $id++; + + $string = ''; + if ($log['type'] == 'WARNING') + { + $string .= getColoredString('[WARNING]', 'black', 'yellow') . ' - '; + $string .= getColoredString($log['message'], 'black', 'yellow'); + } + elseif ($log['type'] == 'ERROR') + { + $string .= getColoredString('[ERROR]', 'black', 'red') . ' - '; + $string .= getColoredString($log['message'], 'black', 'red'); + } + elseif ($log['type'] == "LEVEL_STOP") + { + continue; + } + else + { + $string .= getColoredString($log['message'], 'green', 'black'); + } + + + printf($mask, + $id, + $string, + (!empty($log['runtime']) ? + round($log['runtime'] * 1000, 4) . 'ms' : + '')); +} \ No newline at end of file diff --git a/Core/Views/view.logger_default.php b/Core/Views/view.logger_default.php new file mode 100644 index 0000000..becf38a --- /dev/null +++ b/Core/Views/view.logger_default.php @@ -0,0 +1,27 @@ +FuzeWorks debug log'; +$layer = 0; +foreach ($this->assigned_variables['Logs'] as $log) { + if ($log['type'] == 'LEVEL_START') { + ++$layer; + $color = 255 - ($layer * 25); + $string .= '
'; + $string .= '
' . $log['message'] . '' . (!empty($log['runtime']) ? '(' . round($log['runtime'] * 1000, 4) . 'ms)' : '') . '
'; + } elseif ($log['type'] == 'LEVEL_STOP') { + --$layer; + $string .= '
'; + } elseif ($log['type'] == 'ERROR') { + $string .= '
[' . $log['type'] . ']' . (!empty($log['context']) && is_string($log['context']) ? '[' . $log['context'] . ']' : '') . ' ' . $log['message'] . ' + ' . (!empty($log['logFile']) ? $log['logFile'] : '') . ' : ' . (!empty($log['logLine']) ? $log['logLine'] : '') . '(' . round($log['runtime'] * 1000, 4) . ' ms)
'; + } elseif ($log['type'] == 'WARNING') { + $string .= '
[' . $log['type'] . ']' . (!empty($log['context']) && is_string($log['context']) ? '[' . $log['context'] . ']' : '') . ' ' . $log['message'] . ' + ' . (!empty($log['logFile']) ? $log['logFile'] : '') . ' : ' . (!empty($log['logLine']) ? $log['logLine'] : '') . '(' . round($log['runtime'] * 1000, 4) . ' ms)
'; + } elseif ($log['type'] == 'INFO') { + $string .= '
' . (!empty($log['context']) ? '[' . $log['context'] . ']' : '') . ' ' . $log['message'] . '(' . round($log['runtime'] * 1000, 4) . ' ms)
'; + } elseif ($log['type'] == 'DEBUG') { + $string .= '
[' . $log['type'] . ']' . (!empty($log['context']) ? '[' . $log['context'] . ']' : '') . ' ' . $log['message'] . '(' . round($log['runtime'] * 1000, 4) . ' ms)
'; + } +} + +echo $string; \ No newline at end of file diff --git a/tests/autoload.php b/tests/autoload.php index 420528f..e9b2430 100644 --- a/tests/autoload.php +++ b/tests/autoload.php @@ -45,6 +45,7 @@ Core::init(); $cfg = Config::get('error'); $cfg->debug = false; $cfg->error_reporting = false; +$cfg->log_to_file = false; $cfg->commit(); restore_error_handler(); @@ -61,4 +62,6 @@ if ( ! class_exists('vfsStream') && file_exists('vendor/autoload.php')) class_alias('org\bovigo\vfs\vfsStream', 'vfsStream'); class_alias('org\bovigo\vfs\vfsStreamDirectory', 'vfsStreamDirectory'); class_alias('org\bovigo\vfs\vfsStreamWrapper', 'vfsStreamWrapper'); -} \ No newline at end of file +} + +Logger::setLoggerTemplate('logger_cli'); \ No newline at end of file