ForgeLogger Module
Overview
ForgeLogger is a simple yet powerful logging module for the Forge Framework. It provides straightforward logging capabilities with configurable log file locations and easy-to-use logging methods.
Features
- Simple Configuration: Easy to set up with minimal configuration required
- Flexible Log Storage: Configurable log file location
- Automatic Directory Creation: Creates log directories if they don't exist
- Log Levels: Support for different log levels (info, error, etc.)
- Timestamp Integration: Automatic timestamp addition to log entries
Configuration
Configure ForgeLogger through the config/storage.php
file:
// config/storage.php
return [
"log" => [
"path" => "storage/logs",
],
];
path: Specifies the directory where log files will be stored. The path is relative to your application's base directory.
Usage
The FileLogger class implements the LoggerInterface and provides a simple way to log messages:
use Forge\Modules\ForgeLogger\FileLogger;
// The logger is automatically configured with the settings from storage.php
$logger = new FileLogger($config);
// Log a message with a specific level
$logger->log('User login successful', 'info');
$logger->log('Database connection failed', 'error');
Examples
Here are some practical examples of using ForgeLogger in your application:
// Example: Logging in a controller
class UserController
{
private FileLogger $logger;
public function __construct(FileLogger $logger)
{
$this->logger = $logger;
}
public function login(Request $request): Response
{
try {
// Attempt login
$this->logger->log('Login attempt for user: ' . $request->input('email'), 'info');
// Login logic here...
$this->logger->log('Login successful', 'info');
} catch (Exception $e) {
$this->logger->log('Login failed: ' . $e->getMessage(), 'error');
throw $e;
}
}
}
The log entries will be written to app.log
in your configured log directory with the following format:
[2024-01-20 10:15:30] [info] Login attempt for user: user@example.com
[2024-01-20 10:15:31] [info] Login successful