Skip to content

Guide · Open source

How to use log: the PSR-3 logging interface in PHP

Updated · written with AI from the project's own documentation and checked against our catalogue data

php-fig/log (published as psr/log) is a PHP package that contains the PSR-3 logging interface. It is a shared contract that describes how a logger should be called, not a logger that writes anywhere by itself. This guide shows how to install it, how to accept a PSR-3 logger in your own code, and how to get a working logger to pair with it.

PSR Logphp-fig/log · ★ 10.4k · MIT

Quick start

  1. 1Install the package. Run composer require psr/log in your project directory. This needs PHP and Composer.
  2. 2Import the interface. Add use Psr\Log\LoggerInterface; at the top of the class that needs logging.
  3. 3Accept a logger in your constructor. Type-hint LoggerInterface on a constructor parameter so any compatible logger can be passed in.
  4. 4Call the log methods. Use methods such as info() and error() on the injected logger, passing a message and optionally a context array.
  5. 5Install a real implementation. Add a PSR-3 logger package, such as monolog/monolog, if you need output written to files.
  6. 6Pass the logger in. Create the logger in your application bootstrap and inject it wherever LoggerInterface is required.

What php-fig/log actually is

The README is direct about this: the repository holds the interfaces, classes and traits related to PSR-3, and it is not a logger of its own. It is an interface that describes a logger.

This matters because installing the package alone will not write any log files. Its job is to define the methods a logger must have, so that code depending on it can accept any compatible logger.

Install psr/log with Composer

The package is installed with Composer, so you need PHP and Composer available in your project. Run the command from your project directory.

  • Prerequisites: PHP and Composer
composer require psr/log

Type-hint LoggerInterface in your class

Depend on the interface, not on a specific logger. The README example takes an optional LoggerInterface in the constructor and checks whether a logger was provided before logging.

Calls such as info() and error() come from the interface. In the example, the error call passes the exception in an array under the key 'exception'.

  • Import the interface with use Psr\Log\LoggerInterface;
  • Accept LoggerInterface in the constructor so callers choose the logger
  • Guard calls when the logger is optional
<?php use Psr\Log\LoggerInterface; class Foo { private $logger; public function __construct(LoggerInterface $logger = null) { $this->logger = $logger; } public function doSomething() { if ($this->logger) { $this->logger->info('Doing work'); } try { $this->doSomethingElse(); } catch (Exception $exception) { $this->logger->error('Oh no!', array('exception' => $exception)); } // do something useful } }

Get a logger that writes to files

Because this package only provides the interface, you need an implementation to actually record messages. The README says you can pick one of the implementations of the interface to get a logger. It does not name one, so choose a PSR-3 implementation separately.

Projects often compared with this package include monolog/monolog and katzgrau/klogger. Check each project's own documentation for setup and file output options.

  • Install the implementation with Composer alongside psr/log
  • Pass the logger instance into the classes that accept LoggerInterface

Implement the interface yourself

If you are writing your own logger, you can require this package and implement Psr\Log\LoggerInterface in your code. The README points to the PSR-3 specification text for the full details, so read that before writing the methods.

  • Require the package with composer require psr/log
  • Implement Psr\Log\LoggerInterface
  • Follow the PSR-3 specification for expected behaviour

What people use it for

  • A PHP library that logs errors without forcing users into one logging tool
  • A team building a custom logger that should match the PSR-3 standard
  • A developer using a framework or tool that expects a PSR-3 logger to be passed in
  • Passing exceptions into log calls with an 'exception' context key, as in the README example

Frequently asked questions

Is psr/log a logger that writes to files?

No. The README states it is not a logger of its own, only an interface that describes a logger. To write log files you need a separate PSR-3 implementation.

How do I install php-fig/log?

The package is named psr/log and is installed with Composer. Run composer require psr/log in your project directory.

What do I need to use psr/log?

The package is written for PHP and installed with Composer, so you need both PHP and Composer. It is not used in non-PHP projects.

What license is php-fig/log under?

The package is licensed under MIT, according to the directory listing for this item.

Which logger should I use with psr/log?

Any PSR-3 compatible implementation works. Projects compared in this directory include monolog/monolog and katzgrau/klogger, and you should check each one's documentation to decide.

Why would I depend on an interface instead of a logger?

Depending on LoggerInterface lets your code accept any PSR-3 logger, so users of your library are not tied to one implementation.

New agent skills, repos and AI tools — every week

The best new skills and open-source projects, the fastest-growing repos, and one free prompt. Five minutes to read. Unsubscribe anytime.

Our verdict

Use psr/log if you are writing a PHP library, or a logger, that should work with any PSR-3 implementation. Pick a different package, such as a full logger like monolog/monolog, if you simply want a ready-made logger that writes to files.

Install options & downloads