Exemplo n.º 1
0
    def log_message(self, message="", args=None, message_type=MessageType.INFORMATION):
        super(FileLogger, self).log_message(message, args, message_type)
        # If the message level is greater that the current log level then do not log it.
        if super(FileLogger, self).should_message_be_logged(message_type):
            # Log the message
            try:
                writer = open(super().get_file_path(), "w")
                date_object = datetime.now()
                date_format = date_object.strftime(Logger.DEFAULT_DATE_FORMAT)
                date = date_format.format(date_object)

                # Set the style
                writer.write(self.get_text_with_color_flag(message_type))

                # Add the content
                writer.write(StringProcessor.safe_formatter("{}{}", [os.linesep, date]))
                writer.write(StringProcessor.safe_formatter("{}:\t", message_type.name()))
                writer.write(StringProcessor.safe_formatter(os.linesep + message, args))

                # Close off the style
                writer.write("</p>")

                # Close the pre tag when logging Errors
                if message_type is "ERROR":
                    writer.write("</pre>")
            except Exception as e:
                # Failed to write to the event log, write error to the console instead
                console_logger = ConsoleLogger()
                console_logger.log_message(MessageType.ERROR,
                                           StringProcessor.safe_formatter(self.LOG_ERROR_MESSAGE, e.args))
                console_logger.log_message(message_type, message, args)
Exemplo n.º 2
0
 def close(self):
     if os.path.exists(self.get_file_path()):
         try:
             writer = open(self.get_file_path(), "w")
             writer.write("</body></html>")
         except IOError as e:
             console_logger = ConsoleLogger()
             console_logger.log_message(MessageType.ERROR,
                                        StringProcessor.safe_formatter(self.LOG_ERROR_MESSAGE, e.getMessage()))
Exemplo n.º 3
0
    def __init__(self, log_folder="", append=False, message_level=MessageType.INFORMATION,
                 log_name=DEFAULT_LOG_NAME):
        super().__init__(log_folder, append, message_level, log_name)

        try:
            writer = open(super().get_file_path(), "w")
            writer.write(self.DEFAULT_HTML_HEADER)
        except IOError as e:
            console = ConsoleLogger()
            console.log_message(MessageType.ERROR,
                                StringProcessor.safe_formatter(self.LOG_ERROR_MESSAGE, e.args))
Exemplo n.º 4
0
    def make_valid_file_name(new_name):
        if new_name is None or new_name == "":
            raise FileExistsError("Blank or null file name was provided")

        # Replace invalid characters
        replaced_name = new_name
        try:
            replaced_name = new_name.replace("[^a-zA-Z0-9\\._\\- ]+", "~")
        except Exception as e:
            console = ConsoleLogger()
            console.log_message(
                MessageType.ERROR,
                StringProcessor.safe_formatter(
                    "Failed to Replace Invalid Characters because: {}",
                    e.args))
        return replaced_name
Exemplo n.º 5
0
    def __init__(self,
                 log_folder="",
                 append=False,
                 message_level=MessageType.INFORMATION,
                 log_name=DEFAULT_LOG_NAME):
        super().__init__(message_level)

        if log_folder is None or log_folder == "":
            self.directory = self.DEFAULT_LOG_FOLDER
        else:
            self.directory = log_folder

        if not os.path.exists(self.directory):
            os.makedirs(self.directory)

        name = self.make_valid_file_name(log_name)

        if not name.lower().endswith(self.extension()):
            name += self.extension()

        self.fileName = name
        self.filePath = self.directory
        self.messageType = message_level

        if path.exists(self.filePath) and not append:
            try:
                with open(self.filePath, "w") as writer:
                    # writer = open(self.filePath, "w")
                    writer.write(" ")
            except IOError as e:
                # Failed to write to the event log, write error to the console instead
                console = ConsoleLogger()
                console.log_message(
                    MessageType.ERROR,
                    StringProcessor.safe_formatter(
                        "Failed to write to event log because: {}",
                        e.strerror + self.filePath))
Exemplo n.º 6
0
 def log_message(self,
                 message="",
                 args=None,
                 message_type=MessageType.INFORMATION):
     # If the message level is greater that the current log level then do not log it.
     if self.should_message_be_logged(message_type):
         try:
             with open(self.filePath, "w") as writer:
                 writer.write(
                     StringProcessor.safe_formatter(
                         "{}{}",
                         [os.linesep,
                          int(round(time.time() * 1000))]))
                 writer.write(
                     StringProcessor.safe_formatter("{}:\t", message_type))
                 writer.write(StringProcessor.safe_formatter(message, args))
         except IOError as e:
             # Failed to write to the event log, write error to the console instead
             console = ConsoleLogger()
             console.log_message(
                 StringProcessor.safe_formatter(
                     "Failed to write to event log because: {}",
                     e.strerror), args, MessageType.ERROR)
             console.log_message(message, args, message_type)
Exemplo n.º 7
0
 def test_consoleLoggerLogMessage(self):
     console_logger = ConsoleLogger()
     console_logger.log_message("Test String {} {}", ["args1", "args2"])
     self.assertIsNotNone(console_logger)
Exemplo n.º 8
0
 def test_consoleLoggerLogMessageSelectType(self):
     console_logger = ConsoleLogger()
     console_logger.log_message("Test String {}", "args1",
                                MessageType.GENERIC)
     self.assertIsNotNone(console_logger)