【问题标题】:Drupal syslog odd behaviour with jsonDrupal syslog 与 json 的奇怪行为
【发布时间】:2020-09-17 16:40:40
【问题描述】:

我在理解我面临的问题时遇到了一些困难。我正在使用 Drupal 系统日志在文件上记录一些消息,文件上的结果与我 var_dump 消息不同。

这是一个例子:

$logMsg = 'LOGGING TEST : {"test": "test", "test1": {"test2": "test2", "test3":"test3"}}';

print_r($logMsg);

\Drupal::logger('TestLog')
   ->log(0,$logMsg);

print_r 的结果很好:LOGGING TEST : {"test": "test", "test1": {"test2": "test2", "test3":"test3"}}

但是文件中的结果并不好:LOGGING TEST : @"test": "test", "test1": {"test2": "test2", "test3":"test3"}

第一个大括号被@替换,最后一个被删除。

经过几个小时的测试,我找不到它为什么会这样,有人知道吗?

Drupal 的版本是 8.9.5 和 PHP 7.3.22。

谢谢

【问题讨论】:

    标签: json logging drupal-8 php-7.3


    【解决方案1】:

    这与 Drupal 记录器解析消息字符串的方式有关。

    \Drupal::logger('TestLog') 返回一个LoggerInterface,记录在https://api.drupal.org/api/drupal/vendor%21psr%21log%21Psr%21Log%21LoggerInterface.php/interface/LoggerInterface/9.1.x 中。 log() 方法记录在 https://api.drupal.org/api/drupal/vendor%21psr%21log%21Psr%21Log%21LoggerInterface.php/function/LoggerInterface%3A%3Alog/9.1.x 中。但答案实际上在LoggerInterface 的文档中,而不是log() 的文档中:

    消息可能包含以下形式的占位符:{foo},其中 foo 将被键“foo”中的上下文数据替换。

    因此,不要将 JSON 字符串嵌入$message,而是这样做:

    \Drupal::logger('TestLog')->log(
      \Psr\Log\LogLevel::ERROR,
      "LOGGING TEST : {json_data}",
      [
        'json_data' => '{"test": "test", "test1": {"test2": "test2", "test3":"test3"}}',
      ]
    );
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-08-01
      • 2014-12-25
      • 2012-01-19
      • 2021-10-30
      • 2022-01-20
      • 2018-12-11
      相关资源
      最近更新 更多