Darken\Service\LogService

class

PSR-3 compatible logging service for the DarkenPHP framework. This service provides standard logging capabilities following PSR-3 standards and integrates seamlessly with the framework's dependency injection container.

Features:

  • Full PSR-3 compliance with all log levels (emergency, alert, critical, error, warning, notice, info, debug)
  • Message interpolation with placeholder support (e.g., "User {username} logged in")
  • Context data support for structured logging
  • Log retrieval and filtering capabilities
  • Integration with debug bars and development tools

Usage Examples:

Basic logging:

$logger = $kernel->getLogService();
$logger->info('User logged in', ['user_id' => 123]);
$logger->error('Database connection failed', ['host' => 'localhost']);

Message interpolation:

$logger->info('User {username} performed {action}', [
    'username' => 'john_doe',
    'action' => 'login'
]); // Results in: "User john_doe performed login"

Retrieving logs:

$allLogs = $logger->getLogs();
$errorLogs = $logger->getLogsByLevel('error');
$logger->clearLogs();

Debug bar integration:

$debugBar = new SomeDebugBar();
$logs = $app->getLogService()->getLogs();
$debugBar->addLogs($logs);

Methods

Method Details

public array getLogs () LogService.php#71

Get all stored logs. Returns an array of all log entries with their level, message, context, and timestamp. Useful for debugging, testing, and integration with debug tools.

Return array
public array getLogsByLevel (string $level) LogService.php#85

Get logs filtered by specific level. Filters and returns only log entries that match the specified level. Useful for analyzing specific types of log entries.

$level string
Return array
public void clearLogs () LogService.php#96

Clear all stored logs. Removes all log entries from memory. Useful for testing or when you want to start fresh log collection.

Return void
public void log ($level, string|Stringable $message, array $context) LogService.php#114

Logs with an arbitrary level. This is the core logging method that all PSR-3 log level methods delegate to. Implements message interpolation according to PSR-3 specifications.

Message placeholders are replaced with values from the context array. Placeholder format: {key} where 'key' corresponds to a context array key.

$level
$message string|Stringable
$context array
Return void