23
// in my PHP code
$log = new Logger('LaurentCommand');
$log->pushHandler(new StreamHandler('./app/logs/LaurentCommand.log'));
$log->addInfo("Start command",array('username' => 'Joe', 'Age' => '28'));

结果日志文件 LaurentCommand.log :

[2012-12-20 10:28:11] LaurentCommand.INFO: 启动命令 {"username":"Joe","Age":"28"} []

为什么最后这个括号?

4

4 回答 4

54

那是额外的数据。LineFormatter的默认格式是"[%datetime%] %channel%.%level_name%: %message% %context% %extra%\n". 用户名/年龄是上下文,通常为空的额外内容会导致此空数组[]

如果您使用处理器将数据附加到日志记录,他们通常会将其写入额外的键以避免与上下文信息冲突。如果这对您来说确实是个问题,您可以更改默认格式并省略%extra%.

编辑:从 Monolog 1.11 开始, LineFormatter 在构造函数中有一个 $ignoreEmptyContextAndExtra 参数,可让您删除这些参数,因此您可以使用它:

// the last "true" here tells it to remove empty []'s
$formatter = new LineFormatter(null, null, false, true);
$handler->setFormatter($formatter);
于 2012-12-20T11:31:23.773 回答
9

老问题,但抛出另一个简单的选择:

$slackHandler = new \Monolog\Handler\SlackWebhookHandler(...);
$slackHandler->getFormatter()->ignoreEmptyContextAndExtra(true);
于 2017-02-04T12:09:53.113 回答
5

I know this is an old question, but I ran into it too and I want to share my solution.

The brackets at the end of log lines are due to how Monolog's LineFormatter tries to json_encode() the data in %extra%. The brackets are a JSON representation of an empty array.

To turn off those brackets, I ended up having to subclass Monolog\Formatter\LineFormatter with my own class and overwrite its convertToString($data) function so it returns an empty string if there's no data present. Here's my new subclass:

namespace My\Fancy\Monolog;
use Monolog\Formatter\LineFormatter;

class LineFormatter extends LineFormatter {

    protected function convertToString($data)
    {
        if (null === $data || is_scalar($data)) {
            return (string) $data;
        }

        // BEGIN CUSTOM CODE - This section added to prevent empty 
        // brackets from appearing at the end of log lines:
        if ((is_array($data) && !$data) 
            || is_object($data) && !get_object_vars($data)) {
            return '';
        }
        // END CUSTOM CODE 

        $data = $this->normalize($data);
        if (version_compare(PHP_VERSION, '5.4.0', '>=')) {
            return $this->toJson($data);
        }

        return str_replace('\\/', '/', json_encode($data));
    }
}

You can use this class by injecting an instance of it into your Monolog handler class, like so:

$handler = new Monolog\Handler\StreamHandler('/path/to/my/logfile', 'debug');
$handler->setFormatter(new My\Fancy\Monolog\LineFormatter());
$monolog->pushHandler($handler);

Enjoy!

于 2013-11-12T18:34:55.740 回答
2

Symfony 4 解决方案:

  1. 创建记录器:

    use Monolog\Formatter\LineFormatter;
    
    class Formatter extends LineFormatter
    {
        public function __construct(
            $format = null,
            $dateFormat = null,
            $allowInlineLineBreaks = false,
            $ignoreEmptyContextAndExtra = false
        ) {
            parent::__construct($format, $dateFormat, $allowInlineLineBreaks, true);
        }
    }
    
  2. 在中定义格式化程序services.yml

    log.custom.formatter:
      class: App\Formatter
    
  3. 为所需的环境定义格式化程序monolog.yml

    handlers:
      main:
        type: stream
        path: "%kernel.logs_dir%/%kernel.environment%.log"
        level: debug
        channels: ["!event"]
        formatter: log.custom.formatter
    
于 2019-09-03T10:56:22.433 回答