Last active
June 2, 2021 18:35
-
-
Save a-recknagel/4f7a46775c90f83cffa2a0ac0c6cd27b to your computer and use it in GitHub Desktop.
logging to stdout and file
This file contains hidden or 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
import logging.config | |
logging.config.dictConfig( | |
{ | |
"version": 1, | |
"disable_existing_loggers": False, | |
"formatters": { | |
"oneline": { | |
"format": "%(asctime)s [%(levelname)s] %(name)s: %(message)s" | |
}, | |
}, | |
"handlers": { | |
"console": { | |
"level": "WARNING", | |
"formatter": "oneline", | |
"class": "logging.StreamHandler", | |
"stream": "ext://sys.stdout", | |
}, | |
"file": { | |
"level": "DEBUG", | |
"formatter": "oneline", | |
"class": "logging.FileHandler", | |
"filename": "debug.log" | |
}, | |
}, | |
"loggers": { | |
"": { # for package maintainers, pass your package's name here instead of "" | |
"handlers": ["console", "file"], | |
"level": "DEBUG", | |
}, | |
}, | |
} | |
) | |
log = logging.getLogger(__name__) | |
log.info("Foo") # only to file | |
log.warning("Bar") # to stdout and file |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment