Commit 067dc8f2 authored by lussoluca's avatar lussoluca Committed by lussoluca
Browse files

Issue #3019715 by lussoluca, legovaer: Specify formatter

parent dcbbf6c3
Monolog for Drupal 8
=======
====================
Overview
-------
--------
This module integrates Drupal with the fantastic [Monolog library by Seldaek](https://github.com/Seldaek/monolog) to provide a better logging solution. Some of the benefits of using this module are as follows:
- Configurable logging levels
- A multitude of handlers
- A multitude of handlers, formatters and processors
- All the power and flexibility of Monolog
The Drupal Monolog module also has full watchdog integration, so it works with core and contributed modules out of the box.
......@@ -60,7 +60,7 @@ Handlers
Handlers are registered as services in the [Drupal Service Container](https://www.drupal.org/docs/8/api/services-and-dependency-injection/services-and-dependency-injection-in-drupal-8).
You can define as many handlers as you need.
Each handler has a name (that should be under the *monolog.handler.* namespace), an implementing class and a list of arguments.
Each handler has a name (that must be under the *monolog.handler.* namespace), an implementing class and a list of arguments.
Mapping among logger channels and Monolog handlers is done defining parameters.
Under the *monolog.channel_handlers* parameter it is possible to define where to send logs from a specific channel.
......@@ -84,13 +84,13 @@ services:
arguments: ['private://logs/debug.log', 10, '%monolog.level.debug%']
```
The following method:
The following code:
```
\Drupal::logger('php')->debug('debug message');
```
will write the corresponding message to the *private://logs/php.log* file.
will write a message to the *private://logs/php.log* file.
Processors
----------
......@@ -109,9 +109,37 @@ parameters:
monolog.processors: ['message_placeholder', 'current_user', 'request_uri', 'ip', 'referer', 'filter_backtrace']
```
Formatters
----------
Monolog can alter the format of the message using *formatters*. A formatter needs to be registered as services in the
[Drupal Service Container](https://www.drupal.org/docs/8/api/services-and-dependency-injection/services-and-dependency-injection-in-drupal-8).
The module provides a set of already defined formatters like line formatter and json formatter. We suggest you to use the [Devel module](https://www.drupal.org/project/devel) or [Drupal Console](https://drupalconsole.com) to find all of them.
The following example will send all PHP specific logs to a separate file in the logstash format:
```
parameters:
monolog.channel_handlers:
php:
handlers: ['rotating_file_php']
formatter: 'logstash'
default: ['rotating_file_all']
monolog.processors: ['message_placeholder', 'current_user', 'request_uri', 'ip', 'referer']
services:
monolog.handler.rotating_file_php:
class: Monolog\Handler\RotatingFileHandler
arguments: ['private://logs/php.log', 10, 'monolog.level.debug']
monolog.handler.rotating_file_all:
class: Monolog\Handler\RotatingFileHandler
arguments: ['private://logs/debug.log', 10, 'monolog.level.debug']
```
If no formatter is specified the module will fallback to line formatter.
Log to database
--------
---------------
The Monolog module automatically register an handler for every enabled Drupal logger. To log to the standard
watchdog table it is possible to enable the Database Logging module and use *drupal.dblog* as handler:
......@@ -154,7 +182,7 @@ Examples
arguments: ['@monolog.handler.slack', null, 100]
```
You can find the complete list of Processors/Handlers [here](https://github.com/Seldaek/monolog/blob/master/doc/02-handlers-formatters-processors.md#handlers).
You can find the complete list of Processors/Handlers/Formatters [here](https://github.com/Seldaek/monolog/blob/master/doc/02-handlers-formatters-processors.md#handlers).
Extending Monolog
--------
......
......@@ -33,7 +33,7 @@ services:
monolog.handler.null:
class: Monolog\Handler\NullHandler
# Monolog processors
# Monolog processors.
monolog.processor.current_user:
class: Drupal\monolog\Logger\Processor\CurrentUserProcessor
arguments: ['@current_user']
......@@ -52,16 +52,47 @@ services:
monolog.processor.filter_backtrace:
class: Drupal\monolog\Logger\Processor\ContextKeyFilterProcessor
arguments: [['backtrace']]
monolog.processor.introspection:
class: Drupal\monolog\Logger\Processor\IntrospectionProcessor
monolog.processor.git:
class: Monolog\Processor\GitProcessor
monolog.processor.introspection:
class: Monolog\Processor\IntrospectionProcessor
monolog.processor.memory_usage:
class: Monolog\Processor\MemoryUsageProcessor
monolog.processor.memory_peak_usage:
class: Monolog\Processor\MemoryPeakUsageProcessor
monolog.processor.process_id:
class: Monolog\Processor\ProcessIdProcessor
# Monolog formatters.
monolog.formatter.chrome_php:
class: Monolog\Formatter\ChromePHPFormatter
monolog.formatter.elastica:
class: Monolog\Formatter\ElasticaFormatter
monolog.formatter.flowdock:
class: Monolog\Formatter\FlowdockFormatter
monolog.formatter.fluentd:
class: Monolog\Formatter\FluentdFormatter
monolog.formatter.gelf:
class: Monolog\Formatter\GelfMessageFormatter
monolog.formatter.html:
class: Monolog\Formatter\HtmlFormatter
monolog.formatter.json:
class: Monolog\Formatter\JsonFormatter
monolog.formatter.line:
class: Monolog\Formatter\LineFormatter
monolog.formatter.loggly:
class: Monolog\Formatter\LogglyFormatter
monolog.formatter.logstash:
class: Monolog\Formatter\LogstashFormatter
monolog.formatter.mongodb:
class: Monolog\Formatter\MongoDBFormatter
monolog.formatter.normalizer:
class: Monolog\Formatter\NormalizerFormatter
monolog.formatter.scalar:
class: Monolog\Formatter\ScalarFormatter
monolog.formatter.wildfire:
class: Monolog\Formatter\WildfireFormatter
# Should not be needed.
monolog.processor.psr_log_message:
class: Monolog\Processor\PsrLogMessageProcessor
......
......@@ -78,10 +78,26 @@ class MonologLoggerChannelFactory implements LoggerChannelFactoryInterface, Cont
$logger = new Logger($channel_name);
$parameters = $this->container->getParameter('monolog.channel_handlers');
$handlers = array_key_exists($channel_name, $parameters) ? $parameters[$channel_name] : $parameters['default'];
$config = array_key_exists($channel_name, $parameters) ? $parameters[$channel_name] : $parameters['default'];
$formatter = 'line';
$handlers = $config;
if (array_key_exists('handlers', $config)) {
$formatter = $config['formatter'];
$handlers = $config['handlers'];
}
foreach ($handlers as $handler) {
$logger->pushHandler($this->container->get('monolog.handler.' . $handler));
/** @var \Monolog\Handler\HandlerInterface $h */
$h = $this->container->get('monolog.handler.' . $handler);
if ($this->container->has('monolog.formatter.' . $formatter)) {
/** @var \Monolog\Formatter\FormatterInterface $f */
$f = $this->container->get('monolog.formatter.' . $formatter);
$h->setFormatter($f);
}
$logger->pushHandler($h);
}
foreach ($this->container->getParameter('monolog.processors') as $processor) {
......
<?php
namespace Drupal\monolog\Logger\Processor;
use Monolog\Logger;
use Monolog\Processor\IntrospectionProcessor as MonologIntrospectionProcessor;
/**
* Class IntrospectionProcessor
*/
class IntrospectionProcessor extends MonologIntrospectionProcessor {
/**
* @inheritDoc
*/
public function __construct() {
parent::__construct( Logger::DEBUG, array('Drupal\\monolog\\Logger\\'), 0);
}
}
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment