search
HomePHP LibrariesOther librariesmonolog-logging PHP library
monolog-logging PHP library

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.


Disclaimer

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

Logging with Monolog: From Devtools to SlackLogging with Monolog: From Devtools to Slack

17Feb2025

Monolog: A powerful PHP log library that helps you improve application development and maintenance efficiency Logging is an integral part of the application development and maintenance cycle, and it depends not only on the recorded data, but also on the way it is recorded. This article will dive into the Monolog package to show how it can help you get the most out of your logs. Core advantages: Widely Integrated: Monolog is a popular log library that integrates with most mainstream frameworks to facilitate developers to take advantage of its logging capabilities. It follows the PHP-FIG standard, making it easy to switch to other implementations. Flexible processing: Monolog supports multiple processors and bubbling mechanisms, allowing developers to record different types of messages to different outputs. It also supports sending log messages

LogLayer: A Modern Logging Library for TypeScript / JavaScriptLogLayer: A Modern Logging Library for TypeScript / JavaScript

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

Memcache vs. Memcached: Which PHP Library Should You Choose?Memcache vs. Memcached: Which PHP Library Should You Choose?

09Nov2024

Distinguishing "Memcache" and "Memcached" in PHPPHP offers two memcached libraries: memcache and memcached. Understanding their differences helps...

Memcache vs Memcached: Which PHP Memcached Library Should You Choose?Memcache vs Memcached: Which PHP Memcached Library Should You Choose?

19Nov2024

Memcache vs Memcached: Choosing the Right PHP Memcached LibraryIntroductionPHP offers two seemingly similar memcached libraries: memcache and...

How Do I Link Static Libraries That Depend on Other Static Libraries?How Do I Link Static Libraries That Depend on Other Static Libraries?

13Dec2024

Linking Static Libraries to Other Static Libraries: A Comprehensive ApproachStatic libraries provide a convenient mechanism to package reusable...

Which PHP Library Best Fits Your Email Address Validation Needs?Which PHP Library Best Fits Your Email Address Validation Needs?

18Nov2024

PHP Email Address Validation Libraries UncoveredEmail address validation plays a crucial role in data validation, but creating a...

See all articles