Last active
September 12, 2023 11:56
-
-
Save roodee/b28a24565e8c94a9aa9ffa6950c88b2a to your computer and use it in GitHub Desktop.
Run procedure with appropriate logging and exception handling
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
from datetime import datetime | |
from os.path import abspath, dirname, join | |
from pathlib import Path | |
import logging | |
import logging.config | |
import some_module | |
if __name__ == '__main__': | |
# Load the directory to store logging files | |
logging_dir = "some_path" | |
# Make sure, logging directory exists | |
Path(logging_dir).mkdir(exist_ok=True) | |
# Create the logging file name | |
timestamp = datetime.now().strftime("%Y-%m-%d_%H-%M-%S") | |
logfilename = logging_dir + "__" + timestamp + ".log" | |
# Pass the logging file to logging configuration | |
logging_config_file = join(dirname(abspath(__file__)), "../config/logging.conf") | |
logging.config.fileConfig( | |
fname=logging_config_file, | |
defaults={"logfilename": logfilename} | |
) | |
logger = logging.getLogger("") | |
try: | |
logger.debug("Before running some_method()") | |
some_module.some_method() | |
logger.debug("After running some_method()") | |
except Exception as exc: | |
logger.error("Something went wrong!") | |
logger.error(exc, exc_info=True) | |
finally: | |
logger.info("Finished run.") | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment