Created
August 17, 2021 08:41
-
-
Save tomschr/1e6e4537598bc30102f6765abb936764 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# | |
# Example of a child loggers | |
# | |
import logging | |
from logging.config import dictConfig | |
LOGGING_CFG = { | |
"version": 1, | |
"disable_existing_loggers": "False", | |
"formatters": { | |
"console": { | |
'format': '[%(levelname)s] %(name)s: %(message)s', | |
}, | |
}, | |
"handlers": { | |
"console": { | |
"class": "logging.StreamHandler", | |
"formatter": "console", | |
}, | |
}, | |
"loggers": { | |
"crmsh": { | |
"handlers": [ | |
"console", | |
], | |
"level": "DEBUG", | |
}, | |
"crmsh.sbd": { | |
# Comment/uncomment to see the effect: | |
"level": "WARNING", | |
}, | |
"crmsh.bootstrap": { | |
# Comment/uncomment to see the effect: | |
"level": "ERROR", | |
}, | |
} | |
} | |
dictConfig(LOGGING_CFG) | |
def main(): | |
# This is our parent logger: | |
crmsh_log = logging.getLogger("crmsh") | |
# These are our child loggers with different log levels: | |
crmsh_sbd_log = logging.getLogger("crmsh.sbd") | |
crmsh_bs_log = logging.getLogger("crmsh.bootstrap") | |
crmsh_log.debug("A debug message") | |
crmsh_log.info("An info message") | |
crmsh_log.warning("A warning message") | |
crmsh_log.error("An error message") | |
# If you configured the dictionary accordingly, | |
# you don't need to set the logger level manually. | |
# In case you want, use this: | |
# crmsh_sbd_log.setLevel(logging.WARNING) | |
crmsh_sbd_log.debug("Another debug message in a child logger") | |
crmsh_sbd_log.warning("A warning message in a child logger") | |
crmsh_sbd_log.error("An error message in a child logger") | |
# Only some are shown, because of the configuration: | |
crmsh_bs_log.debug("An debug message from a logger with ERROR level") | |
crmsh_bs_log.warning("An warning message from a logger with ERROR level") | |
crmsh_bs_log.error("An error message from a logger with ERROR level") | |
crmsh_bs_log.fatal("An fatal message from a logger with ERROR level") | |
if __name__ == "__main__": | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment