Ron's solution logs only requests which were handled by the routes. If you want to log everything, including 404 file not found, you should consider a simple WSGI middleware like wsgi-request-logger.
#!/usr/bin/python3
import bottle
from bottle import route
from requestlogger import WSGILogger, ApacheFormatter
import waitress
import logging
import sys
@route('/hello')
def hello():
return 'Hello World'
waitress.serve(WSGILogger(
bottle.default_app(), [logging.StreamHandler(sys.stdout)],
ApacheFormatter(), propagate=False
))
The output looks the following way:
192.168.190.102 - - [09/Mar/2023:09:39:10 +0200] "GET /hello HTTP/1.1" 200 11 "" "curl/7.68.0" 0/228
192.168.190.102 - - [09/Mar/2023:09:39:14 +0200] "GET /zzz HTTP/1.1" 404 731 "" "curl/7.68.0" 0/9617
If you want to log to a file, you can add another logging
handler in the list or replace the existing one completely. Here is an example of a handler which logs to a file in a fashion similar to the Apache access logs:
#!/usr/bin/python3
import waitress
import bottle
from bottle import route
from requestlogger import WSGILogger, ApacheFormatter
import logging
import sys
from logging.handlers import TimedRotatingFileHandler
@route('/hello')
def hello():
return 'Hello World'
logging_handlers = [
logging.StreamHandler(sys.stdout),
TimedRotatingFileHandler('access.log', 'd', 7)
]
waitress.serve(WSGILogger(
bottle.default_app(), logging_handlers,
ApacheFormatter(), propagate=False
))
I don't know what's the performance impact of the middleware.