the source code shows that tornado uses a large number of logging modules in Python to process log operations. However, when processing logs, Tornado. Options. parse_command_line () sets the root log level to info! In tornado. options. before parse_command_line (), the Program sets the Log Level of logging, which may be rewritten by tornado.
The following Code shows that tornado has adjusted the logger level.
Import osimport sslimport loggingfrom tornado. httpserver import httpserverfrom tornado. web Import Application, requesthandlerfrom tornado. ioloop import ioloopimport tornado. optionsfrom tornado. options import define, optionslog = logging. getlogger (_ name _) define ("Port", default = 8000, help = "run on the given port", type = int) Class testhandler (requesthandler ): def get (Self): Self. write ("Hello, world! \ N ") Application = Application ([(R"/", testhandler),], DEBUG = true) If _ name _ =" _ main __": print "Main 1 getpolictivelevel =", log. geteffectivelevel () print "tornado default Log Level =" tornado. options. options. logging tornado. options. options. logging = "debug" tornado. options. parse_command_line () print "Main getasktivelevel =", log. geteffectivelevel () # Server = httpserver (application, ssl_options = {# "certfile": OS. path. join (OS. path. abspath (". ")," cert. PEM "), #" Keyfile ": OS. path. join (OS. path. abspath (". ")," privatekey. PEM "), #}) Server = httpserver (applicatio) server. listen (options. port) ioloop. instance (). start ()
The main change to the log level of the root logger is the tornado. Options of Tornado. The source code is as follows:
Def parse_command_line (self, argS = none): If ARGs is none: ARGs = sys. argv remaining = [] For I in xrange (1, Len (ARGs): # All things after the last option are command line arguments if not ARGs [I]. startswith ("-"): remaining = ARGs [I:] Break if ARGs [I] = "--": remaining = ARGs [I + 1:] break Arg = ARGs [I]. lstrip ("-") Name, equals, value = Arg. partition ("=") name = Name. replace ('-', '_') if not name In self: print_help () Raise error ('unrecognized command line option: % R' % name) option = self [name] if not equals: if option. type = bool: value = "true" else: Raise error ('option % R requires a value' % name) option. parse (value) If self. help: print_help () sys. exit (0) # Set up Log Level and pretty console logging by default if self. logging! = 'None': logging. getlogger (). setlevel (getattr (logging, self. Logging. Upper () enable_pretty_logging () return remaining
# Default optionsdefine ("help", type = bool, help = "show this help information") define ("logging", default = "info ", help = ("set the python log level. if 'none', Tornado won' t touch the "" logging configuration. "), metavar =" Debug | info | warning | error | none ")
We can see that tornado. options determines self when parse_command_line. whether logging is none. If it is not none, logging is executed. getlogger (). setlevel (getattr (logging, self. logging. upper (), that is, to set the level of the root logger, in tornado. options defines a logging when being imported, and sets the root level of logging to info when parse_command_line is used.
In short, in tornado applications, you should pay special attention to the order of the logging level setting with tornado. Options. parse_command_line.