Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix double lines in log file #158

Merged
merged 1 commit into from
Sep 23, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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