From f4ac478394d8f49a44d9c795490f039ea400f6b9 Mon Sep 17 00:00:00 2001 From: Andreas Maier Date: Mon, 25 Nov 2019 12:46:50 +0100 Subject: [PATCH] Fixed bug where all output was for the same record Signed-off-by: Andreas Maier --- zhmc_log_forwarder/zhmc_log_forwarder.py | 45 ++++++++++++++---------- 1 file changed, 27 insertions(+), 18 deletions(-) diff --git a/zhmc_log_forwarder/zhmc_log_forwarder.py b/zhmc_log_forwarder/zhmc_log_forwarder.py index 37450e2..ffda50d 100755 --- a/zhmc_log_forwarder/zhmc_log_forwarder.py +++ b/zhmc_log_forwarder/zhmc_log_forwarder.py @@ -982,6 +982,32 @@ def output_entries(self, log_entries): table.append(row) sorted_table = sorted(table, key=lambda row: row.time) + + dest = self.fwd_parms['dest'] + if dest in ('stdout', 'stderr'): + dest_stream = getattr(sys, dest) + for row in sorted_table: + out_str = self.out_str(row) + print(out_str, file=dest_stream) + dest_stream.flush() + else: + assert dest == 'syslog' + for row in sorted_table: + out_str = self.out_str(row) + try: + self.logger.info(out_str) + except Exception as exc: + raise ConnectionError( + "Cannot write log entry to syslog server at " + "{host}, port {port}/{porttype}: {msg}". + format(host=self.syslog_host, port=self.syslog_port, + porttype=self.syslog_porttype, msg=str(exc))) + + def out_str(self, row): + """ + Return an output string for the specified row that fits the specified + output format. + """ format = self.fwd_parms['format'] if format == 'line': line_format = self.fwd_parms['line_format'] @@ -1018,24 +1044,7 @@ def output_entries(self, log_entries): } } out_str = json.dumps(out_dict, indent=CADF_JSON_INDENT) - - dest = self.fwd_parms['dest'] - if dest in ('stdout', 'stderr'): - dest_stream = getattr(sys, dest) - for row in sorted_table: - print(out_str, file=dest_stream) - dest_stream.flush() - else: - assert dest == 'syslog' - for row in sorted_table: - try: - self.logger.info(out_str) - except Exception as exc: - raise ConnectionError( - "Cannot write log entry to syslog server at " - "{host}, port {port}/{porttype}: {msg}". - format(host=self.syslog_host, port=self.syslog_port, - porttype=self.syslog_porttype, msg=str(exc))) + return out_str class DatetimeFormatter(logging.Formatter):