Skip to content

Commit

Permalink
Added support for log levels
Browse files Browse the repository at this point in the history
  • Loading branch information
ricardoboss committed Jul 22, 2021
1 parent 617fc8e commit 55c7a03
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 0 deletions.
31 changes: 31 additions & 0 deletions src/Console.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

use BadMethodCallException;
use DateTime;
use RangeException;
use RuntimeException;
use Throwable;
use function count;
Expand Down Expand Up @@ -67,6 +68,7 @@ class Console
private static bool $timestamps = true;
private static bool $colors = true;
private static string $timestamp_format = "d.m.y H:i:s.v";
private static int $log_level = 0;

private static string $colorFormat = "\033[%s;1m"; // 16-bit colors
// private static $extended = "\033[%s;5;%sm"; // 256-bit colors; 38 = foreground, 48 = background
Expand Down Expand Up @@ -170,43 +172,72 @@ public static function timestampFormat(string $format = "d.m.y H:i:s.v"): void
self::$timestamp_format = $format;
}

public static function logLevel(int $level = 0): void
{
if ($level < 0 || $level > 7)
throw new RangeException("Log level can only be set between 0 and 7 (inclusive).");

self::$log_level = $level;
}

public static function link(string $link): string
{
return Console::cyan(Console::underscore($link));
}

public static function debug(string $message, ...$args): void
{
if (self::$log_level > 0)
return;

self::writeln("[DEBUG ] " . vsprintf($message, $args), 'gray');
}

public static function info(string $message, ...$args): void
{
if (self::$log_level > 1)
return;

self::writeln("[INFO ] " . vsprintf($message, $args));
}

public static function notice(string $message, ...$args): void
{
if (self::$log_level > 2)
return;

self::writeln("[NOTICE] " . vsprintf($message, $args), 'blue');
}

public static function warn(string $message, ...$args): void
{
if (self::$log_level > 3)
return;

self::writeln("[WARN ] " . vsprintf($message, $args), 'yellow');
}

public static function error(string $message, ...$args): void
{
if (self::$log_level > 4)
return;

self::writeln("[ERROR ] " . vsprintf($message, $args), 'red', null, [], true);
}

public static function critical(string $message, ...$args): void
{
if (self::$log_level > 5)
return;

self::writeln("[CRITIC] " . vsprintf($message, $args), 'magenta', null, ['bold'], true);
}

public static function alert(string $message, ...$args): void
{
if (self::$log_level > 6)
return;

self::writeln("[ALERT ] " . vsprintf($message, $args), null, 'yellow', ['bold'], true);
}

Expand Down
1 change: 1 addition & 0 deletions test.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

Console::open();

Console::logLevel();
Console::debug("This is a debug message.");
Console::info("This is an info message.");
Console::notice("This is a notice message.");
Expand Down

0 comments on commit 55c7a03

Please sign in to comment.