Luminova Framework

PHP Luminova: PSR Aware Logger Interface

Last updated: 2024-11-18 19:18:57

The Luminova LoggerAware is an extension of PSR LoggerAwareInterface is part of the PSR-3 logging standard, designed to provide a way for classes to accept a PSR-compliant logger instance.

The Luminova LoggerAware class extends the PSR LoggerAwareInterface from the PSR-3 logging standard. It provides a mechanism for classes to accept and utilize a PSR-compliant logger instance, enabling seamless logging.

This design allows easy swapping of logging implementations and ensures compatibility with any PSR-compliant logger, enhancing flexibility and standardization.

The primary method in LoggerAware is setLogger, which injects a logger instance into the implementing class.


Usage Examples

Using a Class Implementing \Psr\Log\LoggerInterface

use Luminova\Logger\LoggerAware;

$logger = new LoggerAware();
$logger->setLogger(new MyPsrLoggerClass());
$logger->alert('This is a alert message');
$logger->getLogger()->setLevel('debug')->mail('[email protected]', 'This is a debug message');

Primary Benefits:

  • Flexibility: Easily switch between different logging implementations.
  • Standards Compliance: Fully compatible with any PSR-compliant logger.

Class Definition

Properties

logger

The PSR logger interface class instance.

protected \Psr\Log\LoggerInterface $logger = null;

Methods

constructor

Initialize Aware Logger Interface.

public __construct(?\Psr\Log\LoggerInterface $logger = null): mixed

Parameters:

ParameterTypeDescription
$logger\Psr\Log\LoggerInterfaceThe logger instance.

setLogger

Set a logger instance on the object.

public setLogger(\Psr\Log\LoggerInterface $logger): void

Parameters:

ParameterTypeDescription
$logger\Psr\Log\LoggerInterfaceThe logger instance.

getLogger

Get the instance of PSR logger class.

public getLogger(): ?LoggerInterface

Return Value:

\Psr\Log\LoggerInterface - Return instance of logger class in-use.


emergency

Log an emergency message.

public emergency(string $message, array $context = []): void

Parameters:

ParameterTypeDescription
$messagestringThe emergency message to log.
$contextarrayAdditional context data (optional).

alert

Log an alert message.

public alert(string $message, array $context = []): void

Parameters:

ParameterTypeDescription
$messagestringThe alert message to log.
$contextarrayAdditional context data (optional).

critical

Log a critical message.

public critical(string $message, array $context = []): void

Parameters:

ParameterTypeDescription
$messagestringThe critical message to log.
$contextarrayAdditional context data (optional).

error

Log an error message.

public error(string $message, array $context = []): void

Parameters:

ParameterTypeDescription
$messagestringThe error message to log.
$contextarrayAdditional context data (optional).

warning

Log a warning message.

public warning(string $message, array $context = []): void

Parameters:

ParameterTypeDescription
$messagestringThe warning message to log.
$contextarrayAdditional context data (optional).

notice

Log a notice message.

public notice(string $message, array $context = []): void

Parameters:

ParameterTypeDescription
$messagestringThe notice message to log.
$contextarrayAdditional context data (optional).

info

Log an info message.

public info(string $message, array $context = []): void

Parameters:

ParameterTypeDescription
$messagestringThe info message to log.
$contextarrayAdditional context data (optional).

debug

Log a debug message.

public debug(string $message, array $context = []): void

Parameters:

ParameterTypeDescription
$messagestringThe debug message to log.
$contextarrayAdditional context data (optional).

exception

Log an exception message.

public exception(string $message, array $context = []): void

Parameters:

ParameterTypeDescription
$messagestringThe EXCEPTION message to log.
$contextarrayAdditional context data (optional).

php

Log an php message.

public php(string $message, array $context = []): void

Parameters:

ParameterTypeDescription
$messagestringThe php message to log.
$contextarrayAdditional context data (optional).

metrics

Log an performance metric.

public metrics(string $message, array $context = []): void

Parameters:

ParameterTypeDescription
$messagestringThe php message to log.
$contextarrayAdditional context data (optional).

log

Log a message at a specified log level.

public log(string $level, string $message, array $context = []): void

Parameters:

ParameterTypeDescription
$levelstringThe log level (e.g., "emergency," "error," "info").
$messagestringThe log message.
$contextarrayAdditional context data (optional).

Throws: