Weave Code
Code Weaver
Helps Laravel developers discover, compare, and choose open-source packages. See popularity, security, maintainers, and scores at a glance to make better decisions.
Feedback
Share your thoughts, report bugs, or suggest improvements.
Subject
Message

Psr Log Aware Trait Laravel Package

mpdf/psr-log-aware-trait

Provides a PSR-3 LoggerAwareTrait implementation used by mPDF components. Adds simple logger injection and access following the psr/log LoggerAwareInterface, making it easy to add optional logging support without duplicating boilerplate across classes.

View on GitHub
Deep Wiki
Context7

Getting Started

This package provides a simple trait (Mpdf\PsrLogAwareTrait) to make classes compatible with any PSR-3 logger implementation — regardless of whether your project uses psr/log v1, v2, or v3. Start by including it in any class that needs to accept and use a PSR-3 logger:

use Mpdf\PsrLogAwareTrait;
use Psr\Log\LoggerInterface;

class MyService
{
    use PsrLogAwareTrait;

    public function __construct(?LoggerInterface $logger = null)
    {
        $this->setLogger($logger);
    }

    public function doSomething()
    {
        $this->logger->info('Starting process…');
        // ...
    }
}

No further setup is required — the trait handles internal logger compatibility behind the scenes.

Implementation Patterns

  • Lazy Logger Setup: Use the trait in long-lived services or libraries that may be instantiated before a logger is available. Call setLogger() early in the request lifecycle.
  • Library Compatibility: Ideal for reusable packages (e.g., middleware, formatters, workers) that need to support multiple PSR-3 versions without enforcing version constraints that conflict with app dependencies.
  • Null-Safe Integration: Works seamlessly with null loggers — no extra if ($this->logger) checks needed.
  • Type Hinting: Always type-hint Psr\Log\LoggerInterface (or ?LoggerInterface for optional), not concrete implementations.

Gotchas and Tips

  • Not a Logger Itself: This trait adds logger support to your class — it does not provide logging functionality. You still need to inject a valid PSR-3 logger.
  • Trait Name: Despite the namespace (Mpdf\…), it’s not tied to the mpdf/mpdf package — it’s generic and version-agnostic.
  • ⚠️ No Autoloading Requirement: The trait is light (~15 lines) but verify it’s installed via Composer (via transitive dependency, likely via mpdf/mpdf). Run composer show mPDF/psr-log-aware-trait to confirm.
  • 🔍 Debugging Tips: If your logger calls silently fail, check:
    • You called setLogger() — the trait doesn’t auto-inject.
    • Your app’s psr/log version doesn’t conflict with how other packages expect it (e.g., mpdf/mpdf historically lagged on PSR-3 v2+ support — this trait helps bridge that).
  • 🧩 Extension: You can use this alongside other traits (e.g., configuration, caching) — no naming conflicts expected.
  • 📦 Why Still Used?: Though modern Symfony/Laravel apps often auto-configure loggers, this remains useful for:
    • Legacy codebases
    • Composer plugin systems
    • Packages that must avoid psr/log version conflicts (e.g., ^1.0 || ^2.0 || ^3.0)
Weaver

How can I help you explore Laravel packages today?

Conversation history is not saved when not logged in.
Prompt
Add packages to context
No packages found.
davejamesmiller/laravel-breadcrumbs
artisanry/parsedown
bower-asset/punycode
bower-asset/inputmask
bower-asset/jquery
bower-asset/yii2-pjax
laravel/nova
spatie/laravel-mailcoach
spatie/laravel-superseeder
laravel/liferaft
nst/json-test-suite
danielmiessler/sec-lists
jackalope/jackalope-transport
twbs/bootstrap4
php-http/client-implementation
phpcr/phpcr-implementation
cucumber/gherkin-monorepo
haydenpierce/class-finder
psr/simple-cache-implementation
uri-template/tests