7

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.

tok
  • 909
  • 1
  • 10
  • 21

2 Answers2

0

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)
c8999c 3f964f64
  • 1,430
  • 1
  • 12
  • 25
  • 1
    Thanks. This is good to know but not quite what I was looking for. I'd like the wsgi server to do the logging, not the application running under it. – tok Nov 13 '19 at 15:50
0

This is my config file

[loggers]
keys=root

[handlers]
keys=logging_handler

[formatters]
keys=simple_formatter

[logger_root]
level=INFO
handlers=logging_handler

[handler_logging_handler]
formatter=simple_formatter
class=handlers.RotatingFileHandler
maxBytes=31457280
level=INFO
args=('yourlogfile.log',)

[formatter_simple_formatter]
format=%(asctime)s %(levelname)s %(name)s %(threadName)s : %(message)s
datefmt=

You need to define a handler that writes to the logfile that you can specify in the args argument.

Alternatively, if you don't want to use a config file, you can define it in basic configuration directly like so:

logging.basicConfig(
    filename='yourlogfile.log', 
    level=logging.INFO,
    ...
)
Mμ.
  • 8,382
  • 3
  • 26
  • 36