diff --git a/src/LogLib/Classes/Console.php b/src/LogLib/Classes/Console.php index d96b0ca..495ef74 100644 --- a/src/LogLib/Classes/Console.php +++ b/src/LogLib/Classes/Console.php @@ -5,8 +5,8 @@ namespace LogLib\Classes; use Exception; - use LogLib\Abstracts\ConsoleColors; - use LogLib\Abstracts\LevelType; + use LogLib\Enums\ConsoleColors; + use LogLib\Enums\LogLevel; use LogLib\Log; use LogLib\Objects\Event; use LogLib\Objects\Options; @@ -15,21 +15,9 @@ class Console { - /** - * @var array - */ - private static $application_colors = []; - - - /** - * @var float|int|null - */ - private static $last_tick_time; - - /** - * @var int|null - */ - private static $largest_tick_length; + private static array $application_colors = []; + private static float|int|null $last_tick_time; + private static ?int $largest_tick_length; /** * Formats the application name with a color for the console @@ -68,17 +56,17 @@ * Applies a specified color to the given text, using ANSI escape sequences. * * @param string $text The text to apply the color to. - * @param string $color The ANSI color code to apply to the text. + * @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(string $text, string $color): string + private static function color(string $text, ConsoleColors $color): string { if(!Log::getRuntimeOptions()->displayAnsi()) { return $text; } - return "\033[" . $color . "m" . $text . "\033[0m"; + return "\033[" . $color->value . "m" . $text . "\033[0m"; } /** @@ -96,12 +84,12 @@ $color = match($event->getLevel()) { - LevelType::DEBUG => ConsoleColors::LIGHT_PURPLE, - LevelType::VERBOSE => ConsoleColors::LIGHT_CYAN, - LevelType::INFO => ConsoleColors::WHITE, - LevelType::WARNING => ConsoleColors::YELLOW, - LevelType::FATAL => ConsoleColors::RED, - LevelType::ERROR => ConsoleColors::LIGHT_RED, + 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, }; @@ -165,7 +153,7 @@ return; } - if(Validate::checkLevelType(LevelType::DEBUG, Log::getRuntimeOptions()->getLoglevel())) + if(Validate::checkLevelType(LogLevel::DEBUG, Log::getRuntimeOptions()->getLoglevel())) { $backtrace_output = Utilities::getTraceString($event, Log::getRuntimeOptions()->displayAnsi()); @@ -184,7 +172,7 @@ return; } - if(Validate::checkLevelType(LevelType::VERBOSE, Log::getRuntimeOptions()->getLoglevel())) + if(Validate::checkLevelType(LogLevel::VERBOSE, Log::getRuntimeOptions()->getLoglevel())) { $backtrace_output = Utilities::getTraceString($event, Log::getRuntimeOptions()->displayAnsi()); diff --git a/src/LogLib/Classes/Utilities.php b/src/LogLib/Classes/Utilities.php index 4733f8d..3cced33 100644 --- a/src/LogLib/Classes/Utilities.php +++ b/src/LogLib/Classes/Utilities.php @@ -2,8 +2,8 @@ namespace LogLib\Classes; - use LogLib\Abstracts\CallType; - use LogLib\Abstracts\LevelType; + use LogLib\Enums\CallType; + use LogLib\Enums\LogLevel; use LogLib\Objects\Event; use OptsLib\Parse; use Throwable; @@ -28,19 +28,19 @@ /** * Converts a log level to its corresponding string representation. * - * @param int $level The log level to convert. + * @param LogLevel $level The log level to convert. * @return string The string representation of the log level. */ - public static function levelToString(int $level): string + public static function levelToString(LogLevel $level): string { return match ($level) { - LevelType::DEBUG => 'DBG', - LevelType::VERBOSE => 'VRB', - LevelType::INFO => 'INF', - LevelType::WARNING => 'WRN', - LevelType::FATAL => 'CRT', - LevelType::ERROR => 'ERR', + LogLevel::DEBUG => 'DBG', + LogLevel::VERBOSE => 'VRB', + LogLevel::INFO => 'INF', + LogLevel::WARNING => 'WRN', + LogLevel::FATAL => 'CRT', + LogLevel::ERROR => 'ERR', default => 'UNK', }; } @@ -69,7 +69,7 @@ /** * Returns the log level based on the configuration. * - * @return int The log level. This value represents the severity or importance of the log messages. + * @return LogLevel The log level. This value represents the severity or importance of the log messages. * The returned value will be one of the constants defined in the LevelType class: * - DEBUG (6) * - VERBOSE (5) @@ -80,54 +80,54 @@ * - SILENT (0) * If no log level is configured or the configured level is not recognized, the INFO level (4) will be returned by default. */ - public static function getLogLevel(): int + public static function getLogLevel(): LogLevel { $args = Parse::getArguments(); switch(strtolower(($args['log'] ?? $args['log-level'] ?? (getenv('LOG_LEVEL') ?: 'info') ?? 'info'))) { - case LevelType::DEBUG: + case LogLevel::DEBUG: case 'debug': case '6': case 'dbg': - return LevelType::DEBUG; + return LogLevel::DEBUG; - case LevelType::VERBOSE: + case LogLevel::VERBOSE: case 'verbose': case '5': case 'vrb': - return LevelType::VERBOSE; + return LogLevel::VERBOSE; default: - case LevelType::INFO: + case LogLevel::INFO: case 'info': case '4': case 'inf': - return LevelType::INFO; + return LogLevel::INFO; - case LevelType::WARNING: + case LogLevel::WARNING: case 'warning': case '3': case 'wrn': - return LevelType::WARNING; + return LogLevel::WARNING; - case LevelType::ERROR: + case LogLevel::ERROR: case 'error': case '2': case 'err': - return LevelType::ERROR; + return LogLevel::ERROR; - case LevelType::FATAL: + case LogLevel::FATAL: case 'fatal': case '1': case 'crt': - return LevelType::FATAL; + return LogLevel::FATAL; - case LevelType::SILENT: + case LogLevel::SILENT: case 'silent': case '0': case 'sil': - return LevelType::SILENT; + return LogLevel::SILENT; } } @@ -164,7 +164,7 @@ { if($event->getBacktrace() === null || count($event->getBacktrace()) === 0) { - return CallType::LAMBDA_CALL; + return CallType::LAMBDA_CALL->value; } $backtrace = $event->getBacktrace()[count($event->getBacktrace()) - 1]; @@ -184,20 +184,20 @@ { if(isset($backtrace['file'])) { - return ($ansi ? "\033[1;37m" : '') . basename($backtrace['file']) . ($ansi ? "\033[0m" : '') . CallType::STATIC_CALL . CallType::LAMBDA_CALL; + return ($ansi ? "\033[1;37m" : '') . basename($backtrace['file']) . ($ansi ? "\033[0m" : '') . CallType::STATIC_CALL->value . CallType::LAMBDA_CALL->value; } - return basename($backtrace['file']) . CallType::STATIC_CALL . CallType::LAMBDA_CALL; + return basename($backtrace['file']) . CallType::STATIC_CALL->value . CallType::LAMBDA_CALL->value; } if($backtrace['function'] === 'eval') { if(isset($backtrace['file'])) { - return ($ansi ? "\033[1;37m" : '') . basename($backtrace['file']) . ($ansi ? "\033[0m" : '') . CallType::STATIC_CALL . CallType::EVAL_CALL; + return ($ansi ? "\033[1;37m" : '') . basename($backtrace['file']) . ($ansi ? "\033[0m" : '') . CallType::STATIC_CALL->value . CallType::EVAL_CALL->value; } - return basename($backtrace['file']) . CallType::STATIC_CALL . CallType::EVAL_CALL; + return basename($backtrace['file']) . CallType::STATIC_CALL->value . CallType::EVAL_CALL->value; } if($ansi) @@ -218,11 +218,11 @@ if($class === null) { - return $function . CallType::FUNCTION_CALL; + return $function . CallType::FUNCTION_CALL->value; } $type = ($backtrace['type'] === CallType::METHOD_CALL ? CallType::METHOD_CALL : CallType::STATIC_CALL); - return "{$class}{$type}{$function}" . CallType::FUNCTION_CALL; + return "{$class}{$type->value}{$function}" . CallType::FUNCTION_CALL->value; } diff --git a/src/LogLib/Classes/Validate.php b/src/LogLib/Classes/Validate.php index d3141d5..2d2e8a4 100644 --- a/src/LogLib/Classes/Validate.php +++ b/src/LogLib/Classes/Validate.php @@ -2,98 +2,78 @@ namespace LogLib\Classes; - use LogLib\Abstracts\LevelType; + use LogLib\Enums\LogLevel; class Validate { - /** - * Checks if the given level is a valid level type. - * - * @param int $level The level to check. - * @return bool Returns true if the level is valid - */ - public static function LevelType(int $level): bool - { - return in_array($level, LevelType::ALL); - } - /** * Checks if the given input level is valid for the current level. * - * @param int $input The input level to check. - * @param int $current_level The current level to compare against. + * @param LogLevel $input The input level to check. + * @param LogLevel $current_level The current level to compare against. * @return bool Returns true if the input level is valid for the current level, false otherwise. */ - public static function checkLevelType(int $input, int $current_level): bool + public static function checkLevelType(LogLevel $input, LogLevel $current_level): bool { - if(!self::LevelType($input)) - { - return false; - } - - if(!self::LevelType($current_level)) - { - return false; - } - switch($current_level) { - case LevelType::DEBUG: + case LogLevel::DEBUG: $levels = [ - LevelType::DEBUG, - LevelType::VERBOSE, - LevelType::INFO, - LevelType::WARNING, - LevelType::FATAL, - LevelType::ERROR + LogLevel::DEBUG, + LogLevel::VERBOSE, + LogLevel::INFO, + LogLevel::WARNING, + LogLevel::FATAL, + LogLevel::ERROR ]; return in_array($input, $levels, true); - case LevelType::VERBOSE: + case LogLevel::VERBOSE: $levels = [ - LevelType::VERBOSE, - LevelType::INFO, - LevelType::WARNING, - LevelType::FATAL, - LevelType::ERROR + LogLevel::VERBOSE, + LogLevel::INFO, + LogLevel::WARNING, + LogLevel::FATAL, + LogLevel::ERROR ]; return in_array($input, $levels, true); - case LevelType::INFO: + case LogLevel::INFO: $levels = [ - LevelType::INFO, - LevelType::WARNING, - LevelType::FATAL, - LevelType::ERROR + LogLevel::INFO, + LogLevel::WARNING, + LogLevel::FATAL, + LogLevel::ERROR ]; return in_array($input, $levels, true); - case LevelType::WARNING: + case LogLevel::WARNING: $levels = [ - LevelType::WARNING, - LevelType::FATAL, - LevelType::ERROR + LogLevel::WARNING, + LogLevel::FATAL, + LogLevel::ERROR ]; return in_array($input, $levels, true); - case LevelType::ERROR: + case LogLevel::ERROR: $levels = [ - LevelType::FATAL, - LevelType::ERROR + LogLevel::FATAL, + LogLevel::ERROR ]; return in_array($input, $levels, true); - case LevelType::FATAL: - return $input === LevelType::FATAL; + case LogLevel::FATAL: + return $input == LogLevel::FATAL; - default: - case LevelType::SILENT: + case LogLevel::SILENT: return false; } + + return false; } } \ No newline at end of file diff --git a/src/LogLib/Abstracts/CallType.php b/src/LogLib/Enums/CallType.php similarity index 68% rename from src/LogLib/Abstracts/CallType.php rename to src/LogLib/Enums/CallType.php index d095a92..6830dd0 100644 --- a/src/LogLib/Abstracts/CallType.php +++ b/src/LogLib/Enums/CallType.php @@ -1,41 +1,41 @@ '; + case METHOD_CALL = '->'; /** * Represents a static method call. * * @var string STATIC_CALL */ - public const STATIC_CALL = '::'; + case STATIC_CALL = '::'; /** * Represents a function call. * * @var string FUNCTION_CALL */ - public const FUNCTION_CALL = '()'; + case FUNCTION_CALL = '()'; /** * Represents a lambda function call. * * @var string LAMBDA_CALL */ - public const LAMBDA_CALL = 'λ'; + case LAMBDA_CALL = 'λ'; /** * Represents an eval() call. * * @var string EVAL_CALL */ - public const EVAL_CALL = 'eval()'; + case EVAL_CALL = 'eval()'; } \ No newline at end of file diff --git a/src/LogLib/Abstracts/ConsoleColors.php b/src/LogLib/Enums/ConsoleColors.php similarity index 55% rename from src/LogLib/Abstracts/ConsoleColors.php rename to src/LogLib/Enums/ConsoleColors.php index fe0f4cb..ddf2e29 100644 --- a/src/LogLib/Abstracts/ConsoleColors.php +++ b/src/LogLib/Enums/ConsoleColors.php @@ -1,42 +1,42 @@ getLoglevel())) { return; @@ -155,7 +149,7 @@ */ public static function info(string $application_name, string $message): void { - self::log($application_name, LevelType::INFO, $message); + self::log($application_name, LogLevel::INFO, $message); } /** @@ -165,7 +159,7 @@ */ public static function verbose(string $application_name, string $message): void { - self::log($application_name, LevelType::VERBOSE, $message); + self::log($application_name, LogLevel::VERBOSE, $message); } /** @@ -175,7 +169,7 @@ */ public static function debug(string $application_name, string $message): void { - self::log($application_name, LevelType::DEBUG, $message); + self::log($application_name, LogLevel::DEBUG, $message); } /** @@ -188,7 +182,7 @@ */ public static function warning(string $application_name, string $message, ?Throwable $throwable=null): void { - self::log($application_name, LevelType::WARNING, $message, $throwable); + self::log($application_name, LogLevel::WARNING, $message, $throwable); } /** @@ -201,7 +195,7 @@ **/ public static function error(string $application_name, string $message, ?Throwable $throwable=null): void { - self::log($application_name, LevelType::ERROR, $message, $throwable); + self::log($application_name, LogLevel::ERROR, $message, $throwable); } /** @@ -214,7 +208,7 @@ */ public static function fatal(string $application_name, string $message, ?Throwable $throwable=null): void { - self::log($application_name, LevelType::FATAL, $message, $throwable); + self::log($application_name, LogLevel::FATAL, $message, $throwable); } /** diff --git a/src/LogLib/Objects/Event.php b/src/LogLib/Objects/Event.php index 406c890..0ba408c 100644 --- a/src/LogLib/Objects/Event.php +++ b/src/LogLib/Objects/Event.php @@ -4,15 +4,14 @@ namespace LogLib\Objects; - use LogLib\Abstracts\LevelType; use LogLib\Classes\Utilities; + use LogLib\Enums\LogLevel; use Throwable; class Event { /** - * @see LevelType - * @var int + * @var LogLevel */ private $level; @@ -35,11 +34,11 @@ * Event constructor. * * @param string $message - * @param int $level + * @param LogLevel $level * @param Throwable|null $exception * @param array|null $backtrace */ - public function __construct(string $message, int $level, ?Throwable $exception=null, ?array $backtrace=null) + public function __construct(string $message, LogLevel $level, ?Throwable $exception=null, ?array $backtrace=null) { $this->message = $message; $this->level = $level; @@ -50,10 +49,10 @@ /** * Returns the level of the event * - * @see LevelType - * @return int + * @return LogLevel + * @see LogLevel */ - public function getLevel(): int + public function getLevel(): LogLevel { return $this->level; } @@ -108,5 +107,4 @@ { $this->backtrace = $backtrace; } - } \ No newline at end of file diff --git a/src/LogLib/Objects/RuntimeOptions.php b/src/LogLib/Objects/RuntimeOptions.php index c1051b8..a3c54fc 100644 --- a/src/LogLib/Objects/RuntimeOptions.php +++ b/src/LogLib/Objects/RuntimeOptions.php @@ -4,115 +4,155 @@ namespace LogLib\Objects; - use LogLib\Abstracts\LevelType; use LogLib\Classes\Utilities; + use LogLib\Enums\LogLevel; class RuntimeOptions { - /** - * Indicates if the console output is enabled - * - * @var bool - * @property_name console_output - */ - private $console_output; + private $consoleOutput; + private bool $displayAnsi; + private bool $handleExceptions; + private LogLevel $logLevel; + private bool $fileLoggingEnabled; + private LogLevel $fileLoggingLevel; /** - * Indicates if ANSI colors should be used in the console output * - * @var bool - * @property_name display_ansi - */ - private $display_ansi; - - /** - * Indicates if LogLib should handle uncaught exceptions - * - * @var bool - * @property_name handle_exceptions - */ - private $handle_exceptions; - - /** - * The current log level - * - * @var int - * @see LevelType - */ - private $log_level; - - /** - * Public Constructor + * @return void */ public function __construct() { - $this->console_output = Utilities::runningInCli(); - $this->display_ansi = Utilities::getDisplayAnsi(); - $this->log_level = Utilities::getLogLevel(); - $this->handle_exceptions = true; + $this->consoleOutput = Utilities::runningInCli(); + $this->displayAnsi = Utilities::getDisplayAnsi(); + $this->logLevel = Utilities::getLogLevel(); + $this->fileLoggingEnabled = true; + $this->fileLoggingLevel = LogLevel::ERROR; + $this->handleExceptions = true; } /** - * @return bool + * Checks if console output is enabled. + * + * @return bool Returns true if console output is enabled, false otherwise. */ public function isConsoleOutput(): bool { - return $this->console_output; + return $this->consoleOutput; } /** - * @param bool $console_output + * Set the console output flag. + * + * @param bool $consoleOutput Indicates whether to enable or disable console output. + * @return void */ - public function setConsoleOutput(bool $console_output): void + public function setConsoleOutput(bool $consoleOutput): void { - $this->console_output = $console_output; + $this->consoleOutput = $consoleOutput; } /** - * @return bool + * Determines if ANSI display is enabled. + * + * @return bool Returns true if ANSI display is enabled, false otherwise. */ public function displayAnsi(): bool { - return $this->display_ansi; + return $this->displayAnsi; } /** - * @param bool $display_ansi + * Sets whether to display ANSI colors in the console output. + * + * @param bool $displayAnsi A boolean value indicating whether ANSI colors should be displayed. + * @return void */ - public function setDisplayAnsi(bool $display_ansi): void + public function setDisplayAnsi(bool $displayAnsi): void { - $this->display_ansi = $display_ansi; + $this->displayAnsi = $displayAnsi; } /** - * @return bool + * Get the flag indicating whether exceptions are being handled. + * + * @return bool True if exceptions are being handled */ public function handleExceptions(): bool { - return $this->handle_exceptions; + return $this->handleExceptions; } /** - * @param bool $handle_exceptions + * Set the exception handling behavior. + * + * @param bool $handleExceptions A boolean value indicating whether to handle exceptions. + * @return void */ - public function setHandleExceptions(bool $handle_exceptions): void + public function setHandleExceptions(bool $handleExceptions): void { - $this->handle_exceptions = $handle_exceptions; + $this->handleExceptions = $handleExceptions; } /** - * @return int + * Returns the current log level. + * + * @return LogLevel The current log level. */ - public function getLoglevel(): int + public function getLoglevel(): LogLevel { - return $this->log_level; + return $this->logLevel; } /** - * @param int $log_level + * Sets the log level for logging operations. + * + * @param LogLevel $logLevel The log level to be set. + * @return void */ - public function setLoglevel(int $log_level): void + public function setLoglevel(LogLevel $logLevel): void { - $this->log_level = $log_level; + $this->logLevel = $logLevel; + } + + /** + * Checks if file logging is enabled. + * + * @return bool True if file logging is enabled, false otherwise. + */ + public function isFileLoggingEnabled(): bool + { + return $this->fileLoggingEnabled; + } + + /** + * Enables or disables file logging. + * + * @param bool $fileLoggingEnabled Indicates whether file logging should be enabled. + * @return void + */ + public function setFileLoggingEnabled(bool $fileLoggingEnabled): void + { + $this->fileLoggingEnabled = $fileLoggingEnabled; + } + + /** + * Gets the current file logging level. + * + * @return LogLevel The file logging level. + */ + public function getFileLoggingLevel(): LogLevel + { + return $this->fileLoggingLevel; + } + + /** + * Set the logging level for file output. + * + * @param LogLevel $fileLoggingLevel The logging level to be used for file output. + * @return void + */ + public function setFileLoggingLevel(LogLevel $fileLoggingLevel): void + { + $this->fileLoggingLevel = $fileLoggingLevel; } } \ No newline at end of file diff --git a/tests/exception_handling.php b/tests/exception_handling.php index 2153b25..2241608 100644 --- a/tests/exception_handling.php +++ b/tests/exception_handling.php @@ -1,10 +1,9 @@