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

pull/15/head
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

@ -1,5 +1,4 @@
pipeline:
# TODO add config for all other versions
test-3.9:
group: test
image: python:3.9-alpine

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."""

View File

@ -0,0 +1,23 @@
"""Tests for the logger module."""
from app.logger import Logger
from datetime import datetime
def test_custom_stdout(capfd):
"""Test the custom command."""
logger = Logger()
timestamp = datetime.now().strftime("%Y-%m-%d %H:%M:%S")
logger.custom("a message", header="cewl")
out, _ = capfd.readouterr()
assert out == f"[{timestamp}][cewl] a message\n"
def test_log_stdout(capfd):
"""Test the log command with several levels."""
logger = Logger()
# TODO