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
This commit is contained in:
Abel Hoogeveen 2016-06-02 15:19:31 +02:00
parent c0bba7915f
commit 53f597f947
5 changed files with 172 additions and 32 deletions

View File

@ -1,6 +1,6 @@
<?php
return array(
<?php return array (
'debug' => false,
'error_reporting' => true,
);
'log_to_file' => false,
'logger_template' => 'logger_default',
) ;

View File

@ -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 = '<h3>FuzeWorks debug log</h3>';
$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 .= '<div style="background: rgb(188 , 232 ,' . $color . ');border: 1px black solid;margin: 5px 0;padding: 5px 20px;">';
$string .= '<div style="font-weight: bold; font-size: 11pt;">' . $log['message'] . '<span style="float: right">' . (!empty($log['runtime']) ? '(' . round($log['runtime'] * 1000, 4) . 'ms)' : '') . '</span></div>';
} elseif ($log['type'] == 'LEVEL_STOP') {
--$layer;
$string .= '</div>';
} elseif ($log['type'] == 'ERROR') {
$string .= '<div style="' . ($layer == 0 ? 'padding-left: 21px;' : '') . 'font-size: 11pt; background-color:#f56954;">[' . $log['type'] . ']' . (!empty($log['context']) && is_string($log['context']) ? '<u>[' . $log['context'] . ']</u>' : '') . ' ' . $log['message'] . '
<span style="float: right">' . (!empty($log['logFile']) ? $log['logFile'] : '') . ' : ' . (!empty($log['logLine']) ? $log['logLine'] : '') . '(' . round($log['runtime'] * 1000, 4) . ' ms)</span></div>';
} elseif ($log['type'] == 'WARNING') {
$string .= '<div style="' . ($layer == 0 ? 'padding-left: 21px;' : '') . 'font-size: 11pt; background-color:#f39c12;">[' . $log['type'] . ']' . (!empty($log['context']) && is_string($log['context']) ? '<u>[' . $log['context'] . ']</u>' : '') . ' ' . $log['message'] . '
<span style="float: right">' . (!empty($log['logFile']) ? $log['logFile'] : '') . ' : ' . (!empty($log['logLine']) ? $log['logLine'] : '') . '(' . round($log['runtime'] * 1000, 4) . ' ms)</span></div>';
} elseif ($log['type'] == 'INFO') {
$string .= '<div style="' . ($layer == 0 ? 'padding-left: 21px;' : '') . 'font-size: 11pt;">' . (!empty($log['context']) ? '<u>[' . $log['context'] . ']</u>' : '') . ' ' . $log['message'] . '<span style="float: right">(' . round($log['runtime'] * 1000, 4) . ' ms)</span></div>';
} elseif ($log['type'] == 'DEBUG') {
$string .= '<div style="' . ($layer == 0 ? 'padding-left: 21px;' : '') . 'font-size: 11pt; background-color:#CCCCCC;">[' . $log['type'] . ']' . (!empty($log['context']) ? '<u>[' . $log['context'] . ']</u>' : '') . ' ' . $log['message'] . '<span style="float: right">(' . round($log['runtime'] * 1000, 4) . ' ms)</span></div>';
}
}
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, '<?php ' . $contents);
}
}
/**

View File

@ -0,0 +1,91 @@
<?php
if (!function_exists('getColoredString'))
{
function getColoredString($string, $foreground_color, $background_color) {
// Determine the color system
$foreground_colors = array();
$background_colors = array();
$foreground_colors['black'] = '0;30';
$foreground_colors['dark_gray'] = '1;30';
$foreground_colors['blue'] = '0;34';
$foreground_colors['light_blue'] = '1;34';
$foreground_colors['green'] = '0;32';
$foreground_colors['light_green'] = '1;32';
$foreground_colors['cyan'] = '0;36';
$foreground_colors['light_cyan'] = '1;36';
$foreground_colors['red'] = '0;31';
$foreground_colors['light_red'] = '1;31';
$foreground_colors['purple'] = '0;35';
$foreground_colors['light_purple'] = '1;35';
$foreground_colors['brown'] = '0;33';
$foreground_colors['yellow'] = '1;33';
$foreground_colors['light_gray'] = '0;37';
$foreground_colors['white'] = '1;37';
$background_colors['black'] = '40';
$background_colors['red'] = '41';
$background_colors['green'] = '42';
$background_colors['yellow'] = '43';
$background_colors['blue'] = '44';
$background_colors['magenta'] = '45';
$background_colors['cyan'] = '46';
$background_colors['light_gray'] = '47';
$colored_string = "";
// Check if given foreground color found
if (isset($foreground_colors[$foreground_color])) {
$colored_string .= "\033[" . $foreground_colors[$foreground_color] . "m";
}
// Check if given background color found
if (isset($background_colors[$background_color])) {
$colored_string .= "\033[" . $background_colors[$background_color] . "m";
}
// Add string and end coloring
$colored_string .= $string . "\033[0m";
return $colored_string;
}
}
$mask = "|%5s |%-90s | %10s |\n";
$id = 1;
printf($mask, 'Id', 'Title', 'Runtime');
printf($mask, $id, getColoredString('FuzeWorks debug log', 'black', 'light_gray'), '0 ms');
foreach ($this->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' :
''));
}

View File

@ -0,0 +1,27 @@
<?php
$string = '<h3>FuzeWorks debug log</h3>';
$layer = 0;
foreach ($this->assigned_variables['Logs'] as $log) {
if ($log['type'] == 'LEVEL_START') {
++$layer;
$color = 255 - ($layer * 25);
$string .= '<div style="background: rgb(188 , 232 ,' . $color . ');border: 1px black solid;margin: 5px 0;padding: 5px 20px;">';
$string .= '<div style="font-weight: bold; font-size: 11pt;">' . $log['message'] . '<span style="float: right">' . (!empty($log['runtime']) ? '(' . round($log['runtime'] * 1000, 4) . 'ms)' : '') . '</span></div>';
} elseif ($log['type'] == 'LEVEL_STOP') {
--$layer;
$string .= '</div>';
} elseif ($log['type'] == 'ERROR') {
$string .= '<div style="' . ($layer == 0 ? 'padding-left: 21px;' : '') . 'font-size: 11pt; background-color:#f56954;">[' . $log['type'] . ']' . (!empty($log['context']) && is_string($log['context']) ? '<u>[' . $log['context'] . ']</u>' : '') . ' ' . $log['message'] . '
<span style="float: right">' . (!empty($log['logFile']) ? $log['logFile'] : '') . ' : ' . (!empty($log['logLine']) ? $log['logLine'] : '') . '(' . round($log['runtime'] * 1000, 4) . ' ms)</span></div>';
} elseif ($log['type'] == 'WARNING') {
$string .= '<div style="' . ($layer == 0 ? 'padding-left: 21px;' : '') . 'font-size: 11pt; background-color:#f39c12;">[' . $log['type'] . ']' . (!empty($log['context']) && is_string($log['context']) ? '<u>[' . $log['context'] . ']</u>' : '') . ' ' . $log['message'] . '
<span style="float: right">' . (!empty($log['logFile']) ? $log['logFile'] : '') . ' : ' . (!empty($log['logLine']) ? $log['logLine'] : '') . '(' . round($log['runtime'] * 1000, 4) . ' ms)</span></div>';
} elseif ($log['type'] == 'INFO') {
$string .= '<div style="' . ($layer == 0 ? 'padding-left: 21px;' : '') . 'font-size: 11pt;">' . (!empty($log['context']) ? '<u>[' . $log['context'] . ']</u>' : '') . ' ' . $log['message'] . '<span style="float: right">(' . round($log['runtime'] * 1000, 4) . ' ms)</span></div>';
} elseif ($log['type'] == 'DEBUG') {
$string .= '<div style="' . ($layer == 0 ? 'padding-left: 21px;' : '') . 'font-size: 11pt; background-color:#CCCCCC;">[' . $log['type'] . ']' . (!empty($log['context']) ? '<u>[' . $log['context'] . ']</u>' : '') . ' ' . $log['message'] . '<span style="float: right">(' . round($log['runtime'] * 1000, 4) . ' ms)</span></div>';
}
}
echo $string;

View File

@ -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');
}
}
Logger::setLoggerTemplate('logger_cli');