Logger
The Logger plugin provides PSR-3 compatible logging with configurable output destinations and log levels.
PSR-3 Compatibility
The plugin implements Psr\Log\LoggerInterface, making it interoperable with any PSR-3 compliant library. The LoggerInterface port extends the PSR interface, allowing type-hints with either Phexium\Plugin\Logger\Port\LoggerInterface or Psr\Log\LoggerInterface.
Adapters
Two adapters are available:
- FileLogger writes log entries to a file with configurable minimum log level.
- NullLogger discards all messages, useful for testing.
Why Use It
Logging is essential for debugging, auditing, and monitoring application behavior. The Logger plugin provides a standardized interface that works across different environments—file logging in production, silent logging in tests.
Usage
The logger provides standard PSR-3 methods:
$this->logger->info('Book created', ['bookId' => $book->getId()]);
$this->logger->warning('Book not available', ['status' => $status]);
$this->logger->error('Operation failed', ['exception' => $e->getMessage()]);
Log levels from most to least severe: emergency, alert, critical, error, warning, notice, info, debug.
Testing
The NullLogger adapter prevents log file creation during tests:
$logger = new NullLogger();
$handler = new CreateBookHandler($repository, $eventBus, $logger);
// No log files created
For asserting log messages, a spy logger can capture calls:
final class SpyLogger extends NullLogger
{
public array $logs = [];
public function log($level, string|\Stringable $message, array $context = []): void
{
$this->logs[] = ['level' => $level, 'message' => $message];
}
}
See Also
- Commands & Handlers - Handlers use logging
- Test Doubles - SpyLogger for test assertions
- PSR-3 Logger Interface - Interface specification