How to store Tornado logs to a file? How to store Tornado logs to a file? flask flask

How to store Tornado logs to a file?


You have used enable_pretty_logging which is good, and if you might note the documentation says you can pass in a logger. So what is a logger? Turns out Python has very extensive support for logging actions through the builtin logging module (which is mentioned in the documentation too). Generally, you need to set up handlers that write to some specific file, which you can do by

handler = logging.FileHandler(log_file_filename)logger.addHandler(handler)logger.setLevel(logging.INFO)logger.info('foo')

This will log all info level entries (or higher) into the file. These loggers can be gathered by the logging.getLogger function, and you can explicitly select these as per the tornado documentation by

access_log = logging.getLogger("tornado.access")app_log = logging.getLogger("tornado.application")gen_log = logging.getLogger("tornado.general")

Simply append your handler to the logger that is generating the messages you want to log to a file. If it's the tornado.application generating the messages you want to see

handler = logging.FileHandler(log_file_filename)app_log = logging.getLogger("tornado.application")enable_pretty_logging()app_log.addHandler(handler)

Or you can also use the builtin tornado options that enable this

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