yii2-psr-log-target
A Yii 2 log target that forwards framework log messages to any PSR-3 compatible logger, such as Monolog.
Repository Health
Technical Analysis
yii2-psr-log-target bridges Yii 2’s built-in logging system with the broader PHP logging ecosystem by implementing a Yii Target that forwards every log message to a PSR-3 compatible logger such as Monolog. Instead of committing to Yii’s native file, email, or database targets, teams can plug in any PSR-3 logger and reuse its handlers, formatters, and processors (Slack, syslog, Sentry, and more) without changing any application code that calls Yii::info() or Yii::error().
Configuration happens entirely through the log application component: register PsrTarget as a target, hand it a LoggerInterface instance, and optionally restrict it to specific severity levels. The target maps Yii’s internal log levels (error, warning, info, trace, profile) onto PSR-3 levels, preserves message category, memory usage, and stack trace as PSR-3 context, and ships a PsrMessage value object for callers who want to attach structured context without relying on string interpolation.
What You Get
- A
PsrTargetYii 2 log target class implementing PSR-3’sLoggerAwareInterface - A
PsrMessageDTO for structured, string-castable log messages with attached context - Automatic mapping between Yii’s internal log levels and PSR-3
LogLevelconstants - Configurable level filtering via
setLevels(), accepting Yii level names or PSR-3 constants - Optional exception trace extraction and original-timestamp propagation into log context
Common Use Cases
- Centralizing logs from a Yii 2 app into Monolog handlers for Slack, syslog, Sentry, or an ELK stack
- Reusing a PSR-3 logger already standardized on elsewhere in the stack for Yii’s framework/application logs
- Routing only error/critical severity messages to high-cost alerting channels while keeping info/debug local
- Attaching structured metadata (user id, request id) to log entries via
PsrMessagefor downstream processors
Under The Hood
Architecture
The library is a single PsrTarget class extending Yii 2’s yii\log\Target and implementing PSR-3’s LoggerAwareInterface via LoggerAwareTrait, a textbook Adapter pattern sitting inside Yii’s own log-dispatcher plugin architecture. Its export() method overrides Yii’s Target contract, iterating Yii’s internal buffered message tuples ([text, level, category, timestamp, trace, memory]) and translating each into a PSR-3 log() call, assembling context from category, memory, and trace along the way. filterMessages() is overridden to intersect Yii’s level-based filtering with a custom _levels map built by setLevels(). The whole library is essentially one adapter class plus one value object (PsrMessage), with the one fragility point being its reliance on Yii’s positional, undocumented internal message-tuple shape.
Tech Stack
A pure PHP Composer package (type: yii2-extension) requiring yiisoft/yii2: ~2.0.0 and psr/log: ~1.0.2|~1.1.0|~3.0.0, spanning both PSR-3 v1 and v3 for broad compatibility. Dev dependencies include phpunit/phpunit: ~4.4|~10.4.2, an unusually wide range reflecting long-lived support across old and new PHP/PHPUnit versions. Autoloading is PSR-4 via Composer (samdark\log\ mapped to src), with no build tooling beyond Composer itself; CI runs via a GitHub Actions workflow (build.yml) alongside a legacy .travis.yml.
Code Quality
The tests/ directory contains a data-provider-driven PsrTargetTest covering every Yii level and every PSR-3 level mapping, backed by hand-rolled PSR-3 logger test doubles (PsrArrayLogger, PsrArrayLoggerFactory) that assert exact level/message/context output — solid behavioral coverage of the core mapping logic, though there’s no dedicated test file for PsrMessage or exception-trace extraction. The code has no static typing beyond docblocks, no PHPStan/Psalm configuration, and no linter config at the repo root, but naming is consistent with PSR-4 and Yii idioms (underscore-prefixed private properties, camelCase methods) and misconfiguration is surfaced explicitly via InvalidConfigException.
API Design
The public surface is minimal and idiomatic to Yii 2’s config-array style: register PsrTarget in the log component, assign a logger, and optionally set levels, addTimestampToContext, or extractExceptionTrace — no subclassing required to adopt it. PsrMessage offers an escape hatch for structured context while staying compatible with plain string log calls via __toString(). The README is example-heavy, with working snippets for PSR logger levels, timestamp context, and PsrMessage usage, which meaningfully lowers onboarding friction for anyone already familiar with Yii 2’s logging component configuration.