繁体   English   中英

如何更改龙卷风的记录格式

[英]How to change Tornado's logging format

我有自己喜欢的日志记录格式和目标,可以使用logging.basicConfig进行设置。 我开始在应用程序中使用Tornado WebSockets,现在忽略了使用logging.basicConfig设置的格式和目标。 我所有的日志消息都被打印到stdout(而不是我的目标日志文件),格式是Tornado的(而不是我自己的)。 我该如何解决?

要将日志定向到日志文件,请按以下方式运行Tornado:

python app.py --log_file_prefix=mylog.log

更新:如以下注释中所述,这可能是设置日志文件的更好方法:

tornado.options.options['log_file_prefix'].set('mylog.log')
tornado.options.parse_command_line()

我尝试了一种解决方案,以在stdout流级别覆盖龙卷风记录器的默认格式(似乎可以与这三种记录器一起使用:app_log,gen_log,access_log):

import logging
from tornado.log import app_log, gen_log, access_log, LogFormatter

# define your new format, for instance :
my_log_format = '%(color)s::: %(levelname)s %(name)s %(asctime)s ::: %(module)s:%(lineno)d in %(funcName)s :::%(end_color)s\
                 \n %(message)s\n' 

# create an instance of tornado formatter, just overriding the 'fmt' arg
my_log_formatter = LogFormatter(fmt=my_log_format, color=True)

# get the parent logger of all tornado loggers :
root_logger     = logging.getLogger()

# set your format to root_logger
root_streamhandler = root_logger.handlers[0]
root_streamhandler.setFormatter(my_log_formatter)

...然后使用龙卷风的任何记录流,例如:

### let's say we log from your 'main.py' file in an '__init__' function : 

app_log.info('>>> this is app_log')
gen_log.info('>>> this is gen_log ')
access_log.info('>>> this is access_log ')

...而不是默认的stdout:

[I 180318 21:14:35 main:211] >>> this is app_log 
[I 180318 21:14:35 main:212] >>> this is gen_log 
[I 180318 21:14:35 main:213] >>> this is access_log 

...您将以自己的格式获得标准输出:

::: INFO tornado.application 180318 21:14:44 ::: main:211 in __init__ :::                   
>>> this is app_log 

::: INFO tornado.general 180318 21:14:44 ::: main:212 in __init__ :::                               
>>> this is gen_log 

::: INFO tornado.access 180318 21:14:44 ::: main:213 in __init__ :::                                
 >>> this is access_log 

我知道此解决方案不能直接回答您的basicConfig问题,但可以帮助我猜测...

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM