for log rotation with logrotate.d

called by kill -USR1 PID
This commit is contained in:
David D 2019-04-30 20:55:58 -04:00
parent a3494215cf
commit 5f92665074
3 changed files with 23 additions and 0 deletions

View File

@ -68,3 +68,7 @@ class AccessLogGenerator(object):
length or "-",
)
)
def close_stream(self):
self.stream.flush()
self.stream.close()

View File

@ -11,6 +11,9 @@ from .endpoints import build_endpoint_description_strings
from .server import Server
from .utils import import_by_path
import signal
logger = logging.getLogger(__name__)
DEFAULT_HOST = "127.0.0.1"
@ -40,6 +43,8 @@ class CommandLineInterface(object):
server_class = Server
def __init__(self):
self.access_log = None
self.parser = argparse.ArgumentParser(description=self.description)
self.parser.add_argument(
"-p", "--port", type=int, help="Port number to listen on", default=None
@ -183,6 +188,14 @@ class CommandLineInterface(object):
self.server = None
# For logrotate at SIGNALUSR1
def logrotate(self, signum, stack):
if self.access_log is not None:
access_log_stream = open(self.access_log, "a", 1)
self.server.rotate_log_action(AccessLogGenerator(access_log_stream))
@classmethod
def entrypoint(cls):
"""
@ -244,7 +257,9 @@ class CommandLineInterface(object):
if args.access_log == "-":
access_log_stream = sys.stdout
else:
self.access_log = args.access_log
access_log_stream = open(args.access_log, "a", 1)
signal.signal(signal.SIGUSR1, self.logrotate)
elif args.verbosity >= 1:
access_log_stream = sys.stdout
# Import application

View File

@ -326,3 +326,7 @@ class Server(object):
"""
if self.action_logger:
self.action_logger(protocol, action, details)
def rotate_log_action(self, log):
self.action_logger.close_stream()
self.action_logger = log