From 103f6ab33a60e318df75c537ee143674b735c90d Mon Sep 17 00:00:00 2001 From: root Date: Fri, 2 Jun 2023 12:31:55 +0200 Subject: [PATCH] Added logfile --- game.py | 39 ++++++++++++++++++++++++++++++++++++++- supervisor.conf | 2 ++ 2 files changed, 40 insertions(+), 1 deletion(-) diff --git a/game.py b/game.py index bf3af49..a5ea5c2 100755 --- a/game.py +++ b/game.py @@ -1,8 +1,11 @@ #! /bin/python3 from random import choice -from flask import Flask, render_template +from flask import Flask, render_template, request from configparser import ConfigParser +from datetime import datetime +import logging +import pytz app = Flask(__name__, static_url_path='', @@ -11,6 +14,40 @@ app = Flask(__name__, config = ConfigParser() config.read('config.ini') +logging.basicConfig(encoding='utf-8', level=logging.INFO) +logger = logging.getLogger('waitress') +tz = pytz.timezone('Europe/Berlin') + +@app.after_request +def log_the_request(response): + now = datetime.now(tz=tz) + + if not request.remote_user: + remote_user = "-" + else: + remote_user = request.remote_user + + if not request.referrer: + referrer = "-" + else: + referrer = request.referrer + + log = { + 'remote_addr': request.remote_addr, + 'remote_user': remote_user, + 'url': request.full_path, + 'date': now.strftime("%d/%b/%Y:%H:%M:%S %z"), + 'referrer': referrer, + 'user_agent': request.user_agent.string, + 'method': request.method, + 'content_length': response.content_length, + 'status_code': response.status_code + } + + logfile = "{} - {} [{}] \"{} {}\" {} {} \"{}\" \"{}\"".format(log['remote_addr'], log['remote_user'], log['date'], log['method'], log['url'], log['status_code'], log['content_length'], log['referrer'], log['user_agent']) + logger.info(logfile) + + return response @app.route("/") def hello(): diff --git a/supervisor.conf b/supervisor.conf index bab74a8..3dc9874 100644 --- a/supervisor.conf +++ b/supervisor.conf @@ -4,4 +4,6 @@ directory=/opt/ab/ autostart=true autorestart=true redirect_stderr=true +stdout_logfile_maxbytes=32MB +stdout_logfile_backups=10 user=www-data