fluent / fluent/fluent-logger-python

_format_msg_json doesn't merge arguments

オープン
#170 コメント 2 件 リアクション 0 件 担当者 0 名 GitHub で見る
主要言語
Python
スター
457
フォーク
138
PR マージ指標
30日以内にマージされた PR はありません

説明

I'm using the `FluentHandler` class for Python logging module. I was hoping to pass a json string to the logger and have it interpreted as json, which it does, but the `FluentRecordFormatter` doesn't merge logged arguments in.

Example
```
logger = logging.getLogger('fluent.test')
logger.info('{"from": "%(a)s", "to": "%(b)s"}', {"a":"userA","b":"userB"})
```

Expected result
```
{"type":"INFO","from": "userA", "to": "userB"}
```

Actual result
```
{"type":"INFO","from":"%(a)s","to":"%(b)s"}
```

I expected the arguments to be merged in based on the behaviour of the [default Python formatter](https://github.com/python/cpython/blob/46abfc1416ff8e450999611ef8f231ff871ab133/Lib/logging/__init__.py#L664) which calls [`record.getMessage`](https://github.com/python/cpython/blob/46abfc1416ff8e450999611ef8f231ff871ab133/Lib/logging/__init__.py#L369) which merges in arguments

```
if self.args:
msg = msg % self.args
```

However, the behaviour of the `FluentRecordFormatter` is to [`json.loads(str(msg))`](https://github.com/fluent/fluent-logger-python/blob/7389fb6600f57b6eb21c854019df7d06a79fbb57/fluent/handler.py#L130) the message, and ignore any arguments.

Would it be suitable to merge in the arguments when loading the json? I'd be happy to contribute a PR for it. [Here's a test that would cover it.](https://github.com/fluent/fluent-logger-python/blob/7389fb6600f57b6eb21c854019df7d06a79fbb57/tests/test_handler.py)

```
def test_json_encoded_message_with_args(self):
handler = fluent.handler.FluentHandler('app.follow', port=self._port)

with handler:
logging.basicConfig(level=logging.INFO)
log = logging.getLogger('fluent.test')
handler.setFormatter(fluent.handler.FluentRecordFormatter())
log.addHandler(handler)

log.info('{"key": "%(a)s", "param": "value"}', {"a": "hello world!"})

log.removeHandler(handler)

data = self.get_data()
self.assertTrue('key' in data[0][2])
self.assertEqual('hello world!', data[0][2]['key'])
```

コントリビューションガイド

このリポジトリのコントリビューションガイドは索引されていません

調査の方向性

Start in fluent/handler.py at FluentRecordFormatter, especially the json.loads(str(msg)) path, and review tests/test_handler.py. Run the existing handler tests, then cover the reported logger call with arguments and confirm the emitted JSON contains the substituted values.

索引モデルが issue の本文から書いたものです。

評価

技術スタック
python
領域
devtools
issue の種類
バグ
難易度
2/5
見積もり時間
1〜3時間
活発さ
停滞
明瞭さ
明確に書かれている
初心者へのやさしさ
52/100

新しい issue をメールで受け取る

初心者向けの GitHub issue を短くまとめたダイジェスト。