diff --git a/smbclientng/core/Logger.py b/smbclientng/core/Logger.py index 32008f1..71e75c3 100644 --- a/smbclientng/core/Logger.py +++ b/smbclientng/core/Logger.py @@ -50,6 +50,15 @@ def __init__(self, config, logfile=None): open(self.logfile, "w").close() def print(self, message=""): + """ + Prints a message to stdout and logs it to a file if logging is enabled. + + This method prints the provided message to the standard output and also logs it to a file if a log file path is specified during the Logger instance initialization. The message can include color codes for color-coded output, which can be disabled by setting the `nocolors` attribute to True. + + Args: + message (str): The message to be printed and logged. + """ + nocolor_message = re.sub(r"\x1b[\[]([0-9;]+)m", "", message) if self.config.no_colors: print(nocolor_message) @@ -58,6 +67,15 @@ def print(self, message=""): self.__write_to_logfile(nocolor_message) def info(self, message): + """ + Logs a message at the INFO level. + + This method logs the provided message at the INFO level. The message can include color codes for color-coded output, which can be disabled by setting the `nocolors` attribute to True. The message is also logged to a file if a log file path is specified during the Logger instance initialization. + + Args: + message (str): The message to be logged at the INFO level. + """ + nocolor_message = re.sub(r"\x1b[\[]([0-9;]+)m", "", message) if self.config.no_colors: print("[info] %s" % nocolor_message) @@ -66,6 +84,15 @@ def info(self, message): self.__write_to_logfile("[info] %s" % nocolor_message) def debug(self, message): + """ + Logs a message at the DEBUG level if debugging is enabled. + + This method logs the provided message at the DEBUG level if the `debug` attribute is set to True during the Logger instance initialization. The message can include color codes for color-coded output, which can be disabled by setting the `nocolors` attribute to True. + + Args: + message (str): The message to be logged. + """ + if self.config.debug == True: nocolor_message = re.sub(r"\x1b[\[]([0-9;]+)m", "", message) if self.config.no_colors: @@ -75,6 +102,15 @@ def debug(self, message): self.__write_to_logfile("[debug] %s" % nocolor_message) def error(self, message): + """ + Logs an error message to the console and the log file. + + This method logs the provided error message to the standard error output and also logs it to a file if a log file path is specified during the Logger instance initialization. The message can include color codes for color-coded output, which can be disabled by setting the `nocolors` attribute to True. + + Args: + message (str): The error message to be logged. + """ + nocolor_message = re.sub(r"\x1b[\[]([0-9;]+)m", "", message) if self.config.no_colors: print("[error] %s" % nocolor_message) @@ -83,6 +119,15 @@ def error(self, message): self.__write_to_logfile("[error] %s" % nocolor_message) def __write_to_logfile(self, message): + """ + Writes the provided message to the log file specified during Logger instance initialization. + + This method appends the provided message to the log file specified by the `logfile` attribute. If no log file path is specified, this method does nothing. + + Args: + message (str): The message to be written to the log file. + """ + if self.logfile is not None: f = open(self.logfile, "a") f.write(message + "\n")