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:
parent
3d63f48621
commit
4048bc252e
4 changed files with 51 additions and 22 deletions
|
|
@ -71,7 +71,7 @@ class ConfigParser:
|
||||||
with open(configFilename) as cfgFile:
|
with open(configFilename) as cfgFile:
|
||||||
config = cfgFile.read()
|
config = cfgFile.read()
|
||||||
|
|
||||||
self.log.i("reading Config: " + configFilename)
|
self.log.info("reading Config: " + configFilename)
|
||||||
return json.loads(config)
|
return json.loads(config)
|
||||||
|
|
||||||
def _create_raw_Object(self, jsonDict, msgName, creator):
|
def _create_raw_Object(self, jsonDict, msgName, creator):
|
||||||
|
|
@ -90,8 +90,8 @@ class ConfigParser:
|
||||||
item = creator(key, val)
|
item = creator(key, val)
|
||||||
items.append(item)
|
items.append(item)
|
||||||
except Exception:
|
except Exception:
|
||||||
self.log.w("ignoring " + msgName + ": " + key + "! reason:")
|
self.log.warning("ignoring " + msgName + ": " + key + "! reason:")
|
||||||
self.log.w(str(Exception))
|
self.log.warning(str(Exception))
|
||||||
return items
|
return items
|
||||||
|
|
||||||
def _load_module(self, clazz, modPart):
|
def _load_module(self, clazz, modPart):
|
||||||
|
|
@ -135,15 +135,15 @@ class ConfigParser:
|
||||||
if class_check(item):
|
if class_check(item):
|
||||||
repl.append(item)
|
repl.append(item)
|
||||||
else:
|
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:
|
except ImportError, err:
|
||||||
self.log.w("Could not import " + clazz + ": " + str(clazzItem) + "! reason")
|
self.log.warning("Could not import " + clazz + ": " + str(clazzItem) + "! reason")
|
||||||
self.log.w(str(err))
|
self.log.warning(str(err))
|
||||||
except KeyError, k:
|
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:
|
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[:]
|
del items[:]
|
||||||
items.extend(repl)
|
items.extend(repl)
|
||||||
|
|
|
||||||
|
|
@ -27,12 +27,12 @@ class Command:
|
||||||
|
|
||||||
try:
|
try:
|
||||||
self.commandStart = dt.now()
|
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())
|
#self.output=sp.check_output(self.command.split())
|
||||||
process = Popen(self.command, stdout=sp.PIPE, stderr=sp.PIPE, shell=True)
|
process = Popen(self.command, stdout=sp.PIPE, stderr=sp.PIPE, shell=True)
|
||||||
self.output, self.error = process.communicate()
|
self.output, self.error = process.communicate()
|
||||||
self.log.d(str(self.output))
|
self.log.debug(str(self.output))
|
||||||
self.log.d(str(self.error))
|
self.log.debug(str(self.error))
|
||||||
self.retcode = process.poll()
|
self.retcode = process.poll()
|
||||||
except CalledProcessError:
|
except CalledProcessError:
|
||||||
self.error = CalledProcessError.output
|
self.error = CalledProcessError.output
|
||||||
|
|
|
||||||
|
|
@ -40,7 +40,7 @@ class Job:
|
||||||
self.jobThreshold += 1
|
self.jobThreshold += 1
|
||||||
|
|
||||||
if self.jobThreshold >= serviceThreshold:
|
if self.jobThreshold >= serviceThreshold:
|
||||||
self.log.d("Threshold reached!")
|
self.log.debug("Threshold reached!")
|
||||||
self.handle_alarm(jobInfo, self.jobThreshold - serviceThreshold)
|
self.handle_alarm(jobInfo, self.jobThreshold - serviceThreshold)
|
||||||
|
|
||||||
def handle_alarm(self, jobInfo, thresholdOffset):
|
def handle_alarm(self, jobInfo, thresholdOffset):
|
||||||
|
|
@ -49,8 +49,8 @@ class Job:
|
||||||
task.execute(jobInfo.get_message(), self.core)
|
task.execute(jobInfo.get_message(), self.core)
|
||||||
|
|
||||||
def handle_call(self):
|
def handle_call(self):
|
||||||
self.log.d("handle call")
|
self.log.debug("handle call")
|
||||||
self.log.d(self.service)
|
self.log.debug(self.service)
|
||||||
try:
|
try:
|
||||||
jobInfo = JobInfo(self.host, self.service)
|
jobInfo = JobInfo(self.host, self.service)
|
||||||
self.service._execute(jobInfo)
|
self.service._execute(jobInfo)
|
||||||
|
|
@ -58,12 +58,12 @@ class Job:
|
||||||
|
|
||||||
self.handle_threshold(jobInfo, self.service.get_threshold(), jobInfo.was_execution_successful())
|
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)
|
self.jobInfos.append(jobInfo)
|
||||||
|
|
||||||
except Exception, e:
|
except Exception, e:
|
||||||
self.log.d(e)
|
self.log.debug(e)
|
||||||
|
|
||||||
|
|
||||||
class JobInfo(object):
|
class JobInfo(object):
|
||||||
|
|
|
||||||
41
linspector
41
linspector
|
|
@ -5,7 +5,10 @@ __default_config__ = "./examples/minimal.json"
|
||||||
|
|
||||||
import argparse
|
import argparse
|
||||||
import logging
|
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.frontends.lish import LishFrontend
|
||||||
from lib.config.parser import FullConfigParser
|
from lib.config.parser import FullConfigParser
|
||||||
from apscheduler.scheduler import Scheduler
|
from apscheduler.scheduler import Scheduler
|
||||||
|
|
@ -38,15 +41,42 @@ def parseArgs():
|
||||||
return parser.parse_args()
|
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):
|
def handleJob(jobInfo):
|
||||||
jobInfo.handle_call()
|
jobInfo.handle_call()
|
||||||
|
|
||||||
|
|
||||||
def main():
|
def main():
|
||||||
args = parseArgs()
|
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)
|
configParser = FullConfigParser(log)
|
||||||
linConf, core = configParser.parse_config(args.config)
|
linConf, core = configParser.parse_config(args.config)
|
||||||
|
|
@ -70,11 +100,10 @@ def main():
|
||||||
|
|
||||||
frontend = LishFrontend(jobs=jobs, scheduler=scheduler, linspectorConfig=linConf)
|
frontend = LishFrontend(jobs=jobs, scheduler=scheduler, linspectorConfig=linConf)
|
||||||
|
|
||||||
log.d("shutting down scheduler")
|
log.debug("shutting down scheduler")
|
||||||
|
logging.shutdown()
|
||||||
scheduler.shutdown(wait=False)
|
scheduler.shutdown(wait=False)
|
||||||
|
|
||||||
log.close()
|
|
||||||
|
|
||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
main()
|
main()
|
||||||
Loading…
Add table
Add a link
Reference in a new issue