Download the PHP package timdev/stack-logger without Composer
On this page you can find all versions of the php package timdev/stack-logger. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Informations about the package stack-logger
PHP Stack Logger
Wrap your PSR-3 logger with context accumulation and callable context elements.
Inspiration
Inspired by the similar functionality in pinojs. Design and implementation details differ, but the core idea remains: push scoped context on a logger and have it automatically pop off when the scope ends. This push/pop behavior is why I think of it as a "stack" logger.
Approach
The provided implementation decorates any
implementation of the PSR3 LoggerInterface, providing implementation of the
additional withContext
and addContext
methods defined in this library's
StackLogger interface.
Also provided is MonologStackLogger
, which
decorates a Monolog\Logger
and provides a working withName
implementation.
Requirements
- PHP >= 8.3
- A PSR-3 compatible logger, such as Monolog.
Usage
Installation
Context Stacking
This can be useful in any situation where want to carry some context through successive calls.
Dynamic (Callable) Context
The other feature provided here is callable context. Any context elements that
are callable
will be invoked at logging-time, and the result of the
computation will be logged. Callables take a single array argument:
function(array $context): mixed
NOTE: you should carefully consider the performance implications when using callables in your stacked context. Context is processed before invoking the wrapped logger's methods. The callables will be invoked on every logging method call, even if the underlying logger is configured to ignore the log-level.
NullLoggers
All StackLogger
implementations provide a static makeNullLogger()
method,
which returns an instance that is configured to discard all log messages. These
"null loggers" can be handy in tests, or as a default logger in classes that
can optionally accept a real logger:
To Do
- [ ] Make MonologStackLogger implement Monolog's ResettableInterface?
- [ ] Consider how this might play with Laravel, the insanely popular PHP framework that I don't personally use much. PRs welcome.