Luminova Framework

PHP Luminova: PSR Aware Logger Interface

Last updated: 2025-01-30 11:09:22

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


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|null - Return instance of logger class in-use, otherwise null.


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(\Throwable|string $message, array $context = []): void

Parameters:

ParameterTypeDescription
$message\Throwable|stringThe 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 $data, array $context = []): void

Parameters:

ParameterTypeDescription
$datastringThe 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: