Last active
April 3, 2020 05:15
-
-
Save lytex/0d2ec5fdb7be2292b402b395bef91ffe to your computer and use it in GitHub Desktop.
Log the current state of a long running program with custom format
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
import logging | |
from uuid import uuid4 | |
import numpy as np | |
from time import sleep | |
LEVEL = 'DEBUG' | |
FILENAME = '123.log' | |
FILEMODE = 'a' | |
FORMAT = '%(asctime)s.%(msecs)03d %(levelname)s:%(name)s:%(message)s' | |
run_uuid = uuid4() | |
run_string = '\n' + f' run {run_uuid} '.center(80, '*') + '\n' + ''.center(80, '*') \ | |
+ '\n' + ''.center(80, '*') + '\n' | |
logging.basicConfig(filename=FILENAME, level=LEVEL, filemode=FILEMODE, | |
datefmt='%Y-%m-%d %H:%M:%S', format=FORMAT) | |
logging.debug(run_string) | |
while(True): | |
# The values you are logging should be enough to rerun the program from here | |
value = np.random.randn(10*2).reshape((10, 2)) | |
# repr makes easy to copy/paste values into the terminal | |
logging.debug(f'\n{repr(value)}') | |
sleep(3) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment