Skip to content

Instantly share code, notes, and snippets.

@sebastianknopf
Last active April 3, 2025 11:02
Show Gist options
  • Save sebastianknopf/677d9ae9f213389e1c3f9aeb0b5e4055 to your computer and use it in GitHub Desktop.
Save sebastianknopf/677d9ae9f213389e1c3f9aeb0b5e4055 to your computer and use it in GitHub Desktop.
datalog.py
import json
import os
from datetime import datetime
from lxml.etree import fromstring
from lxml.etree import tostring
class Datalog:
@classmethod
def cleanup(cls, directory: str, ttl_hours: int = 24) -> None:
if not os.path.exists(directory) or not os.path.isdir(directory):
os.makedirs(directory)
# look for old datalog files and remove them
# for speed up, check for the filename not beginning with today instead of ressource consuming difference calculation
today = datetime.now().strftime('%Y-%m-%d')
for datalog_file in os.listdir(directory):
# proceed only if the datalogfile is not from today
if not datalog_file.startswith(today):
datalog_timestamp = datalog_file.split('_')[0]
datalog_timestamp = datetime.datetime.strptime(datalog_timestamp, '%Y-%m-%d-%H.%M.%S-%f')
difference = (datetime.datetime.now() - datalog_timestamp).total_seconds()
if difference > 60 * 60 * ttl_hours:
datalog_file = os.path.join(directory, datalog_file)
os.remove(datalog_file)
@classmethod
def create(cls, directory: str, data: str, meta: dict, *args) -> None:
cls.cleanup(directory)
if not os.path.exists(directory) or not os.path.isdir(directory):
os.makedirs(directory)
# generate new datalog file
datalog_timestamp = datetime.now().strftime('%Y-%m-%d-%H.%M.%S-%f')
datalog_filename = f"{datalog_timestamp}_{'-'.join([str(a) for a in args])}.xml"
with open(os.path.join(directory, datalog_filename), 'wb') as datalog_file:
try:
xml = tostring(fromstring(data), pretty_print=True)
datalog_file.write(xml)
comment = "<!--\n"
comment += "This message is create by datalog module - See https://gist.github.com/sebastianknopf/677d9ae9f213389e1c3f9aeb0b5e4055\n"
comment += "\n"
comment += "Request Meta Data:\n"
comment += json.dumps(meta, indent=4) + "\n"
comment += "-->"
datalog_file.write(comment.encode('utf-8'))
except Exception:
pass
finally:
datalog_file.close()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment