From 990fe6b1db13fef918b07fb541148e8b5d6e3841 Mon Sep 17 00:00:00 2001 From: Ricardo Cino Date: Sun, 23 Apr 2017 09:25:46 +0200 Subject: [PATCH] Regex update for Queue worker logs (#89) * Update to make the regex to support a qeue log file. * Added docblock --- .../LaravelLogViewer/LaravelLogViewer.php | 48 ++++++++++++------- 1 file changed, 30 insertions(+), 18 deletions(-) diff --git a/src/Rap2hpoutre/LaravelLogViewer/LaravelLogViewer.php b/src/Rap2hpoutre/LaravelLogViewer/LaravelLogViewer.php index 4c67a3c..75e60d3 100644 --- a/src/Rap2hpoutre/LaravelLogViewer/LaravelLogViewer.php +++ b/src/Rap2hpoutre/LaravelLogViewer/LaravelLogViewer.php @@ -25,6 +25,7 @@ class LaravelLogViewer 'critical' => 'danger', 'alert' => 'danger', 'emergency' => 'danger', + 'processed' => 'info', ]; private static $levels_imgs = [ @@ -36,8 +37,26 @@ class LaravelLogViewer 'critical' => 'warning', 'alert' => 'warning', 'emergency' => 'warning', + 'processed' => 'info' ]; + /** + * Log levels that are used + * @var array + */ + private static $log_levels = [ + 'emergency', + 'alert', + 'critical', + 'error', + 'warning', + 'notice', + 'info', + 'debug', + 'processed' + ]; + + const MAX_FILE_SIZE = 52428800; // Why? Uh... Sorry /** @@ -52,6 +71,11 @@ public static function setFile($file) } } + /** + * @param $file + * @return string + * @throws \Exception + */ public static function pathToLogFile($file) { $logsPath = storage_path('logs'); @@ -85,8 +109,6 @@ public static function all() { $log = array(); - $log_levels = self::getLogLevels(); - $pattern = '/\[\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2}\].*/'; if (!self::$file) { @@ -113,18 +135,17 @@ public static function all() foreach ($headings as $h) { for ($i=0, $j = count($h); $i < $j; $i++) { - foreach ($log_levels as $level_key => $level_value) { - if (strpos(strtolower($h[$i]), '.' . $level_value)) { - - preg_match('/^\[(\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2})\].*?(\w+)\.' . $level_key . ': (.*?)( in .*?:[0-9]+)?$/', $h[$i], $current); + foreach (self::$log_levels as $level) { + if (strpos(strtolower($h[$i]), '.' . $level) || strpos(strtolower($h[$i]), $level . ':')) { + preg_match('/^\[(\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2})\](?:.*?(\w+)\.|.*?)' . $level . ': (.*?)( in .*?:[0-9]+)?$/i', $h[$i], $current); if (!isset($current[3])) continue; $log[] = array( 'context' => $current[2], - 'level' => $level_value, - 'level_class' => self::$levels_classes[$level_value], - 'level_img' => self::$levels_imgs[$level_value], + 'level' => $level, + 'level_class' => self::$levels_classes[$level], + 'level_img' => self::$levels_imgs[$level], 'date' => $current[1], 'text' => $current[3], 'in_file' => isset($current[4]) ? $current[4] : null, @@ -154,13 +175,4 @@ public static function getFiles($basename = false) } return array_values($files); } - - /** - * @return array - */ - private static function getLogLevels() - { - $class = new ReflectionClass(new LogLevel); - return $class->getConstants(); - } }