Guide · Open source
How to use Monolog: install it and write logs in PHP
Updated · written with AI from the project's own documentation and checked against our catalogue data
Monolog is a logging library for PHP applications. It records messages and sends them to destinations such as files, network sockets, email inboxes, databases and various web services. This guide is for PHP developers who want structured logs from their application, and it shows how to install Monolog, create a logger and write your first log records.
Quick start
- 1Check your PHP version. Confirm you have PHP 8.1 or above for Monolog 3.x. Make sure Composer is installed.
- 2Install the package. Run composer require monolog/monolog in your project directory.
- 3Create a logger. Instantiate Logger with a channel name such as 'name'.
- 4Add a handler. Push a StreamHandler with a file path and a minimum level such as Level::Warning.
- 5Write records. Call methods such as warning() or error() to add records to the log.
What Monolog needs before you install it
Monolog is a PHP library and is installed with Composer. The README says Monolog 3.x works with PHP 8.1 or above. Older releases have different requirements: Monolog 2.x works with PHP 7.2 or above, and Monolog 1.x works with PHP 5.3 up to 8.1 but is not very maintained anymore.
- PHP 8.1 or above for Monolog 3.x
- Composer
- Monolog 1.x is not recommended, because only important fixes are made to it
Install Monolog with Composer
Run the Composer command below in your project directory. The README says this installs the latest version of Monolog.
composer require monolog/monologCreate a logger and add a handler
A logger is a log channel that holds one or more handlers. Handlers decide where records go. The README example creates a logger named 'name' and pushes a StreamHandler that writes to a file. The Level::Warning argument sets the minimum level the handler will record, so lower-level messages are skipped.
Include the Level, Logger and StreamHandler classes at the top of your file, as in the example below. Replace 'path/to/your.log' with the file path you want to use.
<?php
use Monolog\Level;
use Monolog\Logger;
use Monolog\Handler\StreamHandler;
// create a log channel
$log = new Logger('name');
$log->pushHandler(new StreamHandler('path/to/your.log', Level::Warning));
// add records to the log
$log->warning('Foo');
$log->error('Bar');Write log messages at different levels
Each log method corresponds to a severity level. In the example, warning and error messages are written to the file because they are at or above the handler's Level::Warning threshold. The README also notes that Monolog public APIs accept PSR-3 log levels as of version 1.11.0, although Monolog internally uses its own level scheme.
- Use warning() and error() for records that the example writes to the file
- Set the handler's level to control which records are kept
Send the same logs to several destinations
Monolog can send logs to files, sockets, inboxes, databases and various web services. The README says you can add more handlers to a logger, and the handlers, formatters and processors are documented in a separate file. Special handlers can also be used to build more advanced logging strategies.
- Read doc/02-handlers-formatters-processors.md in the repository for the full list of handlers
- Use the Monolog documentation at https://seldaek.github.io/monolog/ for more detail
Use Monolog with a framework or PSR-3 code
The README says Symfony, Laravel and Lumen come out of the box with Monolog, so you may not need to install it separately in those projects. Because Monolog implements the PSR-3 logger interface, libraries can type-hint against that interface and accept any compatible logger.
- Symfony and Laravel ship with Monolog according to the README
- Libraries can type-hint against the PSR-3 logger interface
What people use it for
- Writing application warnings and errors to a log file with a minimum severity level
- Sending the same log records to several destinations, such as files and other services, through multiple handlers
- Using a PSR-3 logger interface in a PHP library so the host application can choose its own logger
- Adding structured logging to a PHP app built on Symfony or Laravel, which the README says ship with Monolog
Frequently asked questions
How do I install Monolog?
Install it with Composer by running composer require monolog/monolog in your project directory. The README says this installs the latest version.
What PHP version does Monolog need?
Monolog 3.x works with PHP 8.1 or above. Monolog 2.x works with PHP 7.2 or above, and Monolog 1.x works with PHP 5.3 up to 8.1 but is not very maintained anymore.
Is Monolog free to use?
Monolog is licensed under the MIT License, as stated in the README. The LICENSE file in the repository has the details.
Does Monolog come with a log viewer?
No. The README covers the library, not a dashboard or finished log viewer. Monolog writes records to destinations such as files and services.
What is the PSR-3 interface in Monolog?
PSR-3 is a standard logger interface for PHP. Monolog implements it, so libraries can type-hint against it and any compatible logger can be used.
What are alternatives to Monolog?
The README lists comparable projects including Symfony Logger (a PSR-3 component), Laravel logging, Analog and KLogger. Monolog is also used through Symfony and Laravel, which ship with it.
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
Monolog suits PHP developers who need structured, multi-destination logs and teams using Symfony, Laravel or other frameworks that include it. It is not a fit for non-PHP projects, for very old PHP versions, or for anyone who wants a ready-made log viewer.
Install options & downloads