fr3d/psr3-message-assertions
PHPUnit helper to assert your application’s PSR-3 log messages follow the Logger spec. Use the included TestLogger as a logger dependency during tests to validate message formatting and context placeholders. Composer-installable, BSD-2-Clause.
Log:: facade uses Monolog under the hood, which is PSR-3 compliant). No Laravel-specific dependencies.TestLogger, ensuring compliance without modifying production code.Log:: facade and PHPUnit test suite.tests/Feature or tests/Unit directories.tests/Unit/LoggingTest.php) to validate compliance.use Fr3d\Psr3MessageAssertions\PhpUnit\TestLogger;
public function test_log_messages_comply_with_psr3()
{
$logger = new TestLogger();
Log::useTestLogger($logger); // Hypothetical; may require dependency injection.
Log::error("Non-compliant: " . new stdClass()); // Should fail.
}
phpunit.xml as a dedicated test suite or pre-commit hook (e.g., via Laravel Pint or custom script).phpunit.xml snippet:
<testsuites>
<testsuite name="PSR-3 Compliance">
<directory>./tests/Compliance</directory>
</testsuite>
</testsuites>
TestLogger (requires refactoring).TestLogger during testing:
$this->app->bind(\Psr\Log\LoggerInterface::class, function () {
return new TestLogger();
});
composer require --dev fr3d/psr3-message-assertions phpunit/phpunit
TestLogger.use Fr3d\Psr3MessageAssertions\PhpUnit\TestLogger;
trait UsesTestLogger
{
protected function getTestLogger(): TestLogger
{
return new TestLogger();
}
}
TestLogger or replace the package.TestLogger is not properly injected, logs may bypass validation. Solution: Use Laravel’s service container or DI container."All log messages must be strings. Avoid logging objects directly; use
json_encode()or custom formatters."
README.md or CONTRIBUTING.md with logging standards.## Logging Standards
- Use `Log::error("User {id} failed login", ['id' => $userId])` (string + context).
- Avoid: `Log::error($exception)` (non-string message).
How can I help you explore Laravel packages today?