Commit 80412e95 authored by Luca Lusso's avatar Luca Lusso
Browse files

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

parent 5a93cdc4
Loading
Loading
Loading
Loading
+4 −0
Original line number Diff line number Diff line
@@ -178,6 +178,10 @@ 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
@@ -122,6 +122,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);
  }

}