diff --git a/boswatch.py b/boswatch.py index 7167b4c..8009b34 100755 --- a/boswatch.py +++ b/boswatch.py @@ -68,6 +68,7 @@ try: # initialization: rtl_fm = None multimon_ng = None + nmaHandler = None try: # @@ -223,11 +224,11 @@ try: logging.debug("cannot read config file", exc_info=True) exit(1) - # initialization was fine, continue with main program... + + # + # Set the loglevel and backupCount of the file handler + # try: - # - # Set the loglevel and backupCount of the file handler - # logging.debug("set loglevel of fileHandler to: %s",globals.config.getint("BOSWatch","loglevel")) fh.setLevel(globals.config.getint("BOSWatch","loglevel")) logging.debug("set backupCount of fileHandler to: %s", globals.config.getint("BOSWatch","backupCount")) @@ -238,6 +239,32 @@ try: logging.debug("cannot set loglevel of fileHandler", exc_info=True) pass + + # + # Add NMA logging handler + # + try: + if globals.config.has_section("NMAHandler"): + if globals.config.getint("BOSWatch","loglevel") == 10: + logging.debug(" - NMAHandler:") + for key,val in globals.config.items("NMAHandler"): + logging.debug(" -- %s = %s", key, val) + if globals.config.getboolean("NMAHandler", "enableHandler") == True: + logging.debug("add NMA logging handler") + from includes import NMAHandler + logging.handlers.NMAHandler = NMAHandler.NMAHandler + nmaHandler = logging.handlers.NMAHandler(globals.config.get("NMAHandler","APIKey")) + nmaHandler.setLevel(globals.config.getint("NMAHandler","loglevel")) + myLogger.addHandler(nmaHandler) + except: + # It's an error, but we could work without that stuff... + logging.error("cannot add NMA logging handler") + logging.debug("cannot add NMA logging handler", exc_info=True) + pass + + + # initialization was fine, continue with main program... + # # Load plugins # @@ -383,5 +410,7 @@ finally: logging.debug("close Logging") logging.info("BOSWatch exit()") logging.shutdown() + if nmaHandler: + nmaHandler.close() fh.close() - ch.close() + ch.close() \ No newline at end of file diff --git a/config/config.template.ini b/config/config.template.ini index f82235e..53a0964 100644 --- a/config/config.template.ini +++ b/config/config.template.ini @@ -42,6 +42,17 @@ doubleFilter_ignore_time = 5 # you will get more then one alarm anyway if the msg is different (receiving-problems) doubleFilter_check_msg = 0 + +[NMAHandler] +# you could use an logging handler for sending logging records to NotifyMyAndroid +# enableHandler (0|1) will enable the NMA handler +# loglevel for NMAHandler (see BOSWatch loglevel description) +# logging record will send to APIKey +enableHandler = 0 +loglevel = 50 +APIKey = + + [FMS] # look-up-table for adding a description # Using Description (0|1) diff --git a/includes/NMAHandler.py b/includes/NMAHandler.py new file mode 100644 index 0000000..ab62d5e --- /dev/null +++ b/includes/NMAHandler.py @@ -0,0 +1,56 @@ +#!/usr/bin/python +# -*- coding: UTF-8 -*- + +""" +Logging Handler for NotifyMyAndroid + +@author: Jens Herrmann +""" + +import logging +from includes.pynma import pynma + +class NMAHandler(logging.Handler): # Inherit from logging.Handler + """ + Handler instances dispatch logging events to NotifyMyAndroid. + """ + + def __init__(self, APIKey, application="BOSWatch", event="Logging-Handler"): + """ + Initializes the handler with NMA-specific parameters. + + @param APIKey: might be a string containing 1 key or an array of keys + @param application: application name [256] + @param event: event name [1000] + """ + # run the regular Handler __init__ + logging.Handler.__init__(self) + # Our custom argument + self.APIKey = APIKey + self.application = application + self.event = event + self.nma = pynma.PyNMA(self.APIKey) + + + def emit(self, record): + """ + Send logging record via NMA + """ + # record.message is the log message + # record.levelno is the log level + # loglevel: 10 = debug => priority: -2 + # loglevel: 20 = info => priority: -1 + # loglevel: 30 = warning => priority: 0 + # loglevel: 40 = error => priority: 1 + # loglevel: 50 = critical => priority: 2 + if record.levelno >= 50: + priority = 2 + elif record.levelno >= 40: + priority = 1 + elif record.levelno >= 30: + priority = 0 + elif record.levelno >= 20: + priority = -1 + else: + priority = -2 + self.nma.push(self.application, self.event, record.message, priority=priority) \ No newline at end of file