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