What is Monolog and why is it needed?
Monolog is a standard logging library for PHP. It provides a flexible and powerful toolkit for writing logs to various channels (files, databases, email, system log, external services, etc.). Monolog is the de facto standard for PHP applications, especially in the Symfony, Laravel, and other modern frameworks ecosystem.
Why is this library needed? In any application, it is necessary to track events: errors, warnings, informational messages, debug information. Monolog allows you to:
- Centralize logging — write logs in a single format from anywhere in the application.
- Flexibly configure channels — send critical errors to Telegram/Slack, and informational messages to a file.
- Format messages — add date, level, context, call stack.
- Filter logs — process only messages of a certain level.
- Integrate with frameworks — Symfony, Laravel, Yii2 use Monolog specifically.
Installation
Monolog is installed via Composer — the standard package manager for PHP.
composer require monolog/monologIf you are using PHP 8.0+, it is recommended to install the latest stable version. After installation, the project will have a vendor folder and a composer.json file with the dependency.
Quick Start
The simplest example — writing a log to a file:
<?phprequire 'vendor/autoload.php';
use Monolog\\Logger;use Monolog\\Handler\\StreamHandler;
// Create a logger with the channel name 'my_app'$log = new Logger('my_app');// Add a handler — writing to a file$log->pushHandler(new StreamHandler('app.log', Logger::WARNING));
// Now you can log$log->warning('This is a warning');$log->error('This is an error');
echo "Logs written to app.log";?>After running the script, the app.log file will contain something like:
[2025-04-09T10:30:00.123456+00:00] my_app.WARNING: This is a warning [] [][2025-04-09T10:30:00.123789+00:00] my_app.ERROR: This is an error [] []Main Methods and Functions
Let's look at the key methods of Monolog. All of them are part of the Monolog\\Logger class or related classes.
1. pushHandler(HandlerInterface $handler)
What it does: Adds a handler to the stack. A logger can have multiple handlers, and each of them will receive all messages (if filtering is not configured).
Parameters:
$handler— an object implementingMonolog\\Handler\\HandlerInterface(e.g.,StreamHandler,FirePHPHandler,SlackHandler).
Example:
$log = new Logger('app');$log->pushHandler(new StreamHandler('debug.log', Logger::DEBUG));$log->pushHandler(new StreamHandler('errors.log', Logger::ERROR));// Now messages of level ERROR and above will go to both files2. addRecord(int $level, string $message, array $context = [])
What it does: The base method for writing a log. It is usually not called directly; instead, wrapper methods (debug(), info(), etc.) are used.
Parameters:
$level— the logging level (constants:Logger::DEBUG,Logger::INFO,Logger::WARNING, etc.).$message— the message text.$context— an array of additional data (e.g., user ID, IP address).
Example:
Also in library
PDF Generation in Go: Library Overview and Practical Guide
Working with ZIP Archives in PHP: Complete Guide to ZipArchive
Swift Application Configuration (iOS/macOS): Best Practices and Tools