Rename classes to enums and update usage

This commit is contained in:
netkas 2024-10-26 00:18:08 -04:00
parent 2dd8f912b2
commit fa27ed6405
10 changed files with 234 additions and 235 deletions

View file

@ -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());

View file

@ -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;
}

View file

@ -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;
}
}

View file

@ -1,41 +1,41 @@
<?php
namespace LogLib\Abstracts;
namespace LogLib\Enums;
final class CallType
enum CallType : string
{
/**
* Represents a method call.
*
* @var string METHOD_CALL
*/
public const METHOD_CALL = '->';
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()';
}

View file

@ -1,42 +1,42 @@
<?php
namespace LogLib\Abstracts;
namespace LogLib\Enums;
final class ConsoleColors
enum ConsoleColors : string
{
public const BLACK = "0;30";
case BLACK = "0;30";
public const DARK_GRAY = "1;30";
case DARK_GRAY = "1;30";
public const BLUE = "0;34";
case BLUE = "0;34";
public const LIGHT_BLUE = "1;34";
case LIGHT_BLUE = "1;34";
public const GREEN = "0;32";
case GREEN = "0;32";
public const LIGHT_GREEN = "1;32";
case LIGHT_GREEN = "1;32";
public const CYAN = "0;36";
case CYAN = "0;36";
public const LIGHT_CYAN = "1;36";
case LIGHT_CYAN = "1;36";
public const RED = "0;31";
case RED = "0;31";
public const LIGHT_RED = "1;31";
case LIGHT_RED = "1;31";
public const PURPLE = "0;35";
case PURPLE = "0;35";
public const LIGHT_PURPLE = "1;35";
case LIGHT_PURPLE = "1;35";
public const BROWN = "0;33";
case BROWN = "0;33";
public const YELLOW = "1;33";
case YELLOW = "1;33";
public const LIGHT_GRAY = "0;37";
case LIGHT_GRAY = "0;37";
public const WHITE = "1;37";
case WHITE = "1;37";
public const RESET = "0";
case RESET = "0";
/**
* Represents an array of all possible supported color values.

View file

@ -1,43 +1,43 @@
<?php
namespace LogLib\Abstracts;
namespace LogLib\Enums;
final class LevelType
enum LogLevel : int
{
/**
* Silent type.
*/
public const SILENT = 0;
case SILENT = 0;
/**
* Fatal type.
*/
public const FATAL = 1;
case FATAL = 1;
/**
* Error type.
*/
public const ERROR = 2;
case ERROR = 2;
/**
*
*/
public const WARNING = 3;
case WARNING = 3;
/**
* Information type.
*/
public const INFO = 4;
case INFO = 4;
/**
* Verbose type.
*/
public const VERBOSE = 5;
case VERBOSE = 5;
/**
* Debug type.
*/
public const DEBUG = 6;
case DEBUG = 6;
/**
* All types.

View file

@ -5,10 +5,10 @@
namespace LogLib;
use InvalidArgumentException;
use LogLib\Abstracts\LevelType;
use LogLib\Classes\Console;
use LogLib\Classes\Utilities;
use LogLib\Classes\Validate;
use LogLib\Enums\LogLevel;
use LogLib\Objects\Event;
use LogLib\Objects\Options;
use LogLib\Objects\RuntimeOptions;
@ -110,21 +110,15 @@
* Logs a message with a specified application name, level, optional message, and optional throwable.
*
* @param string $application_name The name of the application
* @param int $level The level type of the log (default is LevelType::INFO)
* @param LogLevel $level The level type of the log (default is LevelType::INFO)
* @param string|null $message The message of the log event
* @param Throwable|null $throwable The exception that was thrown, if any
* @return void
* @throws InvalidArgumentException If the provided level type is invalid or a message is null
*/
private static function log(string $application_name, int $level=LevelType::INFO, ?string $message=null, ?Throwable $throwable=null): void
private static function log(string $application_name, LogLevel $level=LogLevel::INFO, ?string $message=null, ?Throwable $throwable=null): void
{
$application = self::getOptions($application_name);
if(!Validate::levelType($level))
{
throw new InvalidArgumentException(sprintf('Invalid level type: %s', $level));
}
if(!Validate::checkLevelType($level, self::getRuntimeOptions()->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);
}
/**

View file

@ -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;
}
}

View file

@ -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;
}
}

View file

@ -1,10 +1,9 @@
<?php
use LogLib\Abstracts\LevelType;
use LogLib\Log;
use LogLib\Objects\Options;
use LogLib\Log;
use LogLib\Objects\Options;
require('ncc');
require('ncc');
import('net.nosial.loglib', 'latest');
$options = new Options('net.nosial.optslib');