PHP Luminova: PSR Aware Logger for Custom Application Logging
Inject a PSR-3 logger into an application class so logging can be configured by its caller.
The LoggerAware class implements 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.
Using a PSR Logger in Application Code
Accept a LoggerInterface in your class constructor. This works with NovaLogger or a configured Monolog instance.
use Luminova\Logger\NovaLogger;
use Psr\Log\LoggerInterface;
final class ReportService
{
public function __construct(private LoggerInterface $logger)
{
}
public function markReady(int $reportId): void
{
$this->logger->info('Report is ready.', ['report_id' => $reportId]);
}
}
$service = new ReportService(new NovaLogger('reports'));
$service->markReady(42);For email or remote delivery, call the corresponding methods on NovaLogger.
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 function __construct(?LoggerInterface $logger = null)Parameters:
| Parameter | Type | Description |
|---|---|---|
$logger | ?LoggerInterface | The logger instance (default null). |
setLogger
Set a logger instance on the object.
public function setLogger(LoggerInterface $logger)Parameters:
| Parameter | Type | Description |
|---|---|---|
$logger | \Psr\Log\LoggerInterface | The logger instance. |
getLogger
Get the instance of PSR logger class.
public function getLogger(): ?LoggerInterfaceReturn:
LoggerInterface|null - Return instance of logger class inuse.
emergency
Log an emergency message.
public function emergency($message, array $context = array())Parameters:
| Parameter | Type | Description |
|---|---|---|
$message | string | The emergency message to log. |
$context | array<array-key, mixed> | Additional context data (optional). |
alert
Log an alert message.
public function alert($message, array $context = array())Parameters:
| Parameter | Type | Description |
|---|---|---|
$message | string | The alert message to log. |
$context | array<array-key, mixed> | Additional context data (optional). |
critical
Log a critical message.
public function critical($message, array $context = array())Parameters:
| Parameter | Type | Description |
|---|---|---|
$message | string | The critical message to log. |
$context | array<array-key, mixed> | Additional context data (optional). |
error
Log an error message.
public function error($message, array $context = array())Parameters:
| Parameter | Type | Description |
|---|---|---|
$message | string | The error message to log. |
$context | array<array-key, mixed> | Additional context data (optional). |
warning
Log a warning message.
public function warning($message, array $context = array())Parameters:
| Parameter | Type | Description |
|---|---|---|
$message | string | The warning message to log. |
$context | array<array-key, mixed> | Additional context data (optional). |
notice
Log a notice message.
public function notice($message, array $context = array())Parameters:
| Parameter | Type | Description |
|---|---|---|
$message | string | The notice message to log. |
$context | array<array-key, mixed> | Additional context data (optional). |
info
Log an info message.
public function info($message, array $context = array())Parameters:
| Parameter | Type | Description |
|---|---|---|
$message | string | The info message to log. |
$context | array<array-key, mixed> | Additional context data (optional). |
debug
Log a debug message.
public function debug($message, array $context = array())Parameters:
| Parameter | Type | Description |
|---|---|---|
$message | string | The debug message to log. |
$context | array<array-key, mixed> | Additional context data (optional). |
exception
Log an exception message.
public function exception(Throwable|string $message, array $context = []): voidParameters:
| Parameter | Type | Description |
|---|---|---|
$message | Throwable|string | The exception message to log. |
$context | array<string|int,mixed> | Additional context data (optional). |
php
Log an php message.
public function php(string $message, array $context = []): voidParameters:
| Parameter | Type | Description |
|---|---|---|
$message | string | The php message to log. |
$context | array<string|int,mixed> | Additional context data (optional). |
metrics
Log an performance metric.
public function metrics(string $data, array $context = []): voidParameters:
| Parameter | Type | Description |
|---|---|---|
$data | string | The php message to log. |
$context | array<string|int,mixed> | Additional context data (optional). |
log
Log a message at a specified log level.
public function log($level, $message, array $context = []): voidParameters:
| Parameter | Type | Description |
|---|---|---|
$level | string | The log level (e.g., emergency, error, info). |
$message | string | The log message. |
$context | array<array-key, mixed> | Additional context data (optional). |
Throws:
- Luminova\Exceptions\InvalidArgumentException - If logger does not implement LoggerInterface