One of the powerful capabilities of monolog is to specify the formatter that should be used. This functionality is still lacking in this module.

I changed the MonologLoggerChannelFactory in a way that it allows users to add a formatter to their services.yml file:


parameters:
  http.response.debug_cacheability_headers: true
  monolog.channel_handlers:
    # Log to the syslog by default.
    default: ['stream_handler']
    # Send the php channel to web server's error log and the browser console:
    php: ['error_log', 'browser_console']
    # Ignore log entries of "content" channel.
    content: ['null']
    formatter: 'logstash'

services:
  cache.backend.null:
    class: Drupal\Core\Cache\NullBackendFactory
  monolog.handler.stream_handler:
    class: Monolog\Handler\StreamHandler
    # Set alert as the minimum level (ignores emergency) and disable bubbling for this Handler
    arguments: ['/app/logs/test.log']
  monolog.formatter.logstash:
    class: Monolog\Formatter\LogstashFormatter
    arguments: ['test']

Support from Acquia helps fund testing for Drupal Acquia logo

Comments

legovaer created an issue. See original summary.

legovaer’s picture

Status: Active » Needs review
FileSize
3.21 KB
lussoluca’s picture

Status: Needs review » Needs work

Great idea! but with this implementation we can only have the same formatter for every handler.
Could we change the configuration to allow a different formatter for different handlers? Maybe something like:

parameters:
  monolog.channel_handlers:
    default: ['stream_handler'] // if no formatter specified fall back to LineFormatter
    php:
      handlers: ['error_log', 'browser_console']
      formatter: 'logstash'
    content: ['null']
lussoluca’s picture

Status: Needs work » Needs review
FileSize
26.31 KB
lussoluca’s picture

FileSize
8.27 KB
legovaer’s picture

Status: Needs review » Reviewed & tested by the community

This is way much better. Just tested it and it works perfect for me.

  • lussoluca committed 067dc8f on 8.x-1.x
    Issue #3019715 by lussoluca, legovaer: Specify formatter
    
lussoluca’s picture

Great!
Committed and pushed to 8.x-1.x!

lussoluca’s picture

Status: Reviewed & tested by the community » Fixed
dragonwize’s picture

Status: Fixed » Needs work

This breaks the existing functionality of the monolog library that uses the default formatter specified in the Handler's getDefaultFormatter().
https://github.com/Seldaek/monolog/search?q=getDefaultFormatter&type=Code

The default should not be line but instead nothing to let monolog set it. As it is now all handler's are using the line formatter that do not have the above yaml config set which is a BC break in a minor release.

lussoluca’s picture

Status: Needs work » Needs review
FileSize
1.21 KB

You are absolutely right. My fault.
If you agree with the attached patch I'll merge it and release a new version of the module

eiriksm’s picture

Status: Needs review » Reviewed & tested by the community

Had the same problem as #10. The patch fixes the problem, and I totally agree with the approach. Thanks for the quick fix. Would be nice to get a new stable release with this too.

  • lussoluca committed b216cac on 8.x-1.x
    Issue #3019715 by lussoluca, legovaer: Specify formatter
    
lussoluca’s picture

Status: Reviewed & tested by the community » Fixed

Committed and pushed to 8.x-1.x.

A new version is on the way

dragonwize’s picture

Also confirmed working as intended. Thanks for the fast turn around.

Status: Fixed » Closed (fixed)

Automatically closed - issue fixed for 2 weeks with no activity.