From e86e2048a124a1c4abf16712b548d7e7d7815ca3 Mon Sep 17 00:00:00 2001 From: Guillaume Horel Date: Mon, 25 Apr 2011 22:39:39 -0400 Subject: Final cleanup of the logging system - set up a root logger, which is going to catch all the other logger instances set up by other modules (sleekxmpp for instance) - by default, if not in background mode, everything gets logged to the console. --- server/alias.py | 38 +++++++++++++++++++++++++------------- 1 file changed, 25 insertions(+), 13 deletions(-) (limited to 'server/alias.py') diff --git a/server/alias.py b/server/alias.py index 8d75ec2..30925e1 100644 --- a/server/alias.py +++ b/server/alias.py @@ -55,25 +55,37 @@ if __name__ == '__main__': if config.pidfile is None: config.pidfile = os.path.join('/var/run/', config.name + '.pid') - #set up the logger - handler = logging.FileHandler(config.logfile) + #set up the root logger + logging.getLogger('').setLevel(args.debug) formatter = logging.Formatter("%(asctime)s - %(name)s - %(levelname)s - %(message)s") - handler.setFormatter(formatter) + if config.background: + #save logs in a file + fh = logging.FileHandler(config.logfile) + fh.setFormatter(formatter) + logging.getLogger('').addHandler(fh) + else: + #save logs to the console + ch = logging.StreamHandler() + ch.setFormatter(formatter) + logging.getLogger('').addHandler(ch) + + if config.background: + context = daemon.DaemonContext(detach_process = True, + pidfile = daemon.pidfile.TimeoutPIDLockFile(config.pidfile,10), + files_preserve=[fh.stream.fileno()], + working_directory=os.path.abspath(config.root), + stdout=sys.stdout, stderr=sys.stderr) + else: + context = daemon.DaemonContext(detach_process = False, + stdout=sys.stdout, stderr=sys.stderr, + working_directory=os.path.abspath(config.root)) - 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.fileno()], - stdout=sys.stdout, stderr=sys.stderr) with context: component = ObjectComponent(config.name, config.secret, config.host, config.port, config.root) if component.connect(): - logger.info('Component {} connected'.format(component.boundjid)) + logging.info('Component {} connected'.format(component.boundjid)) component.process(False) else: - logger.error("Component {} couldn't connect".format(component.boundjid)) + logging.error("Component {} couldn't connect".format(component.boundjid)) -- cgit v1.2.3-70-g09d2