From 5f2d5dbc45f1bb84c3f58078cb132e0992ec0994 Mon Sep 17 00:00:00 2001 From: Guillaume Horel Date: Mon, 25 Apr 2011 00:52:05 -0400 Subject: Some further daemon cleanups. - alias logs should be properly redirected - needs to redirect sleekxmpp somewhere --- server/alias.py | 39 ++++++++++++++++++++++----------------- 1 file changed, 22 insertions(+), 17 deletions(-) (limited to 'server/alias.py') diff --git a/server/alias.py b/server/alias.py index ae3d191..8d75ec2 100644 --- a/server/alias.py +++ b/server/alias.py @@ -1,10 +1,12 @@ #!/usr/bin/python2 import logging from argparse import ArgumentParser -from config import filename, config +from config import config import daemon import daemon.pidfile from server import ObjectComponent +import os.path +import sys if __name__ == '__main__': commandline = ArgumentParser(description = 'Connect the alias \ @@ -31,16 +33,12 @@ if __name__ == '__main__': help = 'run the server in the background', action = 'store_true') commandline.add_argument('--logfile', - help = 'location of the log file (default /var/log/${name}.log', - action = 'store_const') + help = 'location of the log file (default /var/log/${name}.log') commandline.add_argument('--pidfile', - help = 'location of the pid file (default /var/run/${name}.pid', - action = 'store_const') - + help = 'location of the pid file (default /var/run/${name}.pid') args = commandline.parse_args() if args.config is None: - logging.basicConfig(level = args.debug) config.name = args.name config.port = args.port config.secret = args.secret @@ -51,24 +49,31 @@ if __name__ == '__main__': config.pidfile = args.pidfile else: filename = args.config - logging.basicConfig(level = args.debug) config.read(filename) - - logger = logging.getLogger("DaemonLog") - logger.setLevel(logging.INFO) - formatter = logging.Formatter( -"%(asctime)s - %(name)s - %(levelname)s - %(message)s") + if config.logfile is None: + config.logfile = os.path.join('/var/log/', config.name + '.log') + if config.pidfile is None: + config.pidfile = os.path.join('/var/run/', config.name + '.pid') + + #set up the logger handler = logging.FileHandler(config.logfile) + formatter = logging.Formatter("%(asctime)s - %(name)s - %(levelname)s - %(message)s") + handler.setFormatter(formatter) + + logger = logging.getLogger("alias") + logger.setLevel(args.debug) logger.addHandler(handler) + import pdb; pdb.set_trace() context = daemon.DaemonContext(detach_process = config.background, pidfile = daemon.pidfile.TimeoutPIDLockFile(config.pidfile,10), - files_preserve=[handler.stream]) + files_preserve=[handler.stream.fileno()], + stdout=sys.stdout, stderr=sys.stderr) with context: component = ObjectComponent(config.name, config.secret, config.host, config.port, config.root) - if component.connect(): - logging.info('Component {} connected'.format(component.boundjid)) + if component.connect(): + logger.info('Component {} connected'.format(component.boundjid)) component.process(False) else: - logging.error("Component {} couldn't connect".format(component.boundjid)) \ No newline at end of file + logger.error("Component {} couldn't connect".format(component.boundjid)) -- cgit v1.2.3-70-g09d2