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.
Primary Benefits:
- Flexibility: Easily switch between different logging implementations.
- Standards Compliance: Fully compatible with any PSR-compliant logger.
Usage Examples
Using a custom PSR logger class.
Implement your custom class:
namespace App\Utils;
use Psr\Log\LoggerInterface;
class MyLoggerClass implements LoggerInterface
{
// Your logger methods
}
Initialize aware logger with your custom logger instance:
use Luminova\Logger\LoggerAware;
use \App\Utils\MyLoggerClass;
$logger = new LoggerAware(new MyLoggerClass());
Initialize aware logger with Luminova NovaLogger
instance:
use Luminova\Logger\LoggerAware;
use Luminova\Logger\NovaLogger;
$logger = new LoggerAware(new NovaLogger('app'));
Initialize aware logger with Monolog logger instance:
use Luminova\Logger\LoggerAware;
use Monolog\Logger;
use Monolog\Handler\StreamHandler;
$monolog = new Logger('app');
$monolog->pushHandler(new StreamHandler('php://stdout', Logger::DEBUG));
$logger = new LoggerAware($monolog);
Log messages using your logger class:
$logger->alert('This is a alert message');
$logger->info('This is an info message');
$logger->debug('This is a debug message');
NovaLogger-Specific Features
Send log to emil or a remote URL:
$logger->mail('[email protected]', 'This is an email log message');
$logger->remote('https://example.com/api/log', 'This is a remote log message');
When sending logs via email or to a remote server, you can define a default log level to use in case of failure. The log level also serves as an indicator of log severity.
$logger->setLevel('debug')
->mail('[email protected]', 'This is a debug message');
Class Definition
- Full namespace:
\Luminova\Logger\LoggerAware
- Class parent:\Psr\Log\AbstractLogger
- 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|null
- Return instance of logger class in-use, otherwise null.
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(\Throwable|string $message, array $context = []): void
Parameters:
Parameter | Type | Description |
---|---|---|
$message | \Throwable|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 $data, array $context = []): void
Parameters:
Parameter | Type | Description |
---|---|---|
$data | 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