Commit c26929f0 authored by Luca Lusso's avatar Luca Lusso
Browse files

Issue #3223751 by lussoluca: Unable to decode output into JSON: Syntax error

parent 8b3cab8b
Loading
Loading
Loading
Loading
+3 −0
Original line number Diff line number Diff line
@@ -176,6 +176,9 @@ This will send PHP logs to the rotating file handler with json formatter and to
Drupal database handler with line formatter. All other logs goes to Syslog with
default formatter (line).

When sending logs to stdout or stderr it is preferable to use the `drush` line formatter to avoid conversion errors when
Drush runs a command that uses Batch API.

### Processors

Monolog can alter the messages being written to a logging facility using
+3 −0
Original line number Diff line number Diff line
@@ -109,6 +109,9 @@ services:
  monolog.formatter.wildfire:
    class: Monolog\Formatter\WildfireFormatter
    shared: false
  monolog.formatter.drush:
    class: Drupal\monolog\Logger\Formatter\DrushLineFormatter
    shared: false

  # Should not be needed.
  monolog.processor.psr_log_message:
+36 −0
Original line number Diff line number Diff line
<?php

declare(strict_types=1);

namespace Drupal\monolog\Logger\Formatter;

use Monolog\Formatter\LineFormatter;

/**
 * Formatter suitable to be using with Drush logs.
 */
class DrushLineFormatter extends LineFormatter {

  /**
   * {@inheritdoc}
   */
  protected function convertToString($data): string {
    if (null === $data || is_bool($data)) {
      return var_export($data, true);
    }

    if (is_scalar($data)) {
      return (string) $data;
    }

    $result = "";
    array_walk($data, function($val, $key) use(&$result) {
      if ($val != "" && is_scalar($val)) {
        $result .= " | $key=$val";
      }
    });

    return ltrim($result);
  }

}
+3 −1
Original line number Diff line number Diff line
@@ -35,7 +35,9 @@ class ContextKeyFilterProcessor implements ProcessorInterface {
  public function __invoke(LogRecord $record): LogRecord {
    foreach ($this->contextKeys as $key) {
      if (isset($record['context'][$key])) {
        unset($record['context'][$key]);
        $backup = $record->toArray();
        unset($backup['context'][$key]);
        $record = $record->with(context: $backup['context']);
      }
    }