simplified logging. creates logging stuff in linspector now. no Logger class needed anymore. more advanced output in log message like filename,funcname,linenumber

This commit is contained in:
Johannes Findeisen 2013-09-25 03:08:24 +02:00
commit 530fd2a2ba
4 changed files with 51 additions and 22 deletions

View file

@ -71,7 +71,7 @@ class ConfigParser:
with open(configFilename) as cfgFile:
config = cfgFile.read()
self.log.i("reading Config: " + configFilename)
self.log.info("reading Config: " + configFilename)
return json.loads(config)
def _create_raw_Object(self, jsonDict, msgName, creator):
@ -90,8 +90,8 @@ class ConfigParser:
item = creator(key, val)
items.append(item)
except Exception:
self.log.w("ignoring " + msgName + ": " + key + "! reason:")
self.log.w(str(Exception))
self.log.warning("ignoring " + msgName + ": " + key + "! reason:")
self.log.warning(str(Exception))
return items
def _load_module(self, clazz, modPart):
@ -135,15 +135,15 @@ class ConfigParser:
if class_check(item):
repl.append(item)
else:
self.log.w("Ignoring class " + clazzItem["class"] + "! It does not pass the class check!")
self.log.warning("Ignoring class " + clazzItem["class"] + "! It does not pass the class check!")
except ImportError, err:
self.log.w("Could not import " + clazz + ": " + str(clazzItem) + "! reason")
self.log.w(str(err))
self.log.warning("Could not import " + clazz + ": " + str(clazzItem) + "! reason")
self.log.warning(str(err))
except KeyError, k:
self.log.w("Key " + str(k) + " not in classItem " + str(clazzItem))
self.log.warning("Key " + str(k) + " not in classItem " + str(clazzItem))
except Exception, e:
self.log.w("Error while replacing class ( " + clazz + " ): " + str(e))
self.log.warning("Error while replacing class ( " + clazz + " ): " + str(e))
del items[:]
items.extend(repl)

View file

@ -27,12 +27,12 @@ class Command:
try:
self.commandStart = dt.now()
self.log.i("calling command " + str(self.command) + " at " + str(self.commandStart))
self.log.info("calling command " + str(self.command) + " at " + str(self.commandStart))
#self.output=sp.check_output(self.command.split())
process = Popen(self.command, stdout=sp.PIPE, stderr=sp.PIPE, shell=True)
self.output, self.error = process.communicate()
self.log.d(str(self.output))
self.log.d(str(self.error))
self.log.debug(str(self.output))
self.log.debug(str(self.error))
self.retcode = process.poll()
except CalledProcessError:
self.error = CalledProcessError.output

View file

@ -40,7 +40,7 @@ class Job:
self.jobThreshold += 1
if self.jobThreshold >= serviceThreshold:
self.log.d("Threshold reached!")
self.log.debug("Threshold reached!")
self.handle_alarm(jobInfo, self.jobThreshold - serviceThreshold)
def handle_alarm(self, jobInfo, thresholdOffset):
@ -49,8 +49,8 @@ class Job:
task.execute(jobInfo.get_message(), self.core)
def handle_call(self):
self.log.d("handle call")
self.log.d(self.service)
self.log.debug("handle call")
self.log.debug(self.service)
try:
jobInfo = JobInfo(self.host, self.service)
self.service._execute(jobInfo)
@ -58,12 +58,12 @@ class Job:
self.handle_threshold(jobInfo, self.service.get_threshold(), jobInfo.was_execution_successful())
self.log.d("Code: " + str(jobInfo.get_errorcode()) + ", Message: " + str(jobInfo.get_message()))
self.log.debug("Code: " + str(jobInfo.get_errorcode()) + ", Message: " + str(jobInfo.get_message()))
self.jobInfos.append(jobInfo)
except Exception, e:
self.log.d(e)
self.log.debug(e)
class JobInfo(object):

View file

@ -5,7 +5,10 @@ __default_config__ = "./examples/minimal.json"
import argparse
import logging
from lib.core.logger import Logger
import logging.handlers
import os
import os.path as path
from lib.frontends.lish import LishFrontend
from lib.config.parser import FullConfigParser
from apscheduler.scheduler import Scheduler
@ -38,15 +41,42 @@ def parseArgs():
return parser.parse_args()
def setup_logging(logfile="./log/linspector.log", logLevel=logging.DEBUG, logfileLevel=logging.DEBUG):
logfile = path.expanduser(logfile)
if not path.exists(path.dirname(logfile)):
os.makedirs(path.dirname(logfile))
log = logging.getLogger("LinspectorLogger")
log.setLevel(logging.DEBUG)
consoleHandler = logging.StreamHandler()
consoleHandler.setLevel(logLevel)
fileHandler = logging.handlers.RotatingFileHandler(logfile, maxBytes=1024000, backupCount=4)
fileHandler.setLevel(logfileLevel)
consoleFormatter = logging.Formatter('[%(levelname)s]: %(message)s')
#TODO: if debug with file and function else without that
fileFormatter = logging.Formatter('%(asctime)s %(pathname)s %(module)s %(funcName)s %(lineno)d [%(levelname)s]: %(message)s')
consoleHandler.setFormatter(consoleFormatter)
fileHandler.setFormatter(fileFormatter)
log.addHandler(consoleHandler)
log.addHandler(fileHandler)
return log
def handleJob(jobInfo):
jobInfo.handle_call()
def main():
args = parseArgs()
log = Logger(args.logfile, args.loglevel)
print args.logfile
log = setup_logging(args.logfile, args.loglevel)
log.i("parsed arguments")
log.info("parsed arguments")
configParser = FullConfigParser(log)
linConf, core = configParser.parse_config(args.config)
@ -70,11 +100,10 @@ def main():
frontend = LishFrontend(jobs=jobs, scheduler=scheduler, linspectorConfig=linConf)
log.d("shutting down scheduler")
log.debug("shutting down scheduler")
logging.shutdown()
scheduler.shutdown(wait=False)
log.close()
if __name__ == "__main__":
main()