Skip to content

Instantly share code, notes, and snippets.

@lytex
Last active April 3, 2020 05:15
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save lytex/0d2ec5fdb7be2292b402b395bef91ffe to your computer and use it in GitHub Desktop.
Save lytex/0d2ec5fdb7be2292b402b395bef91ffe to your computer and use it in GitHub Desktop.
Log the current state of a long running program with custom format
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