Переглянути джерело

implemented minimal logging to syslog and console

Helmut Pozimski 11 роки тому
батько
коміт
add002fb5e
1 змінених файлів з 33 додано та 3 видалено
  1. 33 3
      stdd

+ 33 - 3
stdd

@@ -10,6 +10,7 @@ import time
 import sys
 import signal
 import os
+import logging
 from optparse import OptionParser
 
 import stddlib.daemon
@@ -42,10 +43,8 @@ parser.add_option("-c", "--config", dest="config", help="define an\
 
 run = True
 
-""" create the configuration object according to the given parameters and
-read the file itself
 
-"""
+""" define a sighandler to properly catch signals """
 
 def sighandler(signum, frame):
 	if signum == 2:
@@ -63,6 +62,11 @@ def sighandler(signum, frame):
 			#TODO: Log error message
 			sys,exit(1)
 	
+""" create the configuration object according to the given parameters and
+read the file itself
+
+"""
+
 signal.signal(signal.SIGTERM, sighandler)
 signal.signal(signal.SIGINT, sighandler)
 
@@ -73,6 +77,31 @@ else:
 	config.Read()
 config.Analyze()
 
+
+""" create a logger to log errors according to configuration """
+
+logger = logging.getLogger("stdd")
+
+if config.syslog_level == "debug":
+	logger.setLevel(logging.DEBUG)
+elif config.syslog_level == "error":
+	logger.setLevel(logging.ERROR)
+else:
+	logget.setLevel(logging.INFO)
+
+syslog_handler = logging.handlers.SysLogHandler("/dev/log")
+console_handler = logging.StreamHandler()
+
+formatter = logging.Formatter("%(asctime)s - %(name)s - %(message)s")
+
+syslog_handler.setFormatter(formatter)
+console_handler.setFormatter(formatter)
+
+if options.daemon is True:
+	logger.addHandler(syslog_handler)
+else:
+	logger.addHandler(console_handler)
+
 if options.daemon is True:
 	if os.access("/run", os.F_OK & os.W_OK) is True:
 		daemon = stddlib.daemon.Daemon("/run", "stdd")
@@ -80,6 +109,7 @@ if options.daemon is True:
 		daemon = stddlib.daemon.Daemon("/var/run", "stdd")
 	daemon.Daemonize()
 	daemon.Start()
+	logger.info("daemon started")
 	cmdline = ""
 	for element in sys.argv:
 		cmdline = cmdline + " " + element