python-json-logger
python-json-logger copied to clipboard
dictConfig does not work as expected, checked with 0.1.11
I'm trying to make jsonlogger wotk with dictconfig. I tried this:
def setup_logging(loglevel, path):
cfg = {
"version": 1,
"disable_existing_loggers": True,
"formatters": {
"json": {
"class": "pythonjsonlogger.jsonlogger.JsonFormatter",
"format": "%(asctime)s.%(msecs)03d %(levelname)s %(message)s",
}
},
"handlers": {
"file": {
"class": "logging.FileHandler",
"filename": path,
"mode": "a",
"formatter": "json",
}
},
"root": {"level": getattr(logging, loglevel), "handlers": ["file"]},
}
logging.config.dictConfig(cfg)
setup_logging("INFO", "log")
logging.warning("bla")
And I get this in file:
2019-10-22 17:48:52,224.224 WARNING bla
Seems to be working fine for me, running on Python 3.6.9. Just dropped your script in test.py and:
$ python test.py && cat log
{"asctime": "2019-11-20 09:36:11,549", "msecs": 549.6327877044678, "levelname": "WARNING", "message": "bla"}
$ python test.py && cat log
{"asctime": "2019-11-20 09:36:11,549", "msecs": 549.6327877044678, "levelname": "WARNING", "message": "bla"}
{"asctime": "2019-11-20 09:36:13,162", "msecs": 162.89401054382324, "levelname": "WARNING", "message": "bla"}
I tried with python 2.7.15
AFAICT, that should have been
"formatters": {
"json": {
"class": "pythonjsonlogger.jsonlogger.JsonFormatter",
"json_kwargs": {
"format": "%(asctime)s.%(msecs)03d %(levelname)s %(message)s",
}
}
},