Added docstrings.
This commit is contained in:
parent
adce0f3812
commit
8e1b4ade13
1 changed files with 32 additions and 1 deletions
|
@ -18,18 +18,39 @@ _msg_types = { # message types with colors
|
||||||
|
|
||||||
|
|
||||||
class Log:
|
class Log:
|
||||||
|
"""
|
||||||
|
Using this class, you can easily write to logfiles.
|
||||||
|
"""
|
||||||
|
|
||||||
def __init__(
|
def __init__(
|
||||||
self, log_path: str = None,
|
self,
|
||||||
|
log_path: str = None,
|
||||||
console_out: bool = True,
|
console_out: bool = True,
|
||||||
console_colored: bool = True,
|
console_colored: bool = True,
|
||||||
file_colored: bool = True
|
file_colored: bool = True
|
||||||
):
|
):
|
||||||
|
"""
|
||||||
|
:param log_path: The path to the log file. E.g. "~/cool_project/crashlog.txt"
|
||||||
|
:param console_out: Specifies whether the written messages should be also printed to the console.
|
||||||
|
:param console_colored: Specifies whether the messages should be printed in color. (e.g. error messages will be
|
||||||
|
colored red)
|
||||||
|
:param file_colored: Specifies whether the logfile should also contain color codes.
|
||||||
|
"""
|
||||||
|
|
||||||
self.log_path = log_path
|
self.log_path = log_path
|
||||||
self.console_out = console_out
|
self.console_out = console_out
|
||||||
self.console_colored = console_colored
|
self.console_colored = console_colored
|
||||||
self.file_colored = file_colored
|
self.file_colored = file_colored
|
||||||
|
|
||||||
def write(self, msg: str, msg_type: str = "info", prefix: str = ""):
|
def write(self, msg: str, msg_type: str = "info", prefix: str = ""):
|
||||||
|
"""
|
||||||
|
Writes a message to the logfile.
|
||||||
|
|
||||||
|
:param msg: The message to be written.
|
||||||
|
:param msg_type: The type of the message. (e.g. "info", "error")
|
||||||
|
:param prefix: An optional prefix that gets added at the beginning of the message.
|
||||||
|
"""
|
||||||
|
|
||||||
logfile_msg = self.format_msg(msg, msg_type, self.file_colored, prefix) + "\n"
|
logfile_msg = self.format_msg(msg, msg_type, self.file_colored, prefix) + "\n"
|
||||||
|
|
||||||
if self.console_out:
|
if self.console_out:
|
||||||
|
@ -55,6 +76,12 @@ class Log:
|
||||||
return formatted_msg
|
return formatted_msg
|
||||||
|
|
||||||
def read(self):
|
def read(self):
|
||||||
|
"""
|
||||||
|
Reads the content of the logfile.
|
||||||
|
|
||||||
|
:return: The content of the logfile as a string.
|
||||||
|
"""
|
||||||
|
|
||||||
if self.log_path is not None:
|
if self.log_path is not None:
|
||||||
log_file = open(self.log_path, "r")
|
log_file = open(self.log_path, "r")
|
||||||
log_content = log_file.read()
|
log_content = log_file.read()
|
||||||
|
@ -65,6 +92,10 @@ class Log:
|
||||||
raise FileNotFoundError("You didn't specify a log path at the log instance definition.")
|
raise FileNotFoundError("You didn't specify a log path at the log instance definition.")
|
||||||
|
|
||||||
def clear(self):
|
def clear(self):
|
||||||
|
"""
|
||||||
|
Completely clears the logfile.
|
||||||
|
"""
|
||||||
|
|
||||||
logfile = open(self.log_path, "w")
|
logfile = open(self.log_path, "w")
|
||||||
logfile.write("")
|
logfile.write("")
|
||||||
logfile.close()
|
logfile.close()
|
||||||
|
|
Loading…
Reference in a new issue