Commit 527fa4bb authored by JOSSOUD Olivier's avatar JOSSOUD Olivier
Browse files

Debug Logger.

parent 5612cdb0
import sys
import configobj
import datetime
import os
class Logger:
def __init__(self, config_parser: configobj.ConfigObj):
self.config = config_parser
self.log_dir = self.config["LOGGER"]["log_dir"]
self.__create_log_dir__()
self.__delete_old_files__()
def write(self, instrument: str, message: str) -> None:
now = datetime.datetime.now(tz=datetime.timezone.utc)
now_str = now.strftime("%Y-%m-%d %H:%M:%S")
msg = "[" + now_str + "]"
if instrument != "":
msg += "[" + instrument + "]"
msg += " " + message + "\n"
sys.stdout.write(msg)
with open(self.__get_current_file_path__(now), "a") as log_file:
log_file.write(msg)
def __get_current_file_path__(self, now: datetime.datetime) -> str:
file_name = now.strftime("%Y-%m-%d") + ".log"
file_path = os.path.join(self.log_dir, file_name)
return file_path
def __delete_old_files__(self) -> None:
log_files = os.listdir(self.log_dir)
for log_file in log_files:
full_file_path = os.path.join(self.log_dir, log_file)
file_stats = os.stat(full_file_path)
modification_time = datetime.datetime.fromtimestamp(file_stats.st_mtime)
if (datetime.datetime.now() - modification_time).days > int(self.config["LOGGER"]["keep_log_days"]):
os.remove(full_file_path)
def __create_log_dir__(self) -> None:
if not os.path.exists(self.log_dir):
os.mkdir(self.log_dir)
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment