Custom Tornado log Format

Source: Internet
Author: User

This article shares with you the main isTornado log Format customization related content, take a look at it, I hope you learn Python help.play for the first timeTornado. Version4.x. In order to solve the problem of log format,GoogleA lot, not a valid one. TornadoThe log format is divided into two pieces, one isLoggingthe format, a piece isTornadoRequest message format. TornadoThe default Access log output is this: warning:tornado.access:404 GET/(127.0.0.1) 167.93msamong them,warning:tornado.access:404to beLoggerlog content,GET/(127.0.0.1) 167.93msto access the message. according toEnable_pretty_loggingfunction,TornadoDefault AdoptionLoggingof theRoot Logger, and, ifLoggerNot ConfiguredHandler, a default is added for it.Streamhandler. Therefore, it should beLogging Root LoggerPre-createdHandler, and specify the log format. based onTornadodocument, the output access log isApplication.log_request, if you want to customize the access log output, you can inheritApplicationand rewritelog_request, or customizelog_requestfunction, and set toApplication.settingsof theLog_functionproperty. of course choose the latter. Log_functionthe notation of the function, referring to the originalApplication.log_requeston the line. Finally, I put my code forGoogleto this friend reference: Import Loggingimport Tornado.log #Log Format Logging.getlogger (). SetLevel (Logging.info) Formatter = logging. Formatter (fmt= "% (levelname). 4s% (asctime) s% (name) s% (message) s", datefmt= "%y-%m-%d%h:%m:%s") Handler = logging. Streamhandler () Handler.setformatter (Formatter) Logging.getlogger (). AddHandler (Handler) #Log Message Formatdef log_request (handler): If Handler.get_status () < 400: Log_method = Tornado.log.access_log.info Elif Handler.get_status () < 500: Log_method = tornado.log.access_log.warning Else Log_method = Tornado.log.access_log.error Request_time = 1000.0 * Handler.request.request_time () Log_method ("%d%s%.2fms%s", Handler.get_status (), Handler._request_summary (), Request_time, Handler.request.headers.get ("User-agent", "" ")) #CustomTornadoLog Message Format app.settings["log_function"] = Log.log_request

Source:the Barbarian People

Custom Tornado log Format

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.