PHP Luminova: PSR Aware Logger Interface
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
- Full namespace:
\Luminova\Logger\LoggerAware
- This class implements:\Psr\Log\LoggerInterface, \Psr\Log\LoggerAwareInterface
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:
Parameter | Type | Description |
---|---|---|
$logger | \Psr\Log\LoggerInterface | The logger instance. |
setLogger
Set a logger instance on the object.
public setLogger(\Psr\Log\LoggerInterface $logger): void
Parameters:
Parameter | Type | Description |
---|---|---|
$logger | \Psr\Log\LoggerInterface | The 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:
Parameter | Type | Description |
---|---|---|
$message | string | The emergency message to log. |
$context | array | Additional context data (optional). |
alert
Log an alert message.
public alert(string $message, array $context = []): void
Parameters:
Parameter | Type | Description |
---|---|---|
$message | string | The alert message to log. |
$context | array | Additional context data (optional). |
critical
Log a critical message.
public critical(string $message, array $context = []): void
Parameters:
Parameter | Type | Description |
---|---|---|
$message | string | The critical message to log. |
$context | array | Additional context data (optional). |
error
Log an error message.
public error(string $message, array $context = []): void
Parameters:
Parameter | Type | Description |
---|---|---|
$message | string | The error message to log. |
$context | array | Additional context data (optional). |
warning
Log a warning message.
public warning(string $message, array $context = []): void
Parameters:
Parameter | Type | Description |
---|---|---|
$message | string | The warning message to log. |
$context | array | Additional context data (optional). |
notice
Log a notice message.
public notice(string $message, array $context = []): void
Parameters:
Parameter | Type | Description |
---|---|---|
$message | string | The notice message to log. |
$context | array | Additional context data (optional). |
info
Log an info message.
public info(string $message, array $context = []): void
Parameters:
Parameter | Type | Description |
---|---|---|
$message | string | The info message to log. |
$context | array | Additional context data (optional). |
debug
Log a debug message.
public debug(string $message, array $context = []): void
Parameters:
Parameter | Type | Description |
---|---|---|
$message | string | The debug message to log. |
$context | array | Additional context data (optional). |
exception
Log an exception message.
public exception(string $message, array $context = []): void
Parameters:
Parameter | Type | Description |
---|---|---|
$message | string | The EXCEPTION message to log. |
$context | array | Additional context data (optional). |
php
Log an php message.
public php(string $message, array $context = []): void
Parameters:
Parameter | Type | Description |
---|---|---|
$message | string | The php message to log. |
$context | array | Additional context data (optional). |
metrics
Log an performance metric.
public metrics(string $message, array $context = []): void
Parameters:
Parameter | Type | Description |
---|---|---|
$message | string | The php message to log. |
$context | array | Additional context data (optional). |
log
Log a message at a specified log level.
public log(string $level, string $message, array $context = []): void
Parameters:
Parameter | Type | Description |
---|---|---|
$level | string | The log level (e.g., "emergency," "error," "info"). |
$message | string | The log message. |
$context | array | Additional context data (optional). |
Throws:
- \Luminova\Exceptions\InvalidArgumentException - If logger does not implement LoggerInterface