python-json-logger icon indicating copy to clipboard operation
python-json-logger copied to clipboard

dictConfig does not work as expected, checked with 0.1.11

Open grihabor opened this issue 6 years ago • 3 comments

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

grihabor avatar Oct 22 '19 14:10 grihabor

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"}

xmo-odoo avatar Nov 20 '19 08:11 xmo-odoo

I tried with python 2.7.15

grihabor avatar Nov 20 '19 15:11 grihabor

AFAICT, that should have been

        "formatters": {
            "json": {
                "class": "pythonjsonlogger.jsonlogger.JsonFormatter",
                "json_kwargs": {
                    "format": "%(asctime)s.%(msecs)03d %(levelname)s %(message)s",
                }
            }
        },

danizen avatar Oct 23 '20 23:10 danizen