Content · Web Development · PHP

PHP PSR-3 Logging Integration

PSR-3 defines common logger methods and interoperable log levels. Log contexts need careful redaction and correlation IDs; avoid leaking exception.

1 views Published Oct 17, 2026 Updated Oct 17, 2026 admin

PSR-3 defines common logger methods and interoperable log levels.

What It Does


PSR-3 defines common logger methods and interoperable log levels. The following examples distinguish a normal use case, a boundary case and a practical application.

Requirements and Compatibility


PHP 8.x and a local test environment. Apply input validation, correct error handling and the requirements of any optional libraries.

The code focuses on the core concept. Production implementation may also need routes, validation, persistence, authorization or deployment tooling.

Example 1: Basic Behavior


Install the PSR-3 contract and a concrete logger implementation.

composer require psr/log monolog/monolog

This is a command-line example; use it in its appropriate file or shell, not in the PHP source editor.

Example 2: Another Case


Use standard PSR-3 log levels and structured context rather than concatenating opaque text.

<?php
require __DIR__ . '/vendor/autoload.php';
use Monolog\Logger;
use Monolog\Handler\StreamHandler;
$logger = new Logger('app');
$logger->pushHandler(new StreamHandler('php://stderr', Logger::INFO));
$logger->info('Order queued', ['order_id' => 42, 'request_id' => 'r-72']);

This example depends on an installed extension, third-party library or configured service and is not a standalone local test.

Practical Example: Putting It to Use


Sanitize fields before passing them to any PSR-3 logger.

<?php
$context = ["event" => "login_failure", "user_id" => 21];
unset($context["password"], $context["access_token"]);
echo json_encode($context, JSON_THROW_ON_ERROR);

Important Behavior and Edge Cases


Log contexts need careful redaction and correlation IDs; avoid leaking exception contents.

Comparison with Related Functions or Approaches


OptionWhat it means in practice
PSR-3 Logging IntegrationPSR-3 defines common logger methods and interoperable log levels.
Related approachMonolog is one widely used PSR-3 implementation.

Common Beginner Mistakes


MistakeCorrect approach
Ignoring the important boundary for PSR-3 Logging IntegrationLog contexts need careful redaction and correlation IDs; avoid leaking exception contents.
Assuming a related mechanism has the same behaviorMonolog is one widely used PSR-3 implementation.

Key Takeaways


  • PSR-3 defines common logger methods and interoperable log levels.
  • Log contexts need careful redaction and correlation IDs; avoid leaking exception contents.
  • Monolog is one widely used PSR-3 implementation.

Before adapting these examples, verify the return values, test the failure paths and confirm that your application meets its security and environment requirements.

Share this resourceSend this page to someone who may find it useful.