Luminova Framework

PHP Luminova: System Log Levels

Last updated: 2025-01-29 13:05:41

LogLevel class defines various logging levels used for categorizing log messages.

The Luminova LogLevel class defines a set of constants representing various logging levels, following standard practices for logging severity. These constants provide a consistent way to categorize and manage log messages based on their importance and urgency.

  • Standardized Log Levels: Includes levels like EMERGENCY, ALERT, ERROR, and DEBUG for comprehensive logging.
  • Custom Levels: Adds additional levels, such as PHP, EXCEPTION and METRICS, are for other application use cases.

Use Cases

  • Categorize log messages by severity for better organization.
  • Integrate into logging systems to ensure consistent handling of log levels.
  • Extend or filter logs based on specific levels, such as debugging or performance metrics.

Usage Examples

Logging message with a specified level:

$logger->log(LogLevel::CRITICAL, 'This is a critical error');

Validating log level:

if (LogLevel::has('critical')) {
    echo "Log level is valid.";
} else {
    echo "Log level not recognized.";
}

Check if a given log level is considered critical:

var_dump(Logger::isCritical(LogLevel::WARNING));    // false
var_dump(Logger::isCritical(LogLevel::CRITICAL));   // true
var_dump(Logger::isCritical(LogLevel::ALERT));      // true
var_dump(Logger::isCritical(LogLevel::DEBUG));      // false

Log level assertion:

namespace App\Utils;

use \Psr\Log\LoggerInterface;

class MyLogger extends LoggerInterface
{
    public function log(string $level, string $message): void
    {
        LogLevel::assert($level, __METHOD__);
        $this->log(sprintf("[%s] %s\n", strtoupper($level), $message));
    }
}

Class Definition

  • Full namespace: \Luminova\Logger\LogLevel
  • This class is marked as final and can't be subclassed

Constants

ConstantTypeValueDescription
EMERGENCYstringemergencySystem is unusable. Critical issues that require immediate attention.
ALERTstringalertAction must be taken immediately. Used for critical issues requiring urgent resolution.
CRITICALstringcriticalCritical conditions, such as an application or service being down. Requires immediate action.
ERRORstringerrorRuntime errors that require attention. Indicates problems that need investigation.
WARNINGstringwarningWarnings about potential issues that may require attention but are not immediately critical.
NOTICEstringnoticeNormal but significant conditions that require attention. Provides useful information.
INFOstringinfoInformational messages that highlight the application's progress at a coarse-grained level.
DEBUGstringdebugDetailed information used for debugging and diagnosing issues, typically during development.
EXCEPTIONstringexceptionException messages or errors, useful for handling uncaught exceptions or error scenarios.
PHPstringphp_errorPHP errors, including parse errors, runtime errors, and warnings.
METRICSstringmetricsPerformance metrics, specifically for APIs or production-level monitoring.

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 specified log level is valid and exists.

public static has(string $level): bool

Parameters:

ParameterTypeDescription
$levelstringThe log level to check (e.g., LogLevel::ERROR, LogLevel::INFO, LogLevel::DEBUG).

Return Value:

bool - Returns true if the log level exists, false otherwise.


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 $level, ?string $function = null): void

Parameters:

ParameterTypeDescription
$levelstringThe log level to validate (e.g., LogLevel::ERROR, LogLevel::INFO, LogLevel::DEBUG).
$functionstring|nullOptional. 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:

  • emergency
  • alert
  • critical
  • exception
  • error
public static isCritical(string $level): bool

Parameters:

ParameterTypeDescription
$levelstringThe log level to check (e.g., LogLevel::ERROR, LogLevel::INFO, LogLevel::DEBUG).

Return Value:

bool - Returns true if the level is critical, otherwise false.