From b08b878e9a02421488653071cfa599d92da9122c Mon Sep 17 00:00:00 2001 From: netkas Date: Mon, 28 Oct 2024 15:27:51 -0400 Subject: [PATCH] Removed Console.php --- src/LogLib/Classes/Console.php | 251 --------------------------------- 1 file changed, 251 deletions(-) delete mode 100644 src/LogLib/Classes/Console.php diff --git a/src/LogLib/Classes/Console.php b/src/LogLib/Classes/Console.php deleted file mode 100644 index d19fd4b..0000000 --- a/src/LogLib/Classes/Console.php +++ /dev/null @@ -1,251 +0,0 @@ -displayAnsi()) - { - return $application_name; - } - - if(!isset(self::$application_colors[$application_name])) - { - $colors = ConsoleColors::BRIGHT_COLORS; - - try - { - $color = $colors[random_int(0, count($colors) - 1)]; - } - catch (Exception $e) - { - throw new RuntimeException(sprintf('Unable to generate random color for application "%s"', $application_name), $e->getCode(), $e); - } - - self::$application_colors[$application_name] = $color; - } - - return self::color($options, $application_name, self::$application_colors[$application_name]); - } - - /** - * Applies a specified color to the given text, using ANSI escape sequences. - * - * @param RuntimeOptions $options The runtime options object. - * @param string $text The text to apply the color to. - * @param ConsoleColors $color The ANSI color code to apply to the text. - * @return string The text with the specified color applied. - */ - private static function color(RuntimeOptions $options, string $text, ConsoleColors $color): string - { - if(!$options->displayAnsi()) - { - return $text; - } - - return "\033[" . $color->value . "m" . $text . "\033[0m"; - } - - /** - * Colorizes the log message based on the event level using ANSI escape sequences. - * - * @param RuntimeOptions $options The runtime options object. - * @param Event $event The log event to colorize. - * @return string The colorized log message. - */ - private static function colorize(RuntimeOptions $options, Event $event): string - { - if(!$options->displayAnsi()) - { - return Utilities::levelToString($event->getLevel()); - } - - $color = match($event->getLevel()) - { - LogLevel::DEBUG => ConsoleColors::LIGHT_PURPLE, - LogLevel::VERBOSE => ConsoleColors::LIGHT_CYAN, - LogLevel::INFO => ConsoleColors::WHITE, - LogLevel::WARNING => ConsoleColors::YELLOW, - LogLevel::FATAL => ConsoleColors::RED, - LogLevel::ERROR => ConsoleColors::LIGHT_RED, - default => null, - }; - - if($color === null) - { - return Utilities::levelToString($event->getLevel()); - } - - return self::color($options, Utilities::levelToString($event->getLevel()), $color); - } - - /** - * Retrieves the current timestamp as a formatted string. - * - * @param RuntimeOptions $options The runtime options object. - * @return string The current timestamp. - */ - private static function getTimestamp(RuntimeOptions $options): string - { - $tick_time = (string)microtime(true); - - if(!is_null(self::$largest_tick_length) && strlen($tick_time) > (int)self::$largest_tick_length) - { - self::$largest_tick_length = strlen($tick_time); - } - - if(strlen($tick_time) < self::$largest_tick_length) - { - $tick_time = str_pad($tick_time, (strlen($tick_time) + (self::$largest_tick_length - strlen($tick_time)))); - } - - $fmt_tick = $tick_time; - if(self::$last_tick_time !== null) - { - $timeDiff = microtime(true) - self::$last_tick_time; - - if ($timeDiff > 1.0) - { - $fmt_tick = self::color($options, $tick_time, ConsoleColors::LIGHT_RED); - } - elseif ($timeDiff > 0.5) - { - $fmt_tick = self::color($options, $tick_time, ConsoleColors::YELLOW); - } - } - - self::$last_tick_time = $tick_time; - return $fmt_tick; - } - - /** - * Outputs a log event to the console. - * - * @param Options $options The options configuration object. - * @param Event $event The log event to output. - * @return void - */ - public static function out(Options $options, Event $event): void - { - if(!Utilities::runningInCli()) - { - return; - } - - if(Validate::checkLevelType(LogLevel::DEBUG, $options->getLoglevel())) - { - $backtrace_output = Utilities::getTraceString($event, $options->displayAnsi()); - - print(sprintf("[%s] [%s] [%s] %s %s" . PHP_EOL, - self::getTimestamp($options), - self::formatAppColor($options->getApplicationName()), - self::colorize($options, $event), - $backtrace_output, $event->getMessage() - )); - - if($event->getException() !== null) - { - self::outException($options, $event->getException()); - } - - return; - } - - if(Validate::checkLevelType(LogLevel::VERBOSE, $options->getLoglevel())) - { - $backtrace_output = Utilities::getTraceString($event, $options->displayAnsi()); - - print(sprintf("[%s] [%s] %s %s" . PHP_EOL, - self::formatAppColor($options->getApplicationName()), - self::colorize($options, $event), - $backtrace_output, $event->getMessage() - )); - - if($event->getException() !== null) - { - self::outException($options, $event->getException()); - } - - return; - } - - print(sprintf("[%s] [%s] %s" . PHP_EOL, - self::formatAppColor($options->getApplicationName()), - self::colorize($options, $event), - $event->getMessage() - )); - } - - /** - * Prints information about the given exception, including the error message, error code, - * and stack trace. - * - * @param RuntimeOptions $options The runtime options object. - * @param Throwable|null $exception The exception to print information about. - * @return void - */ - private static function outException(RuntimeOptions $options, ?Throwable $exception=null): void - { - if($exception === null) - { - return; - } - - $trace_header = self::color($options, $exception->getFile() . ':' . $exception->getLine(), ConsoleColors::PURPLE); - $trace_error = self::color($options, 'error: ', ConsoleColors::RED); - - print($trace_header . ' ' . $trace_error . $exception->getMessage() . PHP_EOL); - print(sprintf('Error code: %s', $exception->getCode()) . PHP_EOL); - $trace = $exception->getTrace(); - - if(count($trace) > 1) - { - print('Stack Trace:' . PHP_EOL); - foreach($trace as $item) - { - print( ' - ' . self::color($options, $item['file'], ConsoleColors::RED) . ':' . $item['line'] . PHP_EOL); - } - } - - if($exception->getPrevious() !== null) - { - print('Previous Exception:' . PHP_EOL); - - self::outException($options, $exception->getPrevious()); - } - } - } \ No newline at end of file