Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

issues #116 - log rotation with logrotate.d #260

Closed
wants to merge 6 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions daphne/access.py
Original file line number Diff line number Diff line change
Expand Up @@ -68,3 +68,7 @@ def write_entry(
length or "-",
)
)

def close_stream(self):
self.stream.flush()
self.stream.close()
11 changes: 11 additions & 0 deletions daphne/cli.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import argparse
import functools
import logging
import signal
import sys
from argparse import ArgumentError, Namespace

Expand Down Expand Up @@ -40,6 +41,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
Expand Down Expand Up @@ -183,6 +186,12 @@ def __init__(self):

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):
"""
Expand Down Expand Up @@ -244,7 +253,9 @@ def run(self, args):
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
Expand Down
4 changes: 4 additions & 0 deletions daphne/server.py
Original file line number Diff line number Diff line change
Expand Up @@ -326,3 +326,7 @@ def log_action(self, protocol, action, details):
"""
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