日志

Sanic 允许你在基于 python3 logging API 的请求上做不同类型的日志 (access log, error log)。如果你想创建一个新的配置,你应该了解 python3 日志模块的基本知识。

快速开始

一个使用默认设置的简单例子如下:

  1. from sanic import Sanic
  2. app = Sanic('test')
  3. @app.route('/')
  4. async def test(request):
  5. return response.text('Hello World!')
  6. if __name__ == "__main__":
  7. app.run(debug=True, access_log=True)

要使用你自己的日志配置,简单使用 logging.config.dictConfig,或者在你初始化 Sanic 应用的时候传递 log_config

  1. app = Sanic('test', log_config=LOGGING_CONFIG)

要关闭日志,只需指定 access_log=False:

  1. if __name__ == "__main__":
  2. app.run(access_log=False)

这会在处理请求时跳过调用日志记录程序。并且你甚至可以在生产中进一步提高速度:

  1. if __name__ == "__main__":
  2. # disable debug messages
  3. app.run(debug=False, access_log=False)

配置

默认情况下,log_config 参数设置为使用 sanic.log.LOGGING_CONFIG_DEFAULTS 字典进行配置。

sanic 使用了三种 loggers,并且 当你想要创建你自己的日志配置时必须被定义:

  • root:
    用来记录内部消息。

  • sanic.error:
    用来记录错误日志。

  • sanic.access:
    用来记录访问日志。

日志格式化:

除了 python 提供的默认参数 (asctime, levelname, message) 外,Sanic 提供了额外的参数给访问日志:

  • host (str)
    request.ip
  • request (str)
    request.method + “ “ + request.url
  • status (int)
    response.status
  • byte (int)
    len(response.body)

默认的访问日志格式如下:

  1. %(asctime)s - (%(name)s)[%(levelname)s][%(host)s]: %(request)s %(message)s %(status)d %(byte)d