PSR-3 Logger Interface for PHP: Complete Guide with Examples

Online Python Trainer for Beginners

Learn Python easily without overwhelming theory. Solve practical tasks with automatic checking, get hints in Russian, and write code directly in your browser — no installation required.

Start Course

PSR-3 Logger Interface for PHP: Complete Guide with Examples

Logging is a critical aspect of any serious application development. Without it, you can't debug errors in production, track user behavior, or analyze system performance. However, when a project uses multiple libraries, each writing logs in its own way, developers face chaos: different levels, different formats, different output methods. That's exactly why the PSR-3 (Logger Interface) standard was created.

PSR-3 is not a logging library itself, but a standard interface (a set of contracts) that defines what a universal PHP logger should look like. It provides eight methods for logging messages at different severity levels (emergency, alert, critical, error, warning, notice, info, debug), plus a universal log() method. Any library implementing PSR-3 can be easily integrated into any project that expects this interface. This allows developers to swap logger implementations (e.g., from Monolog to Analog) without changing application code.

In this article, we'll dive deep into the psr/log library, from installation to advanced practical examples. You'll learn how to use each method correctly, format messages with placeholders, and avoid common pitfalls. This guide is useful for PHP beginners and experienced developers looking to structure their knowledge.

Installation

The psr/log library is installed via Composer, the standard dependency manager for PHP. Run the following command in your terminal:

composer require psr/log

After installation, you'll find the vendor/psr/log directory in your project. If you're using a framework (Laravel, Symfony), PSR-3 is likely already included. To load the autoloader in your PHP script, add:

require __DIR__ . '/vendor/autoload.php';

Note: psr/log is just the interface. For actual log writing, you need an implementation like monolog/monolog. Install Monolog with:

composer require monolog/monolog

Quick Start

Let's create a simple logging example using Monolog (the most popular PSR-3 implementation).

<?php
require 'vendor/autoload.php';

use Monolog\Logger;
use Monolog\Handler\StreamHandler;

// Create a logger named 'my_logger'
$log = new Logger('my_logger');

// Add a handler: write all messages from DEBUG level and above to app.log
$log->pushHandler(new StreamHandler(__DIR__ . '/app.log', Logger::DEBUG));

// Log several messages
$log->info('Application started');
$log->warning('Could not load config file, using defaults');
$log->error('Critical error: database unavailable');

echo "Logs written. Check the app.log file.";

After running this script, you'll see something like this in app.log:

[2025-04-10T15:30:45.123456+00:00] my_logger.INFO: Application started [] []
[2025-04-10T15:30:45.123789+00:00] my_logger.WARNING: Could not load config file, using defaults [] []
[2025-04-10T15:30:45.124012+00:00] my_logger.ERROR: Critical error: database unavailable [] []

That's a minimal working example. Now let's break down each interface method in detail.

Core Methods of the Psr\Log\LoggerInterface

The Psr\Log\LoggerInterface defines 9 methods. The first 8 are for specific logging levels, and the ninth is the universal log() method.

Recommendations