Improved log and log test.

This commit is contained in:
The Wobbler 2025-01-13 18:00:54 +01:00
parent 2655e2c65e
commit 7d38dab998
3 changed files with 51 additions and 20 deletions

1
.gitignore vendored
View file

@ -1,3 +1,4 @@
__pycache__
build
wobbl_tools.egg-info
test_log.txt

View file

@ -1,17 +1,30 @@
#!/usr/bin/python3
import pytest
from wobbl_tools.text.log import Log
print("\n==== wobbl_tools.text.log =====")
test_log = Log("test_log.txt")
test_log.clear()
test_log.write("Testing the log module...")
test_log.write("This should be fine...", "ok")
test_log.write("Something happened...", "warning")
test_log.write("Oh shit!", "error")
print("Logfile contents:")
print(test_log.read())
expected_content = \
"[info]: Testing the log module...\n" \
"[OK]: This should be fine...\n" \
"[WARNING]: Something happened...\n" \
"[ERROR]: Oh shit!\n"
assert test_log.read() == expected_content
without_file = Log()
with pytest.raises(FileNotFoundError, match="You didn't specify a log path at the log instance definition."):
without_file.read()

View file

@ -2,14 +2,14 @@
from .format import format_string
msg_types_ncl = { # message types without colors
_msg_types_ncl = { # message types without colors
"info": "[info]: ",
"ok": "[OK]: ",
"warning": "[WARNING]: ",
"error": "[ERROR]: "
}
msg_types = {
_msg_types = { # message types with colors
"info": "[info]: ",
"ok": format_string("[§dark_green§boldOK§rs]: "),
"warning": format_string("§yellow§bold[WARNING]: ", auto_rs=False),
@ -18,29 +18,41 @@ msg_types = {
class Log:
def __init__(self, log_path: str = None, no_console: bool = False, no_colors: bool = False):
def __init__(
self, log_path: str = None,
console_out: bool = True,
console_colored: bool = True,
file_colored: bool = True
):
self.log_path = log_path
self.no_console = no_console
self.no_colors = no_colors
self.log = ""
self.console_out = console_out
self.console_colored = console_colored
self.file_colored = file_colored
def write(self, msg: str, msg_type: str = "info", prefix: str = ""):
if self.no_colors:
msg = prefix + msg_types_ncl[msg_type] + msg
logfile_msg = self.format_msg(msg, msg_type, self.file_colored, prefix) + "\n"
else:
msg = prefix + msg_types[msg_type] + msg
msg += format_string("§rs")
if self.console_out:
console_msg = self.format_msg(msg, msg_type, self.console_colored, prefix)
if not self.no_console:
print(msg)
print(console_msg)
if self.log_path is not None:
log_file = open(self.log_path, "a")
log_file.write(msg + "\n")
log_file.close()
logfile = open(self.log_path, "a")
logfile.write(logfile_msg)
logfile.close()
self.log += msg + "\n"
def format_msg(self, msg, msg_type: str="info", colored: bool=True, prefix: str=""):
if colored:
formatted_msg = _msg_types[msg_type] + msg
formatted_msg += format_string("§rs")
else:
formatted_msg = _msg_types_ncl[msg_type] + msg
formatted_msg = prefix + formatted_msg
return formatted_msg
def read(self):
if self.log_path is not None:
@ -50,4 +62,9 @@ class Log:
return log_content
else:
return self.log
raise FileNotFoundError("You didn't specify a log path at the log instance definition.")
def clear(self):
logfile = open(self.log_path, "w")
logfile.write("")
logfile.close()