1
0
mirror of https://github.com/Seldaek/monolog.git synced 2025-02-23 14:32:44 +01:00
php-monolog/README.mdown

293 lines
15 KiB
Plaintext
Raw Normal View History

2013-01-07 21:23:22 +01:00
Monolog - Logging for PHP 5.3+ [![Build Status](https://secure.travis-ci.org/Seldaek/monolog.png)](http://travis-ci.org/Seldaek/monolog)
==============================
2011-02-17 03:04:58 +01:00
2013-06-19 12:18:02 +02:00
[![Total Downloads](https://poser.pugx.org/monolog/monolog/downloads.png)](https://packagist.org/packages/monolog/monolog)
[![Latest Stable Version](https://poser.pugx.org/monolog/monolog/v/stable.png)](https://packagist.org/packages/monolog/monolog)
[![Reference Status](https://www.versioneye.com/php/monolog:monolog/reference_badge.svg)](https://www.versioneye.com/php/monolog:monolog/references)
2013-06-19 12:18:02 +02:00
2013-01-07 21:23:22 +01:00
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](https://github.com/php-fig/fig-standards/blob/master/accepted/PSR-3-logger-interface.md)
interface that you can type-hint against in your own libraries to keep
2013-01-07 21:26:46 +01:00
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.
2011-11-13 00:39:39 +00:00
2011-02-17 03:04:58 +01:00
Usage
-----
2014-10-21 21:10:18 +01:00
Install the latest version with `composer require monolog/monolog`
```php
<?php
2011-02-17 03:04:58 +01:00
use Monolog\Logger;
use Monolog\Handler\StreamHandler;
2011-02-17 03:04:58 +01:00
// create a log channel
$log = new Logger('name');
$log->pushHandler(new StreamHandler('path/to/your.log', Logger::WARNING));
// add records to the log
$log->addWarning('Foo');
$log->addError('Bar');
```
2011-02-17 03:04:58 +01:00
2011-02-21 19:59:48 +01:00
Core Concepts
-------------
2012-04-27 08:39:21 -07:00
Every `Logger` instance has a channel (name) and a stack of handlers. Whenever
2012-04-22 18:04:40 +02:00
you add a record to the logger, it traverses the handler stack. Each handler
2014-10-28 13:17:31 +01:00
decides whether it fully handled the record, and if so, the propagation of the
2012-04-22 18:04:40 +02:00
record ends there.
2012-12-31 09:47:56 +01:00
This allows for flexible logging setups, for example having a `StreamHandler` at
2012-04-22 18:04:40 +02:00
the bottom of the stack that will log anything to disk, and on top of that add
2012-04-27 08:39:21 -07:00
a `MailHandler` that will send emails only when an error message is logged.
2012-04-27 18:55:50 +03:00
Handlers also have a `$bubble` property which defines whether they block the
2012-04-27 08:39:21 -07:00
record or not if they handled it. In this example, setting the `MailHandler`'s
2013-09-02 12:20:30 +02:00
`$bubble` argument to false means that records handled by the `MailHandler` will
not propagate to the `StreamHandler` anymore.
2012-04-22 18:04:40 +02:00
2012-04-27 08:39:21 -07:00
You can create many `Logger`s, each defining a channel (e.g.: db, request,
2012-04-22 18:04:40 +02:00
router, ..) and each of them combining various handlers, which can be shared
or not. The channel is reflected in the logs and allows you to easily see or
filter records.
Each Handler also has a Formatter, a default one with settings that make sense
will be created if you don't set one. The formatters normalize and format
incoming records so that they can be used by the handlers to output useful
information.
2012-06-19 20:39:54 +02:00
Custom severity levels are not available. Only the eight
[RFC 5424](http://tools.ietf.org/html/rfc5424) levels (debug, info, notice,
warning, error, critical, alert, emergency) are present for basic filtering
purposes, but for sorting and other use cases that would require
flexibility, you should add Processors to the Logger that can add extra
information (tags, user ip, ..) to the records before they are handled.
2011-02-21 19:59:48 +01:00
2012-04-22 18:05:03 +02:00
Log Levels
----------
2013-10-02 16:47:36 +02:00
Monolog supports the logging levels described by [RFC 5424](http://tools.ietf.org/html/rfc5424).
2012-04-22 18:05:03 +02:00
- **DEBUG** (100): Detailed debug information.
- **INFO** (200): Interesting events. Examples: User logs in, SQL logs.
- **NOTICE** (250): Normal but significant events.
2012-04-22 18:05:03 +02:00
- **WARNING** (300): Exceptional occurrences that are not errors. Examples:
Use of deprecated APIs, poor use of an API, undesirable things that are not
necessarily wrong.
- **ERROR** (400): Runtime errors that do not require immediate action but
should typically be logged and monitored.
- **CRITICAL** (500): Critical conditions. Example: Application component
unavailable, unexpected exception.
- **ALERT** (550): Action must be taken immediately. Example: Entire website
down, database unavailable, etc. This should trigger the SMS alerts and wake
you up.
- **EMERGENCY** (600): Emergency: system is unusable.
2011-05-09 14:57:05 +02:00
Docs
====
2012-06-19 20:39:54 +02:00
**See the `doc` directory for more detailed documentation.
2012-04-27 08:39:21 -07:00
The following is only a list of all parts that come with Monolog.**
2011-09-17 11:43:09 +02:00
Handlers
--------
2011-02-21 19:59:48 +01:00
### Log to files and syslog
2012-04-27 08:39:21 -07:00
- _StreamHandler_: Logs records into any PHP stream, use this for log files.
2012-04-22 18:04:40 +02:00
- _RotatingFileHandler_: Logs records to a file and creates one logfile per day.
2012-04-27 08:39:21 -07:00
It will also delete files older than `$maxFiles`. You should use
2012-04-22 18:04:40 +02:00
[logrotate](http://linuxcommand.org/man_pages/logrotate8.html) for high profile
setups though, this is just meant as a quick and dirty solution.
- _SyslogHandler_: Logs records to the syslog.
2013-07-17 17:58:51 +02:00
- _ErrorLogHandler_: Logs records to PHP's
[`error_log()`](http://docs.php.net/manual/en/function.error-log.php) function.
### Send alerts and emails
- _NativeMailerHandler_: Sends emails using PHP's
2012-04-27 08:39:21 -07:00
[`mail()`](http://php.net/manual/en/function.mail.php) function.
2012-06-19 22:15:10 +02:00
- _SwiftMailerHandler_: Sends emails using a [`Swift_Mailer`](http://swiftmailer.org/) instance.
2013-01-06 21:47:20 +01:00
- _PushoverHandler_: Sends mobile notifications via the [Pushover](https://www.pushover.net/) API.
- _HipChatHandler_: Logs records to a [HipChat](http://hipchat.com) chat room using its API.
2014-03-23 18:53:19 +01:00
- _FlowdockHandler_: Logs records to a [Flowdock](https://www.flowdock.com/) account.
2014-06-15 16:38:49 +02:00
- _SlackHandler_: Logs records to a [Slack](https://www.slack.com/) account.
2014-09-29 22:12:28 +01:00
- _MandrillHandler_: Sends emails via the Mandrill API using a [`Swift_Message`](http://swiftmailer.org/) instance.
- _FleepHookHandler_: Logs records to a [Fleep](https://fleep.io/) conversation using Webhooks.
### Log specific servers and networked logging
2012-04-22 18:04:40 +02:00
- _SocketHandler_: Logs records to [sockets](http://php.net/fsockopen), use this
for UNIX and TCP sockets. See an [example](https://github.com/Seldaek/monolog/blob/master/doc/sockets.md).
2012-06-19 22:15:10 +02:00
- _AmqpHandler_: Logs records to an [amqp](http://www.amqp.org/) compatible
server. Requires the [php-amqp](http://pecl.php.net/package/amqp) extension (1.0+).
- _GelfHandler_: Logs records to a [Graylog2](http://www.graylog2.org) server.
2012-08-18 18:51:10 +02:00
- _CubeHandler_: Logs records to a [Cube](http://square.github.com/cube/) server.
- _RavenHandler_: Logs records to a [Sentry](http://getsentry.com/) server using
[raven](https://packagist.org/packages/raven/raven).
2013-02-13 18:13:14 +01:00
- _ZendMonitorHandler_: Logs records to the Zend Monitor present in Zend Server.
2012-08-23 01:36:36 +04:00
- _NewRelicHandler_: Logs records to a [NewRelic](http://newrelic.com/) application.
2013-11-14 20:46:26 +01:00
- _LogglyHandler_: Logs records to a [Loggly](http://www.loggly.com/) account.
- _RollbarHandler_: Logs records to a [Rollbar](https://rollbar.com/) account.
- _SyslogUdpHandler_: Logs records to a remote [Syslogd](http://www.rsyslog.com/) server.
- _LogEntriesHandler_: Logs records to a [LogEntries](http://logentries.com/) account.
### Logging in development
- _FirePHPHandler_: Handler for [FirePHP](http://www.firephp.org/), providing
inline `console` messages within [FireBug](http://getfirebug.com/).
- _ChromePHPHandler_: Handler for [ChromePHP](http://www.chromephp.com/), providing
inline `console` messages within Chrome.
- _BrowserConsoleHandler_: Handler to send logs to browser's Javascript `console` with
no browser extension required. Most browsers supporting `console` API are supported.
### Log to databases
- _RedisHandler_: Logs records to a [redis](http://redis.io) server.
- _MongoDBHandler_: Handler to write records in MongoDB via a
[Mongo](http://pecl.php.net/package/mongo) extension connection.
- _CouchDBHandler_: Logs records to a CouchDB server.
- _DoctrineCouchDBHandler_: Logs records to a CouchDB server via the Doctrine CouchDB ODM.
- _ElasticSearchHandler_: Logs records to an Elastic Search server.
- _DynamoDbHandler_: Logs records to a DynamoDB table with the [AWS SDK](https://github.com/aws/aws-sdk-php).
### Wrappers / Special Handlers
2012-04-22 18:04:40 +02:00
- _FingersCrossedHandler_: A very interesting wrapper. It takes a logger as
parameter and will accumulate log records of all levels until a record
exceeds the defined severity level. At which point it delivers all records,
including those of lower severity, to the handler it wraps. This means that
until an error actually happens you will not see anything in your logs, but
when it happens you will have the full information, including debug and info
records. This provides you with all the information you need, but only when
you need it.
2014-12-29 21:19:48 +00:00
- _WhatFailureGroupHandler_: This handler extends the _GroupHandler_ ignoring
exceptions raised by each child handler. This allows you to ignore issues
where a remote tcp connection may have died but you do not want your entire
application to crash and may wish to continue to log to other handlers.
2012-04-22 18:04:40 +02:00
- _BufferHandler_: This handler will buffer all the log records it receives
2012-04-27 08:39:21 -07:00
until `close()` is called at which point it will call `handleBatch()` on the
2012-04-22 18:04:40 +02:00
handler it wraps with all the log messages at once. This is very useful to
send an email with all records at once for example instead of having one mail
for every log record.
- _GroupHandler_: This handler groups other handlers. Every record received is
sent to all the handlers it is configured with.
- _FilterHandler_: This handler only lets records of the given levels through
to the wrapped handler.
- _SamplingHandler_: Wraps around another handler and lets you sample records
if you only want to store some of them.
2014-12-29 21:19:48 +00:00
- _NullHandler_: Any record it can handle will be thrown away. This can be used
to put on top of an existing handler stack to disable it temporarily.
- _PsrHandler_: Can be used to forward log records to an existing PSR-3 logger
2012-04-22 18:04:40 +02:00
- _TestHandler_: Used for testing, it records everything that is sent to it and
has accessors to read out the information.
2011-02-21 19:59:48 +01:00
2011-05-09 14:57:05 +02:00
Formatters
----------
- _LineFormatter_: Formats a log record into a one-line string.
- _HtmlFormatter_: Used to format log records into a human readable html table, mainly suitable for emails.
- _NormalizerFormatter_: Normalizes objects/resources down to strings so a record can easily be serialized/encoded.
- _ScalarFormatter_: Used to format log records into an associative array of scalar values.
2011-05-09 14:57:05 +02:00
- _JsonFormatter_: Encodes a log record into json.
- _WildfireFormatter_: Used to format log records into the Wildfire/FirePHP protocol, only useful for the FirePHPHandler.
- _ChromePHPFormatter_: Used to format log records into the ChromePHP format, only useful for the ChromePHPHandler.
2014-03-23 20:42:39 +01:00
- _GelfMessageFormatter_: Used to format log records into Gelf message instances, only useful for the GelfHandler.
- _LogstashFormatter_: Used to format log records into [logstash](http://logstash.net/) event json, useful for any handler listed under inputs [here](http://logstash.net/docs/latest).
- _ElasticaFormatter_: Used to format log records into an Elastica\Document object, only useful for the ElasticSearchHandler.
2014-03-23 20:42:39 +01:00
- _LogglyFormatter_: Used to format log records into Loggly messages, only useful for the LogglyHandler.
- _FlowdockFormatter_: Used to format log records into Flowdock messages, only useful for the FlowdockHandler.
2014-12-06 16:59:28 +01:00
- _MongoDBFormatter_: Converts \DateTime instances to \MongoDate and objects recursively to arrays, only useful with the MongoDBHandler.
2011-05-09 14:57:05 +02:00
Processors
----------
- _IntrospectionProcessor_: Adds the line/file/class/method from which the log call originated.
2011-05-09 14:57:05 +02:00
- _WebProcessor_: Adds the current request URI, request method and client IP to a log record.
2011-07-20 00:07:40 +02:00
- _MemoryUsageProcessor_: Adds the current memory usage to a log record.
- _MemoryPeakUsageProcessor_: Adds the peak memory usage to a log record.
- _ProcessIdProcessor_: Adds the process id to a log record.
2013-02-26 10:19:42 +01:00
- _UidProcessor_: Adds a unique identifier to a log record.
2013-11-20 13:30:00 +01:00
- _GitProcessor_: Adds the current git branch and commit to a log record.
2014-04-04 17:18:39 +02:00
- _TagProcessor_: Adds an array of predefined tags to a log record.
2011-05-09 14:57:05 +02:00
2013-07-29 00:38:30 +02:00
Utilities
---------
2014-04-04 17:20:47 +02:00
- _Registry_: The `Monolog\Registry` class lets you configure global loggers that you
can then statically access from anywhere. It is not really a best practice but can
help in some older codebases or for ease of use.
2013-07-29 00:38:30 +02:00
- _ErrorHandler_: The `Monolog\ErrorHandler` class allows you to easily register
a Logger instance as an exception handler, error handler or fatal error handler.
- _ErrorLevelActivationStrategy_: Activates a FingersCrossedHandler when a certain log
level is reached.
- _ChannelLevelActivationStrategy_: Activates a FingersCrossedHandler when a certain
log level is reached, depending on which channel received the log record.
2015-01-07 12:49:55 +00:00
Third Party Packages
--------------------
Third party handlers, formatters and processors are
[listed in the wiki](https://github.com/Seldaek/monolog/wiki/Third-Party-Packages). You
can also add your own there if you publish one.
2011-05-09 14:57:05 +02:00
About
=====
2011-02-21 19:59:48 +01:00
Requirements
------------
2014-04-04 17:22:02 +02:00
- Monolog works with PHP 5.3 or above, and is also tested to work with HHVM.
2011-02-21 19:59:48 +01:00
2011-05-08 18:26:39 +02:00
Submitting bugs and feature requests
------------------------------------
2011-09-17 11:43:09 +02:00
Bugs and feature request are tracked on [GitHub](https://github.com/Seldaek/monolog/issues)
2011-05-08 18:26:39 +02:00
Frameworks Integration
----------------------
- Frameworks and libraries using [PSR-3](https://github.com/php-fig/fig-standards/blob/master/accepted/PSR-3-logger-interface.md)
can be used very easily with Monolog since it implements the interface.
- [Symfony2](http://symfony.com) comes out of the box with Monolog.
- [Silex](http://silex.sensiolabs.org/) comes out of the box with Monolog.
2015-02-05 11:06:13 +01:00
- [Laravel 4 & 5](http://laravel.com/) come out of the box with Monolog.
- [PPI](http://www.ppi.io/) comes out of the box with Monolog.
- [CakePHP](http://cakephp.org/) is usable with Monolog via the [cakephp-monolog](https://github.com/jadb/cakephp-monolog) plugin.
2013-04-05 16:36:57 +03:00
- [Slim](http://www.slimframework.com/) is usable with Monolog via the [Slim-Monolog](https://github.com/Flynsarmy/Slim-Monolog) log writer.
2013-09-16 11:22:24 +02:00
- [XOOPS 2.6](http://xoops.org/) comes out of the box with Monolog.
2013-12-12 11:38:58 -06:00
- [Aura.Web_Project](https://github.com/auraphp/Aura.Web_Project) comes out of the box with Monolog.
2015-01-29 15:56:43 +01:00
- [Nette Framework](http://nette.org/en/) can be used with Monolog via [Kdyby/Monolog](https://github.com/Kdyby/Monolog) extension.
- [Proton Micro Framework](https://github.com/alexbilbie/Proton) comes out of the box with Monolog.
2011-02-17 03:04:58 +01:00
Author
------
2011-03-27 06:23:03 -07:00
Jordi Boggiano - <j.boggiano@seld.be> - <http://twitter.com/seldaek><br />
2011-03-27 06:22:19 -07:00
See also the list of [contributors](https://github.com/Seldaek/monolog/contributors) which participated in this project.
2011-02-17 03:04:58 +01:00
License
-------
2012-04-27 08:39:21 -07:00
Monolog is licensed under the MIT License - see the `LICENSE` file for details
2011-02-17 03:04:58 +01:00
Acknowledgements
----------------
2012-04-22 18:04:40 +02:00
This library is heavily inspired by Python's [Logbook](http://packages.python.org/Logbook/)
library, although most concepts have been adjusted to fit to the PHP world.