Skip to content

Instantly share code, notes, and snippets.

@junetech
Last active May 8, 2024 05:31
Show Gist options
  • Save junetech/55087f6fa5ade9bef6d4bd86dcd9732d to your computer and use it in GitHub Desktop.
Save junetech/55087f6fa5ade9bef6d4bd86dcd9732d to your computer and use it in GitHub Desktop.
Python file boilerplate
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}"
)
@junetech
Copy link
Author

junetech commented May 8, 2024

This boilerplate does:

  • Create mylog.log file if necessary
  • Log time right before main function
  • Run main()
  • Log time right after main function

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment