make syslog logging work
authorCameron Patrick <cameron@ucc.gu.uwa.edu.au>
Sun, 1 Aug 2004 08:07:29 +0000 (08:07 +0000)
committerCameron Patrick <cameron@ucc.gu.uwa.edu.au>
Sun, 1 Aug 2004 08:07:29 +0000 (08:07 +0000)
sql-edition/servers/VendServer.py

index 9e1ba7a..6e870ff 100755 (executable)
@@ -5,7 +5,7 @@ USE_DB = 0
 
 import ConfigParser
 import sys, os, string, re, pwd, signal
-import logging
+import logging, logging.handlers
 from traceback import format_tb
 if USE_DB: import pg
 from time import time, sleep
@@ -442,7 +442,8 @@ def create_pid_file(name):
                logging.warning('unable to write to pid file '+name+': '+str(e))
 
 def set_stuff_up():
-       def do_nothing(signum, stack): pass
+       def do_nothing(signum, stack):
+               signal.signal(signum, do_nothing)
        def stop_server(signum, stack): raise KeyboardInterrupt
        signal.signal(signal.SIGHUP, do_nothing)
        signal.signal(signal.SIGTERM, stop_server)
@@ -450,8 +451,8 @@ def set_stuff_up():
 
        options = parse_args()
        config_opts = VendConfigFile(options.config_file, config_options)
-       set_up_logging(options)
        if options.daemon: become_daemon()
+       set_up_logging(options)
        if options.pid_file != '': create_pid_file(options.pid_file)
 
        return options, config_opts
@@ -477,12 +478,16 @@ def set_up_logging(options):
                        logger.addHandler(file_logger)
                except IOError, e:
                        logger.warning('unable to write to log file '+options.log_file+': '+str(e))
+
+       if options.syslog:
+               sys_logger = logging.handlers.SysLogHandler('/dev/log', 'daemon')
+               sys_logger.setFormatter(logging.Formatter('vendserver[%d]'%(os.getpid()) + ' %(levelname)s: %(message)s'))
+               logger.addHandler(sys_logger)
        
        if options.verbose:
                logger.setLevel(logging.DEBUG)
        else:
                logger.setLevel(logging.INFO)
-       
 
 def become_daemon():
        dev_null = file('/dev/null')

UCC git Repository :: git.ucc.asn.au