1. tornado logging使用的python內置的logging模塊
2. 在tornado/options.py 中定義了對logging配置項的一些定義(如果需要添加啟動參數,需要在對應的代碼中導入options模塊),以下是一些在options.py中定義的Option
-help
-logging = info|warning|error|none
-log_to_stderr = True|False
-log_file_prefix = your path
-log_file_max_size = int
-log_file_num_backups = int
3. 我們可以通過添加一啟動項-log_file_prefix=your complete log path,將整個webapp相關的日志文件寫入到指定文件中(同時需要添加如下代碼):
from tornado.options import define, options
tornado.options.parse_command_line()
4. 然后通過類似啟動命令:python helloworld.py -log_file_prefix=your ** path
由於tornado優良的可擴展性,我們可以同時啟動多個tornado server進程,這里我們提出這種需求場景,如何記錄各個端口的server日志?
相關代碼:
import logging
import tornado.httpserver
import tornado.ioloop
import tornado.web
from tornado.options import define, options
define("port", default=8083, help="Run server on a specific port", type=int)
class MainHandler(tornado.web.RequestHandler):
def get(self):
logging.info("**Request to MainHandler!")
self.write("Hello to the Tornado world!")
settings = {
"debug": True,
}
application = tornado.web.Application([
(r"/", MainHandler),
], **settings)
if __name__ == "__main__":
http_server = tornado.httpserver.HTTPServer(application)
'''
Get the option(s) from the startup command line if ever.
In this tutorial, we define own "port" option to change the
port via the command line, and then we can run multiple tornado
processes at different ports.
'''
tornado.options.parse_command_line()
# This line should be after the parse_command_line()
http_server.listen(options.port)
tornado.ioloop.IOLoop.instance().start()
1. 這里我們自定義一個可根據啟動參數來修改的監聽端口。
2. 通過-log_file_prefix選項,我們可以將輸出日志文件與端口號綁定,用於區分,相關的啟動命令類似:
python helloworld.py -port=8091 -log_file_prefix=your complete path/test_log@8091.log
python helloworld.py -port=8092 -log_file_prefix=your complete path/test_log@8092.log
通過以上方式,我們就可以記錄多個端口的相關日志