From 65a35d4a645800acf0055bdae8638658f23cb154 Mon Sep 17 00:00:00 2001 From: JHCD Date: Fri, 24 Jul 2015 14:39:32 +0200 Subject: [PATCH] change logging behaviour of NMAHandler #33 NMA applicationname is now configurable NMA event contains now module and function --- boswatch.py | 17 +++-- config/config.template.ini | 10 ++- includes/NMAHandler.py | 129 +++++++++++++++++++++---------------- 3 files changed, 92 insertions(+), 64 deletions(-) diff --git a/boswatch.py b/boswatch.py index 8009b34..a2cb7bb 100755 --- a/boswatch.py +++ b/boswatch.py @@ -249,13 +249,18 @@ try: logging.debug(" - NMAHandler:") for key,val in globals.config.items("NMAHandler"): logging.debug(" -- %s = %s", key, val) + # is NMAHandler enabled? 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) + # we only could do something, if an APIKey is given: + if len(globals.config.get("NMAHandler","APIKey")) > 0: + logging.debug("add NMA logging handler") + from includes import NMAHandler + if globals.config.get("NMAHandler","appName") == "": + nmaHandler = NMAHandler.NMAHandler(globals.config.get("NMAHandler","APIKey")) + else: + nmaHandler = NMAHandler.NMAHandler(globals.config.get("NMAHandler","APIKey"), globals.config.get("NMAHandler","appName")) + 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") diff --git a/config/config.template.ini b/config/config.template.ini index 53a0964..58f3f16 100644 --- a/config/config.template.ini +++ b/config/config.template.ini @@ -46,12 +46,18 @@ 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 for NMAHandler (see BOSWatch loglevel description) loglevel = 50 + +# logging record will send to APIKey APIKey = +# You could change the name of the application (default: BOSWatch) +# (f.e. if you use more than one instance of BOSWatch) +appName = BOSWatch + [FMS] # look-up-table for adding a description diff --git a/includes/NMAHandler.py b/includes/NMAHandler.py index ab62d5e..258a0d5 100644 --- a/includes/NMAHandler.py +++ b/includes/NMAHandler.py @@ -1,56 +1,73 @@ -#!/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 +#!/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 + message = record.message + + # if exist, add details as NMA event: + # record.module is the module- or filename + if (len(record.module) > 0): + event = "Module: " + record.module + # record.functionName is the name of the function + # will be "" if the message is not in a function + if len(record.funcName) > 0: + if not record.funcName == "": + event += " - " + record.funcName + "()" + else: + # we have to set an event-text, use self.event now + event = self.event + + # 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 + + # pynma.push(self, application="", event="", description="", url="", contenttype=None, priority=0, batch_mode=False, html=False) + self.nma.push(application=self.application, event=event, description=message, priority=priority) \ No newline at end of file