monolog
Sends your logs to files, sockets, inboxes, databases and various web services
Top Related Projects
Free and open log management
Quick Overview
Monolog is a popular PHP logging library that sends logs to files, sockets, inboxes, databases, and various web services. It implements the PSR-3 interface for maximum interoperability and provides a flexible and extensible logging system for PHP applications.
Pros
- Supports a wide range of logging handlers and formatters
- Easy integration with popular PHP frameworks and libraries
- Highly customizable and extensible
- Follows PSR-3 standards for compatibility
Cons
- Can be overkill for simple logging needs
- Some advanced features may have a learning curve
- Performance impact when using multiple handlers or complex processing
Code Examples
- Basic usage:
use Monolog\Logger;
use Monolog\Handler\StreamHandler;
$log = new Logger('name');
$log->pushHandler(new StreamHandler('path/to/your.log', Logger::WARNING));
$log->warning('Foo');
$log->error('Bar');
- Using multiple handlers:
use Monolog\Logger;
use Monolog\Handler\StreamHandler;
use Monolog\Handler\FirePHPHandler;
$log = new Logger('name');
$log->pushHandler(new StreamHandler('path/to/your.log', Logger::DEBUG));
$log->pushHandler(new FirePHPHandler());
$log->info('My logger is now ready');
- Custom formatting:
use Monolog\Logger;
use Monolog\Handler\StreamHandler;
use Monolog\Formatter\JsonFormatter;
$log = new Logger('name');
$handler = new StreamHandler('path/to/your.log', Logger::DEBUG);
$handler->setFormatter(new JsonFormatter());
$log->pushHandler($handler);
$log->info('Log entry', ['extra' => 'data']);
Getting Started
To start using Monolog in your PHP project:
-
Install Monolog using Composer:
composer require monolog/monolog
-
Create a logger instance and add a handler:
use Monolog\Logger; use Monolog\Handler\StreamHandler; $log = new Logger('my_app'); $log->pushHandler(new StreamHandler('logs/app.log', Logger::DEBUG)); $log->info('Logger initialized');
-
Use the logger throughout your application:
$log->error('An error occurred', ['context' => $errorDetails]);
Competitor Comparisons
Pros of log
- Provides a standardized interface for logging libraries
- Lightweight and focused solely on defining the logging interface
- Allows for easy interoperability between different logging implementations
Cons of log
- Lacks built-in logging functionality; requires separate implementation
- Limited in scope compared to full-featured logging libraries
- Requires additional setup and configuration for practical use
Code Comparison
log (PSR-3 interface):
interface LoggerInterface
{
public function emergency($message, array $context = array());
public function alert($message, array $context = array());
public function critical($message, array $context = array());
// ... other log levels
}
monolog (Implementation):
use Monolog\Logger;
use Monolog\Handler\StreamHandler;
$log = new Logger('name');
$log->pushHandler(new StreamHandler('path/to/your.log', Logger::WARNING));
$log->warning('Foo');
Summary
log (PSR-3) defines a standard interface for logging in PHP, promoting interoperability between libraries. It's lightweight but requires separate implementation. monolog is a full-featured logging library that implements the PSR-3 interface, providing ready-to-use logging functionality with various handlers and formatters. While log ensures consistency across different logging libraries, monolog offers a complete solution out of the box, making it more convenient for immediate use in projects.
Free and open log management
Pros of graylog2-server
- Full-featured log management system with advanced search and analysis capabilities
- Scalable architecture suitable for handling large volumes of log data
- Web-based user interface for easy log exploration and visualization
Cons of graylog2-server
- More complex setup and configuration compared to Monolog
- Requires additional infrastructure and resources to run
- Steeper learning curve for users new to log management systems
Code Comparison
Monolog (PHP):
use Monolog\Logger;
use Monolog\Handler\StreamHandler;
$log = new Logger('name');
$log->pushHandler(new StreamHandler('path/to/your.log', Logger::WARNING));
$log->warning('Foo');
graylog2-server (Java):
import org.graylog2.log.Log;
import org.graylog2.plugin.ServerStatus;
private static final Log LOG = Log.getLog(YourClass.class);
LOG.warn("This is a warning message");
While Monolog is a lightweight logging library for PHP applications, graylog2-server is a comprehensive log management system. Monolog is easier to integrate into existing PHP projects, while graylog2-server offers more advanced features for log analysis and visualization. The choice between the two depends on the scale of your logging needs and the complexity of your infrastructure.
Convert designs to code with AI
Introducing Visual Copilot: A new AI model to turn Figma designs to high quality code using your components.
Try Visual CopilotREADME
Monolog - Logging for PHP
Note This is the documentation for Monolog 3.x, if you are using older releases see the documentation for Monolog 2.x or Monolog 1.x
Monolog sends your logs to files, sockets, inboxes, databases and various web services. See the complete list of handlers below. Special handlers allow you to build advanced logging strategies.
This library implements the PSR-3 interface that you can type-hint against in your own libraries to keep a maximum of interoperability. You can also use it in your applications to make sure you can always use another compatible logger at a later time. As of 1.11.0 Monolog public APIs will also accept PSR-3 log levels. Internally Monolog still uses its own level scheme since it predates PSR-3.
Installation
Install the latest version with
composer require monolog/monolog
Basic Usage
<?php
use Monolog\Level;
use Monolog\Logger;
use Monolog\Handler\StreamHandler;
// create a log channel
$log = new Logger('name');
$log->pushHandler(new StreamHandler('path/to/your.log', Level::Warning));
// add records to the log
$log->warning('Foo');
$log->error('Bar');
Documentation
- Usage Instructions
- Handlers, Formatters and Processors
- Utility Classes
- Extending Monolog
- Log Record Structure
Support Monolog Financially
Get supported Monolog and help fund the project with the Tidelift Subscription or via GitHub sponsorship.
Tidelift delivers commercial support and maintenance for the open source dependencies you use to build your applications. Save time, reduce risk, and improve code health, while paying the maintainers of the exact dependencies you use.
Third Party Packages
Third party handlers, formatters and processors are listed in the wiki. You can also add your own there if you publish one.
About
Requirements
- Monolog
^3.0
works with PHP 8.1 or above. - Monolog
^2.5
works with PHP 7.2 or above. - Monolog
^1.25
works with PHP 5.3 up to 8.1, but is not very maintained anymore and will not receive PHP support fixes anymore.
Support
Monolog 1.x support is somewhat limited at this point and only important fixes will be done. You should migrate to Monolog 2 or 3 where possible to benefit from all the latest features and fixes.
Submitting bugs and feature requests
Bugs and feature request are tracked on GitHub
Framework Integrations
- Frameworks and libraries using PSR-3 can be used very easily with Monolog since it implements the interface.
- Symfony comes out of the box with Monolog.
- Laravel comes out of the box with Monolog.
- Lumen comes out of the box with Monolog.
- PPI comes out of the box with Monolog.
- CakePHP is usable with Monolog via the cakephp-monolog plugin.
- Slim is usable with Monolog via the Slim-Monolog log writer.
- XOOPS 2.6 comes out of the box with Monolog.
- Aura.Web_Project comes out of the box with Monolog.
- Nette Framework is usable with Monolog via the contributte/monolog or orisai/nette-monolog extensions.
- Proton Micro Framework comes out of the box with Monolog.
- FuelPHP comes out of the box with Monolog.
- Equip Framework comes out of the box with Monolog.
- Yii 2 is usable with Monolog via the yii2-monolog or yii2-psr-log-target plugins.
- Hawkbit Micro Framework comes out of the box with Monolog.
- SilverStripe 4 comes out of the box with Monolog.
- Drupal is usable with Monolog via the monolog module.
- Aimeos ecommerce framework is usable with Monolog via the ai-monolog extension.
- Magento comes out of the box with Monolog.
- Spiral Framework comes out of the box with Monolog bridge.
Author
Jordi Boggiano - j.boggiano@seld.be - http://twitter.com/seldaek
See also the list of contributors who participated in this project.
License
Monolog is licensed under the MIT License - see the LICENSE file for details
Acknowledgements
This library is heavily inspired by Python's Logbook library, although most concepts have been adjusted to fit to the PHP world.
Top Related Projects
Free and open log management
Convert designs to code with AI
Introducing Visual Copilot: A new AI model to turn Figma designs to high quality code using your components.
Try Visual Copilot