Skip to content

Instantly share code, notes, and snippets.

@attomos
Created November 28, 2020 06:25
Show Gist options
  • Save attomos/b1ab348014fcc18a7f8bf3db60afdc82 to your computer and use it in GitHub Desktop.
Save attomos/b1ab348014fcc18a7f8bf3db60afdc82 to your computer and use it in GitHub Desktop.
Why assert failure does not show in logs? Checkout before() and after() 😉
import logging
import traceback
from datetime import datetime
def setup_logging():
# logging.getLogger("requests").setLevel(logging.WARNING)
# logging.getLogger("urllib3").setLevel(logging.WARNING)
# logging.getLogger("paramiko").setLevel(logging.WARNING)
today = datetime.today().strftime("%Y.%m.%d")
logging.basicConfig(
level=logging.INFO,
format="%(asctime)s [%(levelname)5s] %(message)s",
handlers=[logging.FileHandler(f"main_{today}.log"), logging.StreamHandler(),],
)
def main():
logging.info("hello")
assert 1 == 2, "number of images should match"
def before():
setup_logging()
main()
def after():
try:
setup_logging()
main()
except Exception as e:
logging.error(e)
exc = traceback.format_exc()
logging.error(exc)
# traceback.print_exc()
if __name__ == "__main__":
after()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment