PHP Luminova: Application Logger Helper Class
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 the default logging system that complies with PSR's LoggerInterface
. It provides a unified and flexible set of static methods for managing application logs. This class acts as a dispatcher, ensuring compatibility with any PSR-compliant logger registered in the application's logger configuration.
When NovaLogger
is used as the default logger, the dispatch
method automatically sends critical logs to a remote server or email in production—provided an email address or URL is configured in the environment file (.env
).
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.
Usage
The Luminova Logger can be accessed and used in multiple ways, ensuring flexibility and convenience.
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
Logging messages using specific level:
$logger->info('This is an informational message.');
Logger::info('This is an informational message.');
Logger::error('This is an error message.');
Logger::log('critical', 'This is a critical issue.');
Dispatch message in production environment:
This setup will send the log to email if logger.mail.logs
is specified or to a remote URL logger.remote.logs
if URL is specified. If none of the configuration is specified it will log as an error
log.
Logger::dispatch('error', 'This is an error message.');
Note: The
dispatch
method automatically determines the appropriate destination (file, email, or remote server) based on the destination type and configuration, making log management more intuitive.
Specifically send a log to an email:
Logger::dispatch('[email protected]', 'Critical error detected.');
Logger::mail('[email protected]', 'Critical error detected.');
Specifically send a log to a remote URL:
Logger::dispatch('https://example.com/api/log', 'Critical error detected.');
Logger::remote('https://example.com/api/log', 'Critical error detected.');
Customization
To use a custom logger instead of the default:
- Implement the
App\Config\Logger
class'sgetLogger
method to return your preferred PSR-compliant logger. - The custom logger will automatically handle all logging operations in your application.
Example: Custom Logger Integration
public function getLogger(): ?LoggerInterface
{
return new MonologLogger('custom-channel');
}
This makes it easy to switch logging implementations while maintaining PSR compliance.
Class Definition
- Class namespace:
\Luminova\Logger\Logger
- This class marked as final
Methods
constructor
Allows initialization of logger instance.
public __construct(): mixed
getLogger
Get the shared instance of the application's PSR-compliant logger.If no logger is specified in App\Config\Logger->getLogger()
, the default NovaLogger
is used.
public static getLogger(): \Psr\Log\LoggerInterface
Return Value:
\Psr\Log\LoggerInterface|Luminova\Logger\NovaLogger
- Return the active logger instance.
emergency
Logs a system emergency (highest severity).
public static emergency(string $message, array $context = []): void
Parameters:
Parameter | Type | Description |
---|---|---|
$message | string | The emergency message to log. |
$context | array | Additional context data (optional). |
alert
Logs an alert that requires immediate action.
public static alert(string $message, array $context = []): void
Parameters:
Parameter | Type | Description |
---|---|---|
$message | string | The alert message to log. |
$context | array | Additional context data (optional). |
critical
Logs a critical condition that requires prompt attention.
public static critical(string $message, array $context = []): void
Parameters:
Parameter | Type | Description |
---|---|---|
$message | string | The critical message to log. |
$context | array | Additional context data (optional). |
error
Logs an error that prevents execution but does not require immediate shutdown.
public static error(string $message, array $context = []): void
Parameters:
Parameter | Type | Description |
---|---|---|
$message | string | The error message to log. |
$context | array | Additional context data (optional). |
warning
Logs a warning about a potential issue.
public static warning(string $message, array $context = []): void
Parameters:
Parameter | Type | Description |
---|---|---|
$message | string | The warning message to log. |
$context | array | Additional context data (optional). |
notice
Logs a normal but significant event.
public static notice(string $message, array $context = []): void
Parameters:
Parameter | Type | Description |
---|---|---|
$message | string | The notice message to log. |
$context | array | Additional context data (optional). |
info
Logs general informational messages.
public static info(string $message, array $context = []): void
Parameters:
Parameter | Type | Description |
---|---|---|
$message | string | The info message to log. |
$context | array | Additional context data (optional). |
debug
Logs debugging information for developers.
public static debug(string $message, array $context = []): void
Parameters:
Parameter | Type | Description |
---|---|---|
$message | string | The debug message to log. |
$context | array | Additional context data (optional). |
exception
Logs an exception with additional context.
public static 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
Logs a PHP runtime error.Alias for phpError
, logs PHP-related issues.
public static php(string $message, array $context = []): void
Parameters:
Parameter | Type | Description |
---|---|---|
$message | string | The php message to log. |
$context | array | Additional context data (optional). |
metrics
Logs performance and metric data.
public static metrics(string $data, array $context = []): void
Parameters:
Parameter | Type | Description |
---|---|---|
$data | string | The profiling data to log. |
$context | array | Additional context data (optional). |
log
Write log a message at a specified log level.
public static log(string $level, string $message, array $context = []): void
Parameters:
Parameter | Type | Description |
---|---|---|
$level | string | The log level (e.g., LogLevel::INFO , emergency ). |
$message | string | The log message. |
$context | array | Additional context data (optional). |
Throws:
- \Luminova\Exceptions\InvalidArgumentException - If logger does not implement LoggerInterface
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:
Parameter | Type | Description |
---|---|---|
$to | string|null | The destination for the log message (log level, email address, URL or NULL). |
$message | string | The message to log. |
$context | array | Additional data to provide context for the log (optional). |
Throws:
- \Luminova\Exceptions\InvalidArgumentException - If the provided destination is invalid (not a valid log level, email address, or URL).
- \Luminova\Exceptions\RuntimeException - If network or email logging is attempted in a non-novalogger class, or if the logger does not implement the PSR LoggerInterface.
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:
Parameter | Type | Description |
---|---|---|
$email | string | The email address to send the log message to. |
$message | string | The log message to be sent. |
$context | array | Additional context data to be included in the log message (optional). |
Throws:
- \Luminova\Exceptions\InvalidArgumentException - If the provided email address is invalid.
- \Luminova\Exceptions\RuntimeException - If the logger doesn't support email functionality.
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:
Parameter | Type | Description |
---|---|---|
$email | string | The URL of the remote server to send the log message to. |
$message | string | The log message to be sent. |
$context | array | Additional context data to be included in the log message (optional). |
Throws:
- \Luminova\Exceptions\InvalidArgumentException - If the provided URL is invalid.