Last active
May 8, 2024 05:31
-
-
Save junetech/55087f6fa5ade9bef6d4bd86dcd9732d to your computer and use it in GitHub Desktop.
Python file boilerplate
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
LOG_FILENAME = "mylog.log" | |
LOG_ENCODING = "utf-8" | |
def main(): | |
print("Hello world!") | |
if __name__ == "__main__": | |
import datetime | |
import logging | |
import sys | |
root_logger = logging.getLogger() | |
root_logger.setLevel(logging.INFO) | |
handler = logging.FileHandler(LOG_FILENAME, encoding=LOG_ENCODING) | |
root_logger.addHandler(handler) | |
# show log messages on terminal as well | |
root_logger.addHandler(logging.StreamHandler(sys.stdout)) | |
start_d = datetime.datetime.now() | |
logging.info(f"{__name__} program start @ {start_d}"[:-3]) | |
main() | |
end_d = datetime.datetime.now() | |
elapsed_d = end_d - start_d | |
logging.info( | |
f"{__name__} program end @ {end_d}"[:-3] + f"; took total {elapsed_d}" | |
) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This boilerplate does: