Last active
August 23, 2022 00:06
-
-
Save mihow/5c435d935a48f7a2020ab3dd04c6b4b7 to your computer and use it in GitHub Desktop.
Example python logging configs
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
""" | |
This works when running code as script, module and with doctests. | |
Also sets the log level for imported packages like requests. | |
python -m mymodule.myfile | |
python mymodule/myfile.py | |
python -m unittest mymodule/myfile.py | |
""" | |
import logging | |
logging.basicConfig( | |
level=logging.DEBUG, | |
format="%(asctime)s [%(levelname)s] %(message)s", | |
handlers=[ | |
# logging.FileHandler("debug.log"), | |
logging.StreamHandler() | |
] | |
) | |
def do_stuff() | |
""" | |
>>> do_stuff() | |
True | |
""" | |
logging.info("Some info") | |
logging.debug("Some more detailed info") | |
return True | |
if __name__ == "__main__": | |
do_stuff() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment