I have just started to use Sanic, and am trying to get the access log entries written to a log file instead of the console. I am trying to run Sanic with 2 worker processes. I know that using a rotating file handler with multiple processes can cause problems from my experience using Uvicorn. My understanding from reading the Sanic documentation is that I should be able to use a rotating file handler if I enable the background logger. When I enable the background logger and configure the access logger to use a rotating file handler I receive a couple of errors in the console, and the created access log file is empty. I am probably configuring something incorrectly. Any suggestions of what to change would be appreciated.
The errors I receive are:
TypeError: RotatingFileHandler.init() got an unexpected keyword argument ‘stream’
ValueError: Unable to configure handler ‘access_file’
# server.py
from sanic import Sanic
from sanic.response import HTTPResponse, JSONResponse
from sanic.response import html, json, text
from sanic.request import Request
import sys
# Enable the background logger
sanic_config = {
"LOGGING": True
}
# Modify the default logging config to have the
# access log written to a rotating file handler
configuration = {
"version": 1,
"disable_existing_loggers": True,
"loggers": {
"sanic.root": {"level": "INFO", "handlers": ["console"]},
"sanic.error": {
"level": "INFO",
"handlers": ["error_console"],
"propagate": True,
"qualname": "sanic.error",
},
"sanic.access": {
"level": "INFO",
"handlers": ["access_file"],
"propagate": True,
"qualname": "sanic.access",
},
"sanic.server": {
"level": "INFO",
"handlers": ["console"],
"propagate": True,
"qualname": "sanic.server",
},
"sanic.websockets": {
"level": "INFO",
"handlers": ["console"],
"propagate": True,
"qualname": "sanic.websockets",
},
},
"handlers": {
"console": {
"class": "logging.StreamHandler",
"formatter": "generic",
"stream": sys.stdout,
},
"error_console": {
"class": "logging.StreamHandler",
"formatter": "generic",
"stream": sys.stderr,
},
"access_file": {
"formatter": "access",
"class": "logging.handlers.RotatingFileHandler",
"filename": "C:/Logs/Sanic/sanic_access.log",
"maxBytes": 1500,
"backupCount": 3,
"encoding": "utf-8"
},
},
"formatters": {
"generic": {"class": "sanic.logging.formatter.AutoFormatter"},
"access": {
"fmt": "%(asctime)s.%(msecs)03d | %(levelname)s | %(host)s - '%(request)s' - %(status)s - %(duration)",
"use_colors": False,
"datefmt": "%Y-%m-%d %H:%M:%S",
},
},
}
app = Sanic("HelloWorld", log_config=configuration)
app.update_config(sanic_config)
app.static("/", "C:/Sanic/static/")
I am starting Sanic from the terminal with the following command:
sanic server:app --host=127.0.0.1 --port=80 --dev --workers=2 --access-log
I am using a Windows 11 computer running Python 3.13.9, Sanic v25.3.0, and sanic-ext 24.12.0.