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
15 from datetime import datetime
16 from fluent import sender
21 class FluentLogHandler(logging.Handler):
22 '''This is a minimalist log handler for use with Fluentd
24 Needs to be attached to a logger using the addHandler method.
25 It only picks up from every record:
26 - the formatted message (no timestamp and no level)
28 - the runlogdate (to tie multiple run-related logs together)
29 The timestamp is retrieved by the fluentd library.
32 def __init__(self, tag, fluentd_ip='127.0.0.1', fluentd_port=24224):
33 logging.Handler.__init__(self)
35 self.formatter = logging.Formatter('%(message)s')
36 self.sender = sender.FluentSender(self.tag, host=fluentd_ip, port=fluentd_port)
38 self.__warning_counter = 0
39 self.__error_counter = 0
41 def start_new_run(self):
42 '''Delimitate a new run in the stream of records with a new timestamp
44 self.runlogdate = str(datetime.now())
46 self.__warning_counter = 0
47 self.__error_counter = 0
49 self.__send_start_record()
51 def emit(self, record):
53 "loglevel": record.levelname,
54 "message": self.formatter.format(record),
55 "@timestamp": self.__get_timestamp()
57 # if runlogdate is 0, it's a log from server (not an nfvbench run) so do not send runlogdate
58 if self.runlogdate != 0:
59 data["runlogdate"] = self.runlogdate
61 self.__update_stats(record.levelno)
62 self.sender.emit(None, data)
64 # send START record for each run
65 def __send_start_record(self):
67 "runlogdate": self.runlogdate,
69 "message": "NFVBENCH run is started",
73 "@timestamp": self.__get_timestamp()
75 self.sender.emit(None, data)
77 # send stats related to the current run and reset state for a new run
78 def send_run_summary(self, run_summary_required):
79 if run_summary_required or self.__get_highest_level() == logging.ERROR:
81 "loglevel": "RUN_SUMMARY",
82 "message": self.__get_highest_level_desc(),
83 "numloglevel": self.__get_highest_level(),
84 "numerrors": self.__error_counter,
85 "numwarnings": self.__warning_counter,
86 "@timestamp": self.__get_timestamp()
88 # if runlogdate is 0, it's a log from server (not an nfvbench run)
89 # so don't send runlogdate
90 if self.runlogdate != 0:
91 data["runlogdate"] = self.runlogdate
92 self.sender.emit(None, data)
94 def __get_highest_level(self):
95 if self.__error_counter > 0:
97 elif self.__warning_counter > 0:
98 return logging.WARNING
101 def __get_highest_level_desc(self):
102 highest_level = self.__get_highest_level()
103 if highest_level == logging.INFO:
105 elif highest_level == logging.WARNING:
106 return "RUN WITH WARNINGS"
108 return "RUN WITH ERRORS"
110 def __update_stats(self, levelno):
111 if levelno == logging.WARNING:
112 self.__warning_counter += 1
113 elif levelno == logging.ERROR:
114 self.__error_counter += 1
116 def __get_timestamp(self):
117 return datetime.utcnow().replace(tzinfo=pytz.utc).strftime(
118 "%Y-%m-%dT%H:%M:%S.%f%z")