Logging in Python Using Config Json file

175 Views Asked by At

I am trying to log from python using a json config file which controls the configuration of the logger , below is sample config I am using

{
    "version": 1,
    "disable_existing_loggers": true,
    "formatters": {
        "json": {
             "format": "%(asctime)s %(levelname)s %(filename)s %(lineno)s %(message)s"
        }
    },
    "handlers": {
        "json": {
            "class": "logging.StreamHandler",
            "formatter": "json"
        }
    },
    "loggers": {
        "": {
            "handlers": ["json"],
            "level": 20
        }
    }
}

This works fine but is there any Doc which would point to different formatters that are available to be used or similarly different handlers that can be used ?

1

There are 1 best solutions below

0
On

Handlers

The different handlers you can choose from are listed in the docs. Among the different handlers, there are:

  • StreamHandler that "sends logging output to streams such as sys.stdout, sys.stderr or any file-like object"
  • FileHandler that "sends logging output to a disk file"
  • SocketHandler that "sends logging output to a network socket"
  • SysLogHandler that "supports sending logging messages to a remote or local Unix syslog".
  • ...

Formatters

logging.Formatters can use any of the following LogRecord attributes inside the fmt string.

For example,

"%(asctime)s %(levelname)s %(name)s %(message)s"
# gives
2023-02-07 17:24:07,981 INFO foobar This is a message.
----
"%(name)s - %(levelname)s - %(pathname)s - %(filename)s - %(funcName)s - %(asctime)s - %(process)d - %(message)s"
# gives
loggername - INFO - default_formatter.py - default_formatter.py - my_func_name - 2023-02-07 17:25:02,734 - 380334 - This is an info message.