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

Log Laravel Package

psr/log

PSR-3 logging interfaces for PHP. Provides LoggerInterface, traits, and related classes to standardize how libraries accept and emit log messages. Not a logger implementation—use it to type-hint a logger or build your own PSR-3 compliant logger.

View on GitHub
Deep Wiki
Context7

Getting Started

Start by installing the package via Composer: composer require psr/log. This gives you access to the Psr\Log\LoggerInterface and supporting classes like AbstractLogger, LoggerAwareTrait, and TestLogger. Your first step is to inject LoggerInterface into classes that need logging — typically via constructor injection. Use the TestLogger for unit tests to assert log messages without writing to disk. Remember: this package only provides the interface, not a concrete logger — you’ll pair it with a full logging library like Monolog, Symfony’s Logger, or Laravel’s built-in logger (which already adheres to PSR-3).

Implementation Patterns

  • Dependency Injection: Always type-hint LoggerInterface in constructors or methods. Laravel automatically resolves the logger service (which implements PSR-3) when you type-hint it, so no extra config is needed.
  • Optional Logging: Use null coalescing or conditional checks: $logger?->info('...') (PHP 8.0+) or if ($logger) { $logger->info(...) } for graceful fallbacks.
  • Laravel Integration: Laravel’s Log facade and Psr\LoggerInterface are fully compatible. You can inject LoggerInterface directly into jobs, mailables, middleware, or service classes and leverage Laravel’s stackable, channel-based configuration (e.g., stack, daily, single) without changes.
  • Testing: Use Psr\Log\Test\TestLogger in tests to capture and inspect log entries:
    $logger = new TestLogger;
    $service = new MyService($logger);
    $service->doSomething();
    $this->assertTrue($logger->hasInfoThatContains('Doing work'));
    

Gotchas and Tips

  • No standalone functionality: This package only defines the interface — if you need actual logging (to files, Slack, etc.), install a PSR-3-compliant implementation (e.g., monolog/monolog).
  • Context must be array|mixed[]: Avoid passing non-array values to context (e.g., objects or scalars); use ['error' => (string) $e] or ['data' => json_encode($data)]. PSR-3 requires context to be an array of scalar or array values.
  • Return types matter: PSR-3 logger methods (e.g., info()) return void (since v2.0+). Don’t chain or assign results.
  • PHP 8.0+ only: Version 2.0+ requires PHP 8.0+ due to strict typing and union types (array|mixed[]). Ensure your project supports this.
  • Static analysis alignment: Type hints like mixed[] in context help static analyzers; avoid mixed $context outside interface compliance.
  • Extension tip: Implement LoggerAwareInterface + use LoggerAwareTrait in your classes to auto-inject the logger from Laravel’s container — but prefer constructor injection for clarity and testability in modern apps.
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
christhompsontldr/phpsdk
enqueue/dsn
bunny/bunny
enqueue/test
enqueue/null
enqueue/amqp-tools
milesj/emojibase
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