Tornado uses the standard logging library, by default, and sends logs to STDOUT. Sometimes you may want logs stored in a database or flat-file.
Logging to flat-files
This method is fairly simple. You could pass the ‘log_file_prefix’ parameter via the command-line, or more elegantly add the options in your code:
If you have multiple instances of the same app running, I would keep the logs separate by using the port number:
Storing logs in a MongoDB collection
MongoLog is a really cool open-source centralized logging module for Python and MongoDB. It’s available on Github at https://github.com/andreisavu/mongodb-log.
Follow the installation docs in the README to get MongoLog all setup. After cloning and installing MongoLog, we need to modify our Tornado app.
First, import MongoLog:
Next we will override the ‘Application.log_request’ method to implement MongoLog.
After everything is up and running, you can view the raw logs in the database or use the web ui that ships with MongoLog:
