eliashaeussler / transient-logger
PSR-3 compliant logger to store logs in memory, especially useful for testing
Installs: 66 241
Dependents: 4
Suggesters: 0
Security: 0
Stars: 1
Watchers: 2
Forks: 0
Open Issues: 3
Requires
- php: ~8.1.0 || ~8.2.0 || ~8.3.0
- psr/log: ^1.0 || ^2.0 || ^3.0
Requires (Dev)
Provides
- psr/log-implementation: 1.0 || 2.0 || 3.0
This package is auto-updated.
Last update: 2024-11-23 05:37:43 UTC
README
Transient PSR-3 logger
This library provides a small PSR-3 compliant logger to stores log records in memory. Each log is converted to a log record and attached to the current logger instance. Logs will be available as long as the logger object is available in memory. This is especially useful for testing applications and libraries.
🔥 Installation
composer require --dev eliashaeussler/transient-logger
⚡ Usage
Create logger
The library provides a TransientLogger
class
which implements PSR's LoggerInterface
.
You can use it just like any other PSR-3 compliant logger:
use EliasHaeussler\TransientLogger; $logger = new TransientLogger\TransientLogger();
Log messages
For each logged message, a new Log\LogRecord
is
created and attached to the logger instance. The appropriate log levels are
represented by a Log\LogLevel
enum which is a wrapper
around PSR's LogLevel
constants.
// Log using generic log() method $logger->log( TransientLogger\Log\LogLevel::Alert, 'Houston, we have a problem!', ['error' => 'rocket down'], ); // Log using specific methods $logger->alert('Houston, we have a problem!', ['error' => 'rocket down']);
Access log records
You can access all log records in several ways:
// Get all log records $logs = $logger->getAll(); // Get by specific log level $errors = $logger->getByLogLevel(TransientLogger\Log\LogLevel::Error); // Iterate over log records foreach ($logger as $logRecord) { $level = $logRecord->level; // instanceof \EliasHaeussler\TransientLogger\Log\LogLevel $message = $logRecord->message; // string or instanceof Stringable $context = $logRecord->context; // array<string, mixed> }
Flush log
If required, you can always flush the log attached to a logger:
$logger->flushLog();
🧑💻 Contributing
Please have a look at CONTRIBUTING.md
.
⭐ License
This project is licensed under GNU General Public License 3.0 (or later).