Introduction
Monolog is the standard logging library for PHP. It implements the PSR-3 LoggerInterface and ships with over 70 handlers that send log records to files, databases, email, Slack, Elasticsearch, and other destinations. It is the default logger in Laravel, Symfony, and most modern PHP frameworks.
What Monolog Does
- Sends log records to files, syslog, stderr, databases, and external services
- Supports PSR-3 log levels: debug, info, notice, warning, error, critical, alert, emergency
- Formats log output as text, JSON, HTML, or custom formats via formatters
- Processes log records through middleware-like processors that enrich context
- Stacks multiple handlers with level-based filtering and bubble control
Architecture Overview
Monolog uses a channel-based architecture where each Logger instance holds a stack of handlers. When a log record is created, it passes through processors (which add data like memory usage, request ID, or stack traces), then flows through the handler stack. Each handler checks if it should handle the record based on its minimum level, formats it, and writes to its destination. Handlers can bubble records upward or stop propagation.
Setup & Configuration
- Install via Composer:
composer require monolog/monolog - Create a Logger with a channel name and push handlers onto it
- Set handler levels to filter what gets written to each destination
- Use processors like
WebProcessororMemoryUsageProcessorto enrich records - In Laravel use
config/logging.php; in Symfony usemonolog.yamlbundle config
Key Features
- 70+ built-in handlers for files, databases, email, Slack, Telegram, and cloud services
- PSR-3 compliant with drop-in compatibility across PHP frameworks
- Processors add request context, memory stats, Git commit info, and unique IDs
- Flexible formatter system with JSON, line, HTML, and custom output formats
- Handler groups, fingers-crossed buffering, and deduplication for production use
Comparison with Similar Tools
- Laravel Log — uses Monolog under the hood, adds Laravel-specific configuration
- KLogger — simple file logger, Monolog offers far more handlers and flexibility
- log4php — Apache project with XML config, Monolog is more PHP-idiomatic
- Sentry SDK — error tracking service, Monolog can forward to Sentry via its handler
FAQ
Q: How does Monolog integrate with Laravel?
A: Laravel uses Monolog as its logging backend. Configure channels in config/logging.php using the monolog driver.
Q: Can I send logs to multiple destinations simultaneously? A: Yes. Push multiple handlers onto the Logger. Each processes the record independently based on its level filter.
Q: What is the FingersCrossed handler? A: It buffers all log records and only flushes them when a record exceeds a threshold level, providing full context around errors without logging everything in normal operation.
Q: Is Monolog async? A: Monolog itself is synchronous. For async delivery, use handlers that write to fast local buffers (Redis, AMQP) and let workers forward logs asynchronously.