_format_msg_json doesn't merge arguments

Aperta
#170 2 commenti 0 reazioni 0 assegnatari Vedi su GitHub

Nessuno ha ancora preso questa issue.

Valutazione

Difficoltà
2/5
Tempo stimato
1-3 ore
Idoneità per principianti
52/100
Tipo di issue
Bug
Chiarezza
Specificata chiaramente
Stato di attività
Ferma
Stack tecnologico
python
Ambito
devtools

Direzione di ricerca

Inizia in fluent/handler.py da FluentRecordFormatter, prestando particolare attenzione al percorso json.loads(str(msg)), e rivedi tests/test_handler.py. Esegui i test esistenti di handler, quindi copri la chiamata di logger segnalata con argomenti e conferma che il JSON emesso contenga i valori sostituiti.

Scritto dal modello di indicizzazione a partire dal testo della issue.

Descrizione

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 which calls record.getMessage which merges in arguments

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

However, the behaviour of the FluentRecordFormatter is to json.loads(str(msg)) 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.

    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'])
Lingua principale
Python
Stelle
457
Fork
138
Metriche di merge delle PR
Nessuna PR unita negli ultimi 30g

Guida per i contributori

Nessuna guida per i contributori indicizzata per questo repository

Come iniziare

  1. Leggi tutta la issue e poi la guida ai contributi del progetto.
  2. Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
  3. Fai un fork del repository e lavora su un branch.
  4. Apri una pull request che faccia riferimento al numero della issue.

Altre issue di fluent/fluent-logger-python

Tutte le issue di fluent/fluent-logger-python

Issue simili

Altre issue su Python

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.