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.
33 def __init__(self, tag, fluentd_ip='127.0.0.1', fluentd_port=24224):
34 logging.Handler.__init__(self)
36 self.formatter = logging.Formatter('%(message)s')
37 self.sender = sender.FluentSender(self.tag, host=fluentd_ip, port=fluentd_port)
39 self.__warning_counter = 0
40 self.__error_counter = 0
42 def start_new_run(self):
43 '''Delimitate a new run in the stream of records with a new timestamp
45 self.runlogdate = self.__get_timestamp()
47 self.__warning_counter = 0
48 self.__error_counter = 0
50 self.__send_start_record()
52 def emit(self, record):
54 "loglevel": record.levelname,
55 "message": self.formatter.format(record),
56 "@timestamp": self.__get_timestamp()
58 # if runlogdate is 0, it's a log from server (not an nfvbench run) so do not send runlogdate
59 if self.runlogdate != 0:
60 data["runlogdate"] = self.runlogdate
62 self.__update_stats(record.levelno)
63 self.sender.emit(None, data)
65 # this function is called by summarizer
66 def record_send(self, record):
67 self.sender.emit(None, record)
69 # send START record for each run
70 def __send_start_record(self):
72 "runlogdate": self.runlogdate,
74 "message": "NFVBENCH run is started",
78 "@timestamp": self.__get_timestamp()
80 self.sender.emit(None, data)
82 # send stats related to the current run and reset state for a new run
83 def send_run_summary(self, run_summary_required):
84 if run_summary_required or self.__get_highest_level() == logging.ERROR:
86 "loglevel": "RUN_SUMMARY",
87 "message": self.__get_highest_level_desc(),
88 "numloglevel": self.__get_highest_level(),
89 "numerrors": self.__error_counter,
90 "numwarnings": self.__warning_counter,
91 "@timestamp": self.__get_timestamp()
93 # if runlogdate is 0, it's a log from server (not an nfvbench run)
94 # so don't send runlogdate
95 if self.runlogdate != 0:
96 data["runlogdate"] = self.runlogdate
97 self.sender.emit(None, data)
99 def __get_highest_level(self):
100 if self.__error_counter > 0:
102 elif self.__warning_counter > 0:
103 return logging.WARNING
106 def __get_highest_level_desc(self):
107 highest_level = self.__get_highest_level()
108 if highest_level == logging.INFO:
110 elif highest_level == logging.WARNING:
111 return "RUN WITH WARNINGS"
112 return "RUN WITH ERRORS"
114 def __update_stats(self, levelno):
115 if levelno == logging.WARNING:
116 self.__warning_counter += 1
117 elif levelno == logging.ERROR:
118 self.__error_counter += 1
120 def __get_timestamp(self):
121 return datetime.utcnow().replace(tzinfo=pytz.utc).strftime(
122 "%Y-%m-%dT%H:%M:%S.%f%z")