1 # -*- coding: utf-8 -*-
2 # Description: logging for netdata python.d modules
5 from time import time, strftime
10 LOG_THROTTLE = 10000 # has to be too big during init
11 LOG_INTERVAL = 1 # has to be too low during init
14 WRITE = sys.stderr.write
15 FLUSH = sys.stderr.flush
18 def log_msg(msg_type, *args):
20 Print message on stderr.
32 # WRITE("COUNTER " + str(LOG_COUNTER) + " THROTTLE " + str(LOG_THROTTLE) + " INTERVAL " + str(LOG_INTERVAL) + " NOW " + str(now) + " NEXT " + str(LOG_NEXT_CHECK) + "\n")
34 if LOG_COUNTER <= LOG_THROTTLE or msg_type == "FATAL" or msg_type == "ALERT":
35 timestamp = strftime('%Y-%m-%d %X')
36 msg = "%s: %s %s: %s" % (timestamp, PROGRAM, str(msg_type), " ".join(args))
39 elif LOG_COUNTER == LOG_THROTTLE + 1:
40 timestamp = strftime('%Y-%m-%d %X')
41 msg = "%s: python.d.plugin: throttling further log messages for %s seconds" % (timestamp, str(int(LOG_NEXT_CHECK + 0.5) - int(now)))
45 if LOG_NEXT_CHECK <= now:
46 if LOG_COUNTER >= LOG_THROTTLE:
47 timestamp = strftime('%Y-%m-%d %X')
48 msg = "%s: python.d.plugin: Prevented %s log messages from displaying" % (timestamp, str(LOG_COUNTER - LOG_THROTTLE))
51 LOG_NEXT_CHECK = now - (now % LOG_INTERVAL) + LOG_INTERVAL
58 Print debug message on stderr.
63 log_msg("DEBUG", *args)
68 Print message on stderr.
70 log_msg("ERROR", *args)
75 Print message on stderr.
77 log_msg("ALERT", *args)
82 Print message on stderr.
84 log_msg("INFO", *args)
89 Print message on stderr and exit.
91 log_msg("FATAL", *args)
92 # using sys.stdout causes IOError: Broken Pipe
94 # sys.stdout.write('DISABLE\n')