tornado日志使用详解
1.需求
将http访问记录,程序自定义日志输出到文件,按天分割,保留最近30天的日志。
2.使用示例
init_logging("%s/QYK.%s.%s.log" % (log_path, tornado.options.options.mode, tornado.options.options.port))
def init_logging(log_file):
# 使用TimedRotatingFileHandler处理器
file_handler = TimedRotatingFileHandler(log_file, when="d", interval=1, backupCount=30)
# 输出格式
log_formatter = logging.Formatter(
"%(asctime)s [%(threadName)-12.12s] [%(levelname)-5.5s] [%(lineno)d] %(message)s"
)
file_handler.setFormatter(log_formatter)
# 将处理器附加到根logger
root_logger = logging.getLogger()
root_logger.addHandler(file_handler)
logging.info('测试日志输出')
运行后日志文件内容:

tornado中会将logging的输出级别设置为info
3.http访问日志
tornado中http访问的日志是由access_log处理器完成的,access_log继承了根logger,因此,
access_log处理器也会将日志输出到示例中的日志文件中,如下所示:
import logging
import datetime
class Test1Handler(ApiHandler):
async def get(self, *args, **kwargs):
self.return_success_response()
return
运行后日志文件内容:

查看tornado源码tornado\web.py,可以看到access_log的输出格式:
def log_request(self, handler):
"""Writes a completed HTTP request to the logs.
By default writes to the python root logger. To change
this behavior either subclass Application and override this method,
or pass a function in the application settings dictionary as
``log_function``.
"""
if "log_function" in self.settings:
self.settings["log_function"](handler)
return
if handler.get_status() < 400:
log_method = access_log.info
elif handler.get_status() < 500:
log_method = access_log.warning
else:
log_method = access_log.error
request_time = 1000.0 * handler.request.request_time()
log_method("%d %s %.2fms", handler.get_status(),
handler._request_summary(), request_time)
如何自定义http访问日志的输出格式呢?通过源码,可知有两种方法可以解决这个问题
1)我们可以在Application中自定义一个log_function即可
def log_func(handler):
if handler.get_status() < 400:
log_method = access_log.info
elif handler.get_status() < 500:
log_method = access_log.warning
else:
log_method = access_log.error
request_time = 1000.0 * handler.request.request_time()
log_method("%d %s %s (%s) %s %s %.2fms",
handler.get_status(), handler.request.method,
handler.request.uri, handler.request.remote_ip,
handler.request.headers["User-Agent"],
handler.request.arguments,
request_time)
class QYKApplication(tornado.web.Application):
def __init__(self, config, webpack_setting, _redis, session_class):
settings = dict()
settings["log_function"] = log_func
运行后日志文件内容:

在输出格式中,增加了访问url,ip,代理服务器,参数等信息
2)重写RequestHandler的_request_summary方法
def _request_summary(self):
return "%s %s (%s) %s\n%s" % (self.request.method, self.request.uri,
self.request.remote_ip, self.request.headers["User-Agent"],
self.request.arguments)
tornado日志使用详解的更多相关文章
- lombok+slf4j+logback SLF4J和Logback日志框架详解
maven 包依赖 <dependency> <groupId>org.projectlombok</groupId> <artifactId>lomb ...
- IIS日志字段详解
IIS日志字段详解 抓住8月的尾巴,弥补下这个月的空白,事情太多,忘了写博客这回事了. IIS日志字段设置 网站运营时 ...
- 关于syslog日志功能详解 事件日志分析、EventLog Analyzer
关于syslog日志功能详解 事件日志分析.EventLog Analyzer 一.日志管理 保障网络安全 Windows系统日志分析 Syslog日志分析 应用程序日志分析 Windows终端服务器 ...
- MySQL日志功能详解
MySQL日志功能详解 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 一.查询日志 它是用来保存所有跟查询相关的日志,这种日志类型默认是关闭状态的,因为MySQL的用户有很多,如果 ...
- syslog之一:Linux syslog日志系统详解
目录: <syslog之一:Linux syslog日志系统详解> <syslog之二:syslog协议及rsyslog服务全解析> <syslog之三:建立Window ...
- SLF4J和Logback日志框架详解
SLF4J和Logback日志框架详解 作者:chszs,转载需注明.博客主页:http://blog.csdn.net/chszs 本文讲述SLF4J和Logback日志框架. SLF4J是一套 ...
- 项目log4j日志管理详解
项目log4j日志管理详解 log4j日志系统在项目中重要性在这里就不再累述,我们在平时使用时如果没有特定要求,只需在log4j.properties文件中顶入输出级别就行了.如果要自定义输出文件,对 ...
- GC日志分析详解
点击返回上层目录 原创声明:作者:Arnold.zhao 博客园地址:https://www.cnblogs.com/zh94 GC日志分析详解 以ParallelGC为例,YoungGC日志解释如下 ...
- Java中日志组件详解
avalon-logkit Java中日志组件详解 lanhy 发布于 2020-9-1 11:35 224浏览 0收藏 作为开发人员,我相信您对日志记录工具并不陌生. Java还具有功能强大且功能强 ...
随机推荐
- 发现linux shell中$0,$?,$!等的特殊用法
记录下linux shell下的特殊用法及参数的说明 变量说明: $$ Shell本身的PID(ProcessID) $! Shell最后运行的后台Process的PID $? 最后运行的命令的结束代 ...
- 基于.net 的加载自定义配置-误操作
有时候 需要 将程序加载自定义的配置文件,除了自己写解析xml文件.内置的ConfigutionManager对象 是个不错的选项. 按照 app.config 的方式,做一个副本.然后从你的配置文件 ...
- 迅雷极速版|xunlei下载
迅雷很不错的下载软件,曾经出现了,迷你版.极速版... 迅雷极速版 迅雷精简版 迅雷极速版-下载: http://pan.baidu.com/s/1dF3XYTj 密码: 5tj3 迅雷精简版-下载: ...
- mysql利用yum安装指定数据存放路径
测试环境: Centos6.5.MySQL5.6.28 yum安装具有速度快,便捷关键是不用编译,编译时间太久了! 01.下载mysql https://mirrors.tuna.tsinghua.e ...
- cocos2dx跟eclipse交叉编译“make: * No rule to make target `all' Stop”的解决方案
cocos2dx和eclipse交叉编译“make: *** No rule to make target `all'. Stop”的解决方案 搞cocos2dx在eclipse上的交叉编译. 项目. ...
- 笔记本连接老式显示器(VGA线+HDMI接口)
参考:http://www.cnblogs.com/me115/p/3970945.html
- HDUOJ-------2844Coins
Coins Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submi ...
- 基于KWIC 的keyword匹配算法(管道+过滤器模式下实现)
以下是基于KWIC 的keyword匹配算法(管道+过滤器模式下实现) 关键部分的管道+过滤器 软件体系下的实现, 在非常多的keyword搜索平台都使用了这一 循环移位+排序输出的 keyword匹 ...
- Python max() 方法
描述 Python max() 方法返回字符串中最大的字母(26个字母中最大的是Z). 语法 max() 方法语法: max(S) 参数 S -- 字符串. 返回值 返回字符串中最大的字母. 实例 以 ...
- Java JNI的具体介绍
JNI就是Java Native Interface的简称,也就是java本地接口.它提供了若干的API实现了和Java和其它语言的通信(主要是C&C++).也许不少人认为Java已经足够强大 ...