Skip to content

Instantly share code, notes, and snippets.

@mihow
Last active August 23, 2022 00:06
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mihow/5c435d935a48f7a2020ab3dd04c6b4b7 to your computer and use it in GitHub Desktop.
Save mihow/5c435d935a48f7a2020ab3dd04c6b4b7 to your computer and use it in GitHub Desktop.
Example python logging configs
"""
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