Fix server request logging.

And switch the other logs to append instead of overwrite on each run.
This commit is contained in:
Brian C. Lane 2017-11-15 14:31:39 -08:00
parent 9f9d227600
commit 950d9df217

View File

@ -21,6 +21,7 @@ import logging
log = logging.getLogger("lorax-composer") log = logging.getLogger("lorax-composer")
program_log = logging.getLogger("program") program_log = logging.getLogger("program")
pylorax_log = logging.getLogger("pylorax") pylorax_log = logging.getLogger("pylorax")
server_log = logging.getLogger("server")
import argparse import argparse
import os import os
@ -68,7 +69,7 @@ def setup_logging(logfile):
log.addHandler(sh) log.addHandler(sh)
pylorax_log.addHandler(sh) pylorax_log.addHandler(sh)
fh = logging.FileHandler(filename=logfile, mode="w") fh = logging.FileHandler(filename=logfile)
fh.setLevel(logging.DEBUG) fh.setLevel(logging.DEBUG)
fmt = logging.Formatter("%(asctime)s %(levelname)s %(name)s: %(message)s") fmt = logging.Formatter("%(asctime)s %(levelname)s %(name)s: %(message)s")
fh.setFormatter(fmt) fh.setFormatter(fmt)
@ -78,10 +79,27 @@ def setup_logging(logfile):
# External program output log # External program output log
program_log.setLevel(logging.DEBUG) program_log.setLevel(logging.DEBUG)
logfile = os.path.abspath(os.path.dirname(logfile))+"/program.log" logfile = os.path.abspath(os.path.dirname(logfile))+"/program.log"
fh = logging.FileHandler(filename=logfile, mode="w") fh = logging.FileHandler(filename=logfile)
fh.setLevel(logging.DEBUG) fh.setLevel(logging.DEBUG)
program_log.addHandler(fh) program_log.addHandler(fh)
# Server request logging
server_log.setLevel(logging.DEBUG)
logfile = os.path.abspath(os.path.dirname(logfile))+"/server.log"
fh = logging.FileHandler(filename=logfile)
fh.setLevel(logging.DEBUG)
server_log.addHandler(fh)
class LogWrapper(object):
"""Wrapper for the WSGIServer which only calls write()"""
def __init__(self, log_obj):
self.log = log_obj
def write(self, msg):
"""Log everything as INFO"""
self.log.info(msg.strip())
if __name__ == '__main__': if __name__ == '__main__':
# parse the arguments # parse the arguments
@ -95,6 +113,7 @@ if __name__ == '__main__':
if not os.path.isdir(logpath): if not os.path.isdir(logpath):
os.makedirs(logpath) os.makedirs(logpath)
setup_logging(opts.logfile) setup_logging(opts.logfile)
log.debug("opts=%s", opts)
if not os.path.isdir(opts.RECIPES): if not os.path.isdir(opts.RECIPES):
log.warn("Creating empty recipe directory at %s", opts.RECIPES) log.warn("Creating empty recipe directory at %s", opts.RECIPES)
@ -107,9 +126,8 @@ if __name__ == '__main__':
# Import example recipes # Import example recipes
commit_recipe_directory(server.config["GITLOCK"].repo, "master", opts.RECIPES) commit_recipe_directory(server.config["GITLOCK"].repo, "master", opts.RECIPES)
http_server = WSGIServer((opts.host, opts.port), server) log.info("Starting %s on %s:%d with recipes from %s", VERSION, opts.host, opts.port, opts.RECIPES)
http_server = WSGIServer((opts.host, opts.port), server, log=LogWrapper(server_log))
# The server writes directly to a file object, so point to our log directory # The server writes directly to a file object, so point to our log directory
server_logfile = os.path.abspath(os.path.dirname(opts.logfile))+"/server.log"
server_log = open(server_logfile, "w")
http_server.log = server_log
http_server.serve_forever() http_server.serve_forever()