Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Logging requests to a file with Waitress, Flask and Python

I need log http-requests to a file in a Waitress server running a Flask application. I wanted to separate the Flask app from the server so I created a file

myapp_waitress.py

from myflaskapp import app
from waitress import serve
from paste.translogger import TransLogger
import logging.config
import os

BASE_DIR = os.getcwd()
log_ini_file = os.path.join(BASE_DIR, "mylog.ini")

logging.config.fileConfig(log_ini_file)
logger = logging.getLogger('waitress')

serve(TransLogger(app, setup_console_handler=False))

and run the Waitress server like

python myapp_waitress.py

What should I to put in mylog.ini file to make Waitress to log requests to a file? I've read https://docs.pylonsproject.org/projects/waitress/en/stable/logging.html several times but as a new pythonist can't make much sense of it. What I'd like to have is a simple example of file logging from Waitress.

like image 272
tok Avatar asked May 31 '26 07:05

tok


1 Answers

Not sure if this is applicable to your case, but Flask offers a decorator function to log requests

from flask import request

@app.before_request
def log_the_request():
    logger(request)
like image 75
c8999c 3f964f64 Avatar answered Jun 02 '26 20:06

c8999c 3f964f64



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!