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
14May2023
With the continuous development and advancement of Internet technology, more and more applications need to handle large amounts of data and requests. In order to ensure that the application can run normally and problems can be detected in time, recording logs to troubleshoot problems has become particularly critical. Logging is an information recording method used to track and record the operation of the system. In PHP, Monolog is a popular logging library that provides a series of powerful logging methods to help developers better debug and optimize their applications. Introduction to MonologMon
11May2023
In the development of Go language, logging is a very important link. Through logs, important information such as the running status of the program, error messages, and performance bottlenecks can be recorded. There are many logging libraries to choose from in the Go language, such as log in the standard library, third-party libraries logrus, zap, etc. This article will introduce how to use the logging library in Go. 1. Log in the Go standard library The log package in the Go standard library provides a simple logging method that can be output to standard output, a file, or other io.Writer instances. log
30Sep2016
Looking for a php/python library management program (similar to Baidu library, managing doc/pdf and other libraries)~~ It mainly needs to have search functions, especially file classification retrieval/file tag retrieval functions, no need for online conversion, online browsing!
22Apr2024
Function libraries and third-party libraries in PHP can extend the functionality of applications. The function library provides predefined functions that can be included through the include statement. Third-party libraries are available from sources such as Packagist, GitHub, and installed using Composer. Implement automatic loading of classes through autoloaders, such as automatic loading of the Guzzle library. Learn how to use the Dompdf third-party library to generate PDF files through practical cases, including loading the library, loading HTML content, and outputting PDF files. The integration of function libraries and third-party libraries greatly expands the functionality of PHP applications and improves development efficiency and project performance.
17Apr2024
The steps for writing a function library in PHP are as follows: Create a PHP file (such as myFunctions.php) to store the functions. Use the function keyword to define functions in a file. Include libraries in other scripts using require_once or include_once statements. Once a function library is included, its functions can be used.
16Jan2024
Full analysis of the Golang log library: Helping you choose the most suitable logging tool. In the development process of the Go language, logging is an essential function. Proper logging can help us quickly and accurately detect problems and analyze system behavior. Choosing a log library that suits you can improve development efficiency and reduce code difficulty. This article will introduce several commonly used Golang log libraries to compare and analyze different application scenarios. log library The log library is a logging library built into the Go language.
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