-
Notifications
You must be signed in to change notification settings - Fork 11
Description
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.