1 # Copyright 2017 Cisco Systems, Inc. All rights reserved.
3 # Licensed under the Apache License, Version 2.0 (the "License"); you may
4 # not use this file except in compliance with the License. You may obtain
5 # a copy of the License at
7 # http://www.apache.org/licenses/LICENSE-2.0
9 # Unless required by applicable law or agreed to in writing, software
10 # distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
11 # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
12 # License for the specific language governing permissions and limitations
17 from datetime import datetime
18 from fluent import sender
22 class FluentLogHandler(logging.Handler):
23 '''This is a minimalist log handler for use with Fluentd
25 Needs to be attached to a logger using the addHandler method.
26 It only picks up from every record:
27 - the formatted message (no timestamp and no level)
29 - the runlogdate (to tie multiple run-related logs together)
30 The timestamp is retrieved by the fluentd library.
31 There will be only one instance of FluentLogHandler running.
34 def __init__(self, fluentd_configs):
35 logging.Handler.__init__(self)
37 self.result_senders = []
39 self.formatter = logging.Formatter('%(message)s')
40 for fluentd_config in fluentd_configs:
41 if fluentd_config.logging_tag:
42 self.log_senders.append(
43 sender.FluentSender(fluentd_config.logging_tag, host=fluentd_config.ip,
44 port=fluentd_config.port))
45 if fluentd_config.result_tag:
46 self.result_senders.append(
47 sender.FluentSender(fluentd_config.result_tag, host=fluentd_config.ip,
48 port=fluentd_config.port))
49 self.__warning_counter = 0
50 self.__error_counter = 0
52 def start_new_run(self):
53 '''Delimitate a new run in the stream of records with a new timestamp
56 self.__warning_counter = 0
57 self.__error_counter = 0
58 self.runlogdate = self.__get_timestamp()
60 self.__send_start_record()
62 def emit(self, record):
64 "loglevel": record.levelname,
65 "message": self.formatter.format(record),
66 "@timestamp": self.__get_timestamp()
68 # if runlogdate is 0, it's a log from server (not an nfvbench run) so do not send runlogdate
69 if self.runlogdate != 0:
70 data["runlogdate"] = self.runlogdate
72 self.__update_stats(record.levelno)
73 for log_sender in self.log_senders:
74 log_sender.emit(None, data)
76 # this function is called by summarizer, and used for sending results
77 def record_send(self, record):
78 for result_sender in self.result_senders:
79 result_sender.emit(None, record)
81 # send START log record for each run
82 def __send_start_record(self):
84 "runlogdate": self.runlogdate,
86 "message": "NFVBENCH run is started",
90 "@timestamp": self.__get_timestamp()
92 for log_sender in self.log_senders:
93 log_sender.emit(None, data)
95 # send stats related to the current run and reset state for a new run
96 def send_run_summary(self, run_summary_required):
97 if run_summary_required or self.__get_highest_level() == logging.ERROR:
99 "loglevel": "RUN_SUMMARY",
100 "message": self.__get_highest_level_desc(),
101 "numloglevel": self.__get_highest_level(),
102 "numerrors": self.__error_counter,
103 "numwarnings": self.__warning_counter,
104 "@timestamp": self.__get_timestamp()
106 # if runlogdate is 0, it's a log from server (not an nfvbench run)
107 # so don't send runlogdate
108 if self.runlogdate != 0:
109 data["runlogdate"] = self.runlogdate
110 for log_sender in self.log_senders:
111 log_sender.emit(None, data)
113 def __get_highest_level(self):
114 if self.__error_counter > 0:
116 elif self.__warning_counter > 0:
117 return logging.WARNING
120 def __get_highest_level_desc(self):
121 highest_level = self.__get_highest_level()
122 if highest_level == logging.INFO:
124 elif highest_level == logging.WARNING:
125 return "RUN WITH WARNINGS"
126 return "RUN WITH ERRORS"
128 def __update_stats(self, levelno):
129 if levelno == logging.WARNING:
130 self.__warning_counter += 1
131 elif levelno == logging.ERROR:
132 self.__error_counter += 1
134 def __get_timestamp(self):
135 return datetime.utcnow().replace(tzinfo=pytz.utc).strftime(
136 "%Y-%m-%dT%H:%M:%S.%f%z")