Monolog is a relatively complete and easily expandable logging library under PHP. Currently, many well-known PHP frameworks including Symfony, Laravel, CakePHP, etc. have built-in Monolog. Monolog can send your logs to files, sockets, inboxes, databases and various web services.
Monolog follows the PSR3 interface specification and can be easily replaced with other logging libraries that follow the same specification. Monolog has good scalability. Through the interfaces Handler, Formatter and Processor, the Monolog class library can be extended and customized in various ways.
Basic usage
<?php use Monolog\Logger; use Monolog\Handler\StreamHandler; // 创建日志频道 $log = new Logger('name'); $log->pushHandler(new StreamHandler('path/to/your.log', Logger::WARNING)); // 添加日志记录 $log->addWarning('Foo'); $log->addError('Bar');
Core concept
Each Logger instance contains a channel name (channel) and a handler stack. When you add a record, the record is processed through the handler stack. Each handler can also decide whether to pass the record to the next handler in the next stack.
Through handlers, we can implement some complex log operations. For example, if we put the StreamHandler at the bottom of the stack, all log records will eventually be written to the hard disk file. At the same time, we put the MailHandler at the top of the stack and send the error log via email by setting the log level. There is a $bubble attribute in Handler. This attribute defines whether the handler intercepts records and prevents them from flowing to the next handler. So if we set the $bubble parameter of MailHandler to false, when an error log occurs, the log will be sent through MailHandler instead of being written to the hard disk through StreamHandler.
Multiple Loggers can be created, and each can define its own channel name and handler stack. Handlers can be shared among multiple Loggers. The channel name will be reflected in the log, making it easier for us to view and filter log records.
If the log format (Formatter) is not specified, Handler will use the default Formatter.
The log levels cannot be customized. Currently, the eight levels defined in RFC 5424 are used: debug, info, notice, warning, error, critical, alert, and emergency. If you have other needs for log records, you can add content to the log records through Processo.
Log level
DEBUG (100): Detailed debug information.
INFO (200): Key event.
NOTICE (250): An ordinary but important event.
WARNING (300): A non-error exception occurred.
ERROR (400): Runtime error, but does not need to be handled immediately.
CRITICA (500): Serious error.
EMERGENCY (600): The system is unavailable.
All resources on this site are contributed by netizens or reprinted by major download sites. Please check the integrity of the software yourself! All resources on this site are for learning reference only. Please do not use them for commercial purposes. Otherwise, you will be responsible for all consequences! If there is any infringement, please contact us to delete it. Contact information: admin@php.cn
Related Article
16Jan2025
Tired of juggling multiple logging libraries across projects? Frustrated with inconsistent error and metadata logging? LogLayer, an open-source solution, streamlines your logging process and enhances developer experience. What is LogLayer? LogLayer
21Jan2025
Modern JavaScript applications demand structured logging. As application complexity increases, efficient log searching, analysis, and monitoring become paramount. However, many logging solutions surprisingly complicate this process. Traditional Jav
05Jan2025
PHP ORM Library RecommendationsWhen it comes to object-relational mapping (ORM) for PHP, there are several libraries that stand out. To address...
03Jan2025
Overflow: Hidden and Expansion of HeightjQuery distinguishes itself from other JavaScript libraries through its cross-platform compatibility and...
27Dec2024
Executing Command Line Binaries in Node.jsExecuting third-party binaries is an essential task when porting CLI libraries from other languages to...
15Jan2025
HeroQR: Your dream PHP QR code generation library. Are you still worried about QR code generation in PHP? Don't hesitate any longer! ?I am pleased to introduce you to HeroQR, an advanced open source PHP library designed to make QR code generation easy, powerful and flexible. Why choose HeroQR? HeroQR stands out for its customizability and ease of use. Whether you're a beginner looking for a simple QR code solution or an experienced developer in need of advanced features, HeroQR has what you need. Main Features of HeroQR HeroQR is designed to provide developers with powerful tools to create and customize QR codes. Here’s a quick overview of its standout features: Unparalleled customization with resizable additions
Hot Tools
PHP library for dependency injection containers
PHP library for dependency injection containers
A collection of 50 excellent classic PHP algorithms
Classic PHP algorithm, learn excellent ideas and expand your thinking
Small PHP library for optimizing images
Small PHP library for optimizing images