PHP Luminova: Log Levels in the Logging System
Defines standard and custom log levels used to categorize and manage log messages by severity.
The LogLevel class in Luminova defines a set of constants that represent logging severity levels. It follows common logging standards and supports both PSR-style string levels (such as 'error', 'info') and RFC 5424 numeric levels (0–7).
These constants provide a consistent and reliable way to classify log messages based on their importance and urgency.
Use Cases
- Organize log messages by severity.
- Ensure consistent log handling across different systems.
- Filter or extend logs for debugging, error tracking, or performance monitoring.
Key Highlights
- Standard Log Levels: Common levels such as
EMERGENCY,ALERT,CRITICAL,ERROR,WARNING,INFO, andDEBUG. - Custom Levels: Additional levels like
PHP,EXCEPTION, andMETRICSfor framework and application-specific use cases. - RFC 5424 Support: Supports numeric severity levels (
0–7) alongside string-based levels. - Critical Levels: Levels considered critical by
isCritical, includingEMERGENCY,ALERT,CRITICAL, andEXCEPTION.
Examples
Log a message with a specific level
use App\Utils\MyLogger;
(new MyLogger)->log(LogLevel::CRITICAL, 'This is a critical error');Validate a log level
if (LogLevel::has('critical')) {
echo 'Log level is valid.';
} else {
echo 'Log level not recognized.';
}Resolve level
$level = LogLevel::resolve('critical') // critical
$level = LogLevel::resolve(2) // critical
$level = LogLevel::resolve(10) // NULLCheck if a log level is critical
var_dump(LogLevel::isCritical(LogLevel::WARNING)); // false
var_dump(LogLevel::isCritical(LogLevel::CRITICAL)); // true
var_dump(LogLevel::isCritical(LogLevel::ALERT)); // true
var_dump(LogLevel::isCritical(LogLevel::DEBUG)); // falseAssert log level in a custom logger
namespace App\Utils;
use Psr\Log\LoggerInterface;
class MyLogger implements LoggerInterface
{
public function log(string|int $level, string $message): void
{
LogLevel::assert($level, __METHOD__);
echo sprintf("[%s] %s\n", strtoupper((string) $level), $message);
}
}Class Definition
- Full namespace:
Luminova\Logger\LogLevel - This class is marked as final and can't be subclassed
Constants
Log Level Constants
| Constant | Type | Value | Description |
|---|---|---|---|
EMERGENCY | string | emergency | System is unusable. Immediate action required. |
ALERT | string | alert | Critical issue that needs immediate attention. |
CRITICAL | string | critical | Severe condition, such as a service or application failure. |
ERROR | string | error | Runtime errors that need investigation. |
WARNING | string | warning | Potential problems that are not yet critical. |
NOTICE | string | notice | Normal but significant events worth noting. |
INFO | string | info | General information about application behavior. |
DEBUG | string | debug | Detailed diagnostic information for development. |
EXCEPTION | string | exception | Uncaught exceptions or handled error scenarios. |
PHP | string | php_error | Native PHP errors such as warnings and fatal errors. |
METRICS | string | metrics | Performance and monitoring data (typically production use). |
RFC 5424 Syslog Severity Levels
These numeric levels are a standard reference used by syslog and many logging systems.
| Level | Name | Description |
|---|---|---|
| 0 | EMERGENCY | System is unusable |
| 1 | ALERT | Immediate action required |
| 2 | CRITICAL | Critical failure |
| 3 | ERROR | Error condition |
| 4 | WARNING | Warning condition |
| 5 | NOTICE | Normal but significant |
| 6 | INFO | Informational message |
| 7 | DEBUG | Debug-level message |
LEVELS
List of all valid log levels.
public const array<string,string> LEVELS = [
'emergency' => self::EMERGENCY,
'alert' => self::ALERT,
'critical' => self::CRITICAL,
'error' => self::ERROR,
'warning' => self::WARNING,
'notice' => self::NOTICE,
'info' => self::INFO,
'debug' => self::DEBUG,
'exception' => self::EXCEPTION,
'php_error' => self::PHP,
'php' => self::PHP,
'metrics' => self::METRICS,
];Methods
has
Checks if the given log level is valid.
Supports both PSR-style string levels (e.g., error, info) and RFC 5424 numeric levels (0–7).
public static has(string|int $level): boolParameters:
| Parameter | Type | Description |
|---|---|---|
$level | string|int | The log level to validate. |
Return Value:
bool - Returns true if the level exists, false otherwise.
resolve
Resolves the canonical value for a given log level.
Maps a PSR-style string level or RFC 5424 numeric level to its internal representation.
public static resolve(string|int $level): ?stringParameters:
| Parameter | Type | Description |
|---|---|---|
$level | string|int | The log level to parse. |
Return Value:
bool - Return the canonical log level, or null if invalid.
assert
Asserts that the given log level is valid.
This function checks if the provided log level exists in the predefined set of log levels.If the level is invalid, it throws an InvalidArgumentException with a detailed error message.
public static assert(string|int $level, ?string $function = null): voidParameters:
| Parameter | Type | Description |
|---|---|---|
$level | string|int | The log level to validate (e.g., LogLevel::ERROR, LogLevel::INFO, LogLevel::DEBUG). |
$function | string|null | Optional. The name of the calling function for context in the error message. |
Throws:
\Luminova\Exceptions\InvalidArgumentException - If the provided log level is not valid.
isCritical
Checks if a given log level is considered critical.
Critical levels represent severe issues that require immediate attention, such as system failures or security breaches.
The following log levels are considered critical:
emergencyalertcriticalexception
public static isCritical(string|int $level): boolParameters:
| Parameter | Type | Description |
|---|---|---|
$level | string|int | The log level to check (e.g., LogLevel::ERROR, LogLevel::INFO, LogLevel::DEBUG). |
Return Value:
bool - Returns true if the level is critical, otherwise false.