eliashaeussler / transient-logger
PSR-3 compliant logger to store logs in memory, especially useful for testing
Installs: 81 138
Dependents: 4
Suggesters: 0
Security: 0
Stars: 1
Watchers: 2
Forks: 0
Open Issues: 2
Requires
- php: ~8.1.0 || ~8.2.0 || ~8.3.0 || ~8.4.0
- psr/log: ^1.0 || ^2.0 || ^3.0
Requires (Dev)
- armin/editorconfig-cli: ^1.8 || ^2.0
- eliashaeussler/php-cs-fixer-config: ^2.0
- eliashaeussler/phpstan-config: ^2.4
- eliashaeussler/rector-config: ^3.0
- ergebnis/composer-normalize: ^2.39
- phpstan/extension-installer: ^1.3
- phpstan/phpstan-phpunit: ^1.3
- phpunit/phpunit: ^10.4 || ^11.0
Provides
- psr/log-implementation: 1.0 || 2.0 || 3.0
This package is auto-updated.
Last update: 2024-12-28 05:01:18 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).