Skip to content
This repository has been archived by the owner on Mar 24, 2021. It is now read-only.

Commit

Permalink
Make sure log files are rotated
Browse files Browse the repository at this point in the history
Currently, whilst deployed on PaaS the log files fill up until all
available space is used and then the app freezes.  This PR forces the
app to rotate log files every 10Mb with 5 backups.

This fixes #476
  • Loading branch information
rossjones committed Mar 20, 2018
1 parent 1d5580a commit 61090bf
Showing 1 changed file with 12 additions and 9 deletions.
21 changes: 12 additions & 9 deletions backdrop/core/log_handler.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
from logging import FileHandler
from logging.handlers import RotatingFileHandler
from logstash_formatter import LogstashFormatter
import logging
from flask import request
Expand All @@ -18,15 +19,15 @@ def filter(self, record):


def get_log_file_handler(path, log_level=logging.DEBUG):
handler = FileHandler(path)
handler = RotatingFileHandler(path, maxBytes=1024*1024*10, backupCount=5)
handler.setFormatter(logging.Formatter(
"%(asctime)s [%(levelname)s] -> %(message)s"))
handler.setLevel(log_level)
return handler


def get_json_log_handler(path, app_name):
handler = FileHandler(path)
handler = RotatingFileHandler(path, maxBytes=1024*1024*10, backupCount=5)
formatter = LogstashFormatter()
formatter.defaults['@tags'] = ['application', app_name]
handler.setFormatter(formatter)
Expand Down Expand Up @@ -64,19 +65,21 @@ def set_up_audit_logging(app, env):

def create_request_logger(app):
def log_request():
app.logger.info("request: %s - %s" % (request.method, request.url),
if request.method != "HEAD":
app.logger.info("request: %s - %s" % (request.method, request.url),
extra=create_logging_extra_dict())
return log_request


def create_response_logger(app):
def log_response(response):
app.logger.info(
"response: %s - %s - %s" % (
request.method, request.url, response.status
),
extra=create_logging_extra_dict()
)
if request.method != "HEAD":
app.logger.info(
"response: %s - %s - %s" % (
request.method, request.url, response.status
),
extra=create_logging_extra_dict()
)
return response
return log_response

Expand Down

0 comments on commit 61090bf

Please sign in to comment.