python: log crash (exception) to file
If you create app for windows you know that user do now like console running on background. But if your app crash you have problem: you d no see error message. Solution is to log it to file.
Here is snippet how to implement it:
import os
import getpass
import logging
# log exception to file => for frozen app
logging.basicConfig(
format="%(asctime)s %(levelname)-8s %(message)s",
datefmt="%Y-%m-%d %H:%M:%S",
filename=os.path.basename(sys.argv[0]) + ".log",
)
if hasattr(sys, "frozen"):
sys.excepthook = exception_hook
def exception_hook(exc_type, exc_value, exc_traceback):
"""Hook for standard exception hook."""
logging.error("Uncaught exception",
exc_info=(exc_type, exc_value, exc_traceback))
logging.info("User: {}, file: '{}'".format(getpass.getuser()))
def main():
# log information about user who run app
logging.getLogger().setLevel(logging.INFO)
logging.info("User: {}".format(getpass.getuser()))
Comments
Post a Comment