Added custom log function; started tests
Some checks failed
continuous-integration/drone the build failed

This commit is contained in:
Jef Roosens 2021-04-26 22:34:12 +02:00
parent 8520b09c4e
commit 8929d743e9
Signed by: Jef Roosens
GPG key ID: B580B976584B5F30
3 changed files with 52 additions and 9 deletions

View file

@ -47,6 +47,34 @@ class Logger:
if not append:
self.log_file.unlink(missing_ok=True)
def custom(self, message: str, header: str = None):
"""Log a message given a header and a message.
If a header is provided (aka truthy), the final form of the messsage
wil be:
`[YYYY-MM-DD HH:MM:SS][header] message`
Otherwise, it's just:
`[YYYY-MM-DD HH:MM:SS] message`
Args:
message: the message to display
header: the header to add to the message
"""
timestamp = datetime.now().strftime("%Y-%m-%d %H:%M:%S")
log_message = f"[{timestamp}] {message}\n"
if header:
log_message = f"[{timestamp}][{header}] {message}\n"
if self.log_file:
self.log_file.write_text(log_message)
if self.stdout:
sys.stdout.write(log_message)
def log(self, level: int, message: str):
"""Log a message with a specific level.
@ -58,14 +86,7 @@ class Logger:
return
level_name = self.LOG_LEVELS[level - 1].upper()
timestamp = datetime.now().strftime("%Y-%m-%d %H:%M:%S")
log_message = f"[{timestamp}][{level_name}]{message}\n"
if self.log_file:
self.log_file.write_text(log_message)
if self.stdout:
sys.stdout.write(log_message)
self.custom(level_name, message)
def debug(self, message: str):
"""Log a debug message."""