1 # -*- coding: utf-8 -*-
2 # Description: logging for netdata python.d modules
6 from time import time, strftime
12 LOG_THROTTLE = 10000 # has to be too big during init
13 LOG_INTERVAL = 1 # has to be too low during init
16 WRITE = sys.stderr.write
17 FLUSH = sys.stderr.flush
20 def log_msg(msg_type, *args):
22 Print message on stderr.
34 # WRITE("COUNTER " + str(LOG_COUNTER) + " THROTTLE " + str(LOG_THROTTLE) + " INTERVAL " + str(LOG_INTERVAL) + " NOW " + str(now) + " NEXT " + str(LOG_NEXT_CHECK) + "\n")
36 if LOG_COUNTER <= LOG_THROTTLE or msg_type == "FATAL" or msg_type == "ALERT":
37 timestamp = strftime('%Y-%m-%d %X')
38 msg = "%s: %s %s: %s" % (timestamp, PROGRAM, str(msg_type), " ".join(args))
41 elif LOG_COUNTER == LOG_THROTTLE + 1:
42 timestamp = strftime('%Y-%m-%d %X')
43 msg = "%s: python.d.plugin: throttling further log messages for %s seconds" % (timestamp, str(int(LOG_NEXT_CHECK + 0.5) - int(now)))
47 if LOG_NEXT_CHECK <= now:
48 if LOG_COUNTER >= LOG_THROTTLE:
49 timestamp = strftime('%Y-%m-%d %X')
50 msg = "%s: python.d.plugin: Prevented %s log messages from displaying" % (timestamp, str(LOG_COUNTER - LOG_THROTTLE))
53 LOG_NEXT_CHECK = now - (now % LOG_INTERVAL) + LOG_INTERVAL
57 if msg_type == "FATAL" or msg_type == "ERROR" or msg_type == "ALERT":
63 Print debug message on stderr.
68 log_msg("DEBUG", *args)
73 Print message on stderr.
75 log_msg("ERROR", *args)
80 Print message on stderr.
82 log_msg("ALERT", *args)
87 Print message on stderr.
89 log_msg("INFO", *args)
94 Print message on stderr and exit.
97 log_msg("FATAL", *args)