Monolog is a popular PHP logging library that implements the PSR-3 logging interface standard, making it compatible with PSR-3-compliant frameworks and applications. Monolog provides a flexible and structured way to log messages in PHP applications, which is essential for debugging and application maintenance.
Logger Instance: The core of Monolog is the Logger
class, which provides different log levels (e.g., debug
, info
, warning
, error
). Developers use these levels to capture log messages of varying severity in their PHP applications.
Handlers: Handlers are central to Monolog’s functionality and determine where and how log entries are stored. Monolog supports a variety of handlers, including:
Formatters: Handlers can be paired with Formatters to customize the log output. Monolog includes formatters for JSON output, simple text formatting, and others to suit specific logging needs.
Processors: In addition to handlers and formatters, Monolog provides Processors, which attach additional contextual information (e.g., user data, IP address) to each log entry.
Here is a basic example of initializing and using a Monolog logger:
use Monolog\Logger;
use Monolog\Handler\StreamHandler;
$logger = new Logger('name');
$logger->pushHandler(new StreamHandler(__DIR__.'/app.log', Logger::WARNING));
// Creating a log message
$logger->warning('This is a warning');
$logger->error('This is an error');
Monolog is widely adopted in the PHP ecosystem and is especially popular with frameworks like Symfony and Laravel.