3.8 KiB
Logging
Sanic allows you to do different types of logging (access log, error log) on the requests based on the python3 logging API. You should have some basic knowledge on python3 logging if you want to create a new configuration.
Quick Start
A simple example using default settings would be like this:
from sanic import Sanic
from sanic.config import LOGGING
# The default logging handlers are ['accessStream', 'errorStream']
# but we change it to use other handlers here for demo purpose
LOGGING['loggers']['network']['handlers'] = [
'accessSysLog', 'errorSysLog']
app = Sanic('test')
@app.route('/')
async def test(request):
return response.text('Hello World!')
if __name__ == "__main__":
app.run(log_config=LOGGING)
And to close logging, simply assign log_config=None:
if __name__ == "__main__":
app.run(log_config=None)
This would skip calling logging functions when handling requests. And you could even do further in production to gain extra speed:
if __name__ == "__main__":
# disable internal messages
app.run(debug=False, log_config=None)
Configuration
By default, log_config parameter is set to use sanic.config.LOGGING dictionary for configuration. The default configuration provides several predefined handlers
:
-
internal (using logging.StreamHandler)
For internal information console outputs. -
accessStream (using logging.StreamHandler)
For requests information logging in console -
errorStream (using logging.StreamHandler)
For error message and traceback logging in console. -
accessSysLog (using logging.handlers.SysLogHandler)
For requests information logging to syslog. Currently supports Windows (via localhost:514), Darwin (/var/run/syslog), Linux (/dev/log) and FreeBSD (/dev/log).
You would not be able to access this property if the directory doesn't exist. (Notice that in Docker you have to enable everything by yourself) -
errorSysLog (using logging.handlers.SysLogHandler)
For error message and traceback logging to syslog. Currently supports Windows (via localhost:514), Darwin (/var/run/syslog), Linux (/dev/log) and FreeBSD (/dev/log).
You would not be able to access this property if the directory doesn't exist. (Notice that in Docker you have to enable everything by yourself)
And filters
:
-
accessFilter (using sanic.log.DefaultFilter)
The filter that allows only levels inDEBUG
,INFO
, andNONE(0)
-
errorFilter (using sanic.log.DefaultFilter)
The filter that allows only levels inWARNING
,ERROR
, andCRITICAL
There are two loggers
used in sanic, and must be defined if you want to create your own logging configuration:
-
sanic:
Used to log internal messages. -
network:
Used to log requests from network, and any information from those requests.
Log format:
In addition to default parameters provided by python (asctime, levelname, message), Sanic provides additional parameters for network logger with accessFilter:
-
host (str)
request.ip -
request (str)
request.method + " " + request.url -
status (int)
response.status -
byte (int)
len(response.body)
The default access log format is
%(asctime)s - (%(name)s)[%(levelname)s][%(host)s]: %(request)s %(message)s %(status)d %(byte)d