Luminova Framework

PHP Luminova: Application Logger Helper Class

Last updated: 2025-01-29 14:23:32

Logger class is a PSR-3 compliant logging system that extends the LoggerInterface. It provides a unified interface for handling application logs and serves as a dispatcher to ensure compatibility.

The Luminova Logger class is a PSR compliant logging system that implements the LoggerInterface. It provides a unified and flexible methods for managing application logs, serving as a dispatcher to ensure seamless compatibility with other PSR-compliant loggers.

Logger methods can be called either using the static methods from Luminova\Logger\Log class Logger class object for non-static, offering flexibility in usage.The logger class supports multiple logging destinations, including the filesystem, remote servers, and email.


Key Features

  • PSR Compliant: Fully implements LoggerInterface for smooth integration with third-party tools and libraries.
  • Default Logger: Preconfigured as the default logging mechanism in Luminova applications for immediate use.
  • Flexible Log Destinations: Allows logging to files, remote servers, or email, meeting diverse application needs.
  • Custom Logger Support: Easily replaceable with a custom PSR-compliant logger through application configuration.

Usage

The Luminova Logger can be accessed and used in multiple ways, ensuring flexibility and convenience.

Using Global Helper Function

Provides global functions and factory methods for convenient and quick access to logging functionalities.

// Log an informational message
logger('info', 'This is an informational message');

// Log a message to a remote server
logger('https://example.com/api/log', 'This is a remote log message');

// Log a message to an email
logger('[email protected]', 'This is an email log message');

Accessing the Logger via Factory

$logger = factory('logger');

or using the application factory:

use Luminova\Application\Factory;

$logger = Factory::logger();

Creating a New Logger Instance

use Luminova\Logger\Logger;

$logger = new Logger();

Basic Logging Examples

Log an informational message

$logger->info('This is an informational message.');

Log an error message

$logger->error('This is an error message.'); // Non-static method 
Logger::dispatch('error', 'This is an error message.'); // Dispatch method

Send log to an email

Logger::dispatch('[email protected]', 'Critical error detected.');
Logger::mail('[email protected]', 'Critical error detected.');

Send log to a remote server

Logger::dispatch('https://example.com/api/log', 'Critical error detected.');
Logger::remote('https://example.com/api/log', 'Critical error detected.');

Note: The dispatch method automatically determines the appropriate destination (file, email, or remote server) based on the input type, making log management more intuitive.

Static Logger Helper

The static logger help class, provide static method for easy logging.

use \Luminova\Logger\Log;

Log::info('This is an informational message.');
Log::error('This is an error message.');
``

***

### Customization

To use a custom logger instead of the default:
1. Implement the `App\Config\Logger` class's `getLogger` method to return your preferred PSR-compliant logger.
2. The custom logger will automatically handle all logging operations in your application. 

#### Example: Custom Logger Integration

```php
public function getLogger(): ?LoggerInterface
{
    return new MonologLogger('custom-channel');
}

This makes it easy to switch logging implementations while maintaining PSR compliance.


Class Definition

Properties

logger

PSR logger interface.

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

Methods

constructor

Initialize logger instance

public __construct(): mixed

getLogger

Get shared instance of your application PSR logger class.If no logger was specified in App\Config\Logger->getLogger() method, default NovaLogger will be returned.

public static getLogger(): \Psr\Log\LoggerInterface

Return Value:

\Psr\Log\LoggerInterface - Return instance of app 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:


dispatch

Dispatches a log message to a specified destination (file, email, or remote server) asynchronously.

The destination is determined by the provided parameter ($to) which can be a log level, email address, URL, or null. Email and remote logging are handled asynchronously. By default, in development, logs are written to a file unless an explicit email address or URL is specified.

In production, if no destination is provided, the method checks for default email or remote URL configurations in the environment file (logger.mail.logs or logger.remote.logs).

public static dispatch(string|null $to, string $message, array $context = []): void

Parameters:

ParameterTypeDescription
$tostring|nullThe destination for the log message (log level, email address, URL or NULL).
$messagestringThe message to log.
$contextarrayAdditional data to provide context for the log (optional).

Throws:


mail

Sends a log message via email.

This method validates the email address and message, then sends the log message to the specified email address using the configured logger.

public static mail(string $email, string $message, array $context = []): void

Parameters:

ParameterTypeDescription
$emailstringThe email address to send the log message to.
$messagestringThe log message to be sent.
$contextarrayAdditional context data to be included in the log message (optional).

Throws:


remote

Sends a log message to a remote server via HTTP POST.

This function sends a log message to a specified remote server using HTTP POST. The function validates the URL and ensures that the log message is not empty.If the URL is invalid or the log message is empty, the function returns without performing any action.

public static remote(string $url, string $message, array $context = []): void

Parameters:

ParameterTypeDescription
$emailstringThe URL of the remote server to send the log message to.
$messagestringThe log message to be sent.
$contextarrayAdditional context data to be included in the log message (optional).

Throws: