Skip to content

Commit

Permalink
fix double lines in log file
Browse files Browse the repository at this point in the history
add flush_memory_handler() function to class Loggers
bugfix 0d3fefc
  • Loading branch information
LegenJCdary authored and cinek810 committed Sep 23, 2022
1 parent d433ddd commit f492c6a
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 8 deletions.
10 changes: 2 additions & 8 deletions source/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -116,15 +116,9 @@ def main():
if options["subcommand"] in ("run", "verify"):
workdir = misc.create_workdir(start_ts, configuration.conf, logger.logger)
Loggers.set_logging_to_file(logger, workdir, start_ts, configuration.conf)
if options["syslog"]:
# handler[1] - MemoryHandler, handler[3] - FileHandler
logger.logger.handlers[1].setTarget(logger.logger.handlers[3])
else:
# handler[0] - MemoryHandler, handler[2] - FileHandler
logger.logger.handlers[0].setTarget(logger.logger.handlers[2])
logger.logger.handlers[0].flush()
logger.flush_memory_handler(True, options["syslog"])
else:
logger.logger.handlers[0].close()
logger.flush_memory_handler(False, options["syslog"])

validators = Validators(logger.logger)
validators.validate_options(options)
Expand Down
14 changes: 14 additions & 0 deletions source/modules/outputs/logging.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,20 @@ def set_logging_to_file(self, log_dir: str, timestamp: str, conf: dict):
file_handler.setLevel(logging.DEBUG)
self.logger.addHandler(file_handler)

def flush_memory_handler(self, subcommand_flag: bool, syslog: bool):
"""Flush initial log messages from memory handler to logfile"""
if syslog:
# handler[1] - MemoryHandler, handler[3] - FileHandler
if subcommand_flag:
self.logger.handlers[1].setTarget(self.logger.handlers[3])
self.logger.handlers[1].flush()
self.logger.handlers[1].close()
else:
# handler[0] - MemoryHandler, handler[2] - FileHandler
if subcommand_flag:
self.logger.handlers[0].setTarget(self.logger.handlers[2])
self.logger.handlers[0].flush()
self.logger.handlers[0].close()

class CustomFormatter(logging.Formatter):
"""Class adding colours to console logger"""
Expand Down

0 comments on commit f492c6a

Please sign in to comment.