Skip to content

(how to) handle control chars in keynames? #54

@spaceone

Description

@spaceone

keynames in extra, for example in dictionaries provided by user input, maybe contain invisible control chars in it's keynames.
They are printed as-is into the logfile, except for a newline being replaced with \n literally.

Example:

import logging

from logfmter import Logfmter

formatter = Logfmter(
    keys=["at", "foo"],
    mapping={"at": "levelname"},
)

handler = logging.StreamHandler()
handler.setFormatter(formatter)

logging.basicConfig(handlers=[handler])

all_chars = "\u0000\u0001\u0002\u0003\u0004\u0005\u0006\u0007\u0008\t\n\u000b\u000c\r\u000e\u000f\u0010\u0011\u0012\u0013\u0014\u0015\u0016\u0017\u0018\u0019\u001a\u001b\u001c\u001d\u001e\u001f !\"#$%&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\]^_`abcdefghijklmnopqrstuvwxyz{|}~\u007f"

extra = {"foo": {all_chars: "yep"}}

logging.error("hello", extra=extra)

Will result into the logline (added manuall displaying of the invisible control chars):

at=ERROR msg=hello foo.^@^A^B^C^D^E^F^G^H› \n^K^L^M^N^O^P^Q^R^S^T^U^V^W^X^Y^Z^[^\^]^^^__!"#$%&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\]^_abcdefghijklmnopqrstuvwxyz{|}~^?=yep`

printed in a terminal it looks like:

at=ERROR msg=hello foo. \n

My question is now: how should those be handled?
We could - identically to the value-formatter - replace them with \uXXXX representations.
Or they could be removed - that is what go-logfmter does. But it removes data.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions