Skip to content

Instantly share code, notes, and snippets.

@kerma
Created August 3, 2016 09:01
Show Gist options
  • Save kerma/a43526cc6f2ff1f07895b0b215d05931 to your computer and use it in GitHub Desktop.
Save kerma/a43526cc6f2ff1f07895b0b215d05931 to your computer and use it in GitHub Desktop.
Tornado access log split to STDOUT
import sys
import logging
import tornado.httpserver
import tornado.log
import tornado.options
import tornado.web
logger = logging.getLogger(__name__)
tornado.options.define("access_to_stdout", default=False, help="Log tornado.access to stdout")
class MainHandler(tornado.web.RequestHandler):
def get(self):
logger.debug("MainHandler.get")
logger.info("MainHandler.get")
logger.warning("MainHandler.get")
logger.error("MainHandler.get")
self.write("Hello world")
def init_logging(access_to_stdout=False):
if access_to_stdout:
access_log = logging.getLogger('tornado.access')
access_log.propagate = False
# make sure access log is enabled even if error level is WARNING|ERROR
access_log.setLevel(logging.INFO)
stdout_handler = logging.StreamHandler(sys.stdout)
access_log.addHandler(stdout_handler)
def bootstrap():
tornado.options.parse_command_line(final=True)
init_logging(tornado.options.options.access_to_stdout)
def start_server():
urls = [(r'/', MainHandler)]
application = tornado.web.Application(urls)
http_server = tornado.httpserver.HTTPServer(application)
http_server.listen(8888)
tornado.ioloop.IOLoop.instance().start()
if __name__ == '__main__':
bootstrap()
start_server()
[unix_http_server]
file=/usr/local/var/run/supervisor.sock ; (the path to the socket file)
[supervisord]
logfile=/usr/local/var/log/supervisord.log ; (main log file;default $CWD/supervisord.log)
logfile_maxbytes=50MB ; (max main logfile bytes b4 rotation;default 50MB)
logfile_backups=10 ; (num of main logfile rotation backups;default 10)
loglevel=info ; (log level;default info; others: debug,warn,trace)
pidfile=/usr/local/var/run/supervisord.pid ; (supervisord pidfile;default supervisord.pid)
nodaemon=true ; (start in foreground if true;default false)
minfds=1024 ; (min. avail startup file descriptors;default 1024)
minprocs=200 ; (min. avail process descriptors;default 200)
[rpcinterface:supervisor]
supervisor.rpcinterface_factory = supervisor.rpcinterface:make_main_rpcinterface
[supervisorctl]
serverurl=unix:///usr/local/var/run/supervisor.sock ; use a unix:// URL for a unix socket
[program:tornado]
directory = /full/path/to/example/
command = /full/path/to/example/venv/bin/python3 example.py --access_to_stdout --logging=debug
autorestart = true
stderr_logfile = application.log
stdout_logfile = access.log
@shubhpy
Copy link

shubhpy commented Jun 14, 2018

Thanks, Very good use of logging with the supervisor.
But it is logging print statements first and request line later.

Mozilla/5.0 (Linux; Android 7.0; Moto G (5) Plus Build/NPN25.137-67-5) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/67.0.3396.87 Mobile Safari/537.36
192.168.15.182
2018-06-14 10:20:57,574 web.py Line:2063 - INFO - 304 GET /login (192.168.15.182) 1.38ms

As in above my login get method, I am printing user-agent and remote_ip that is logged before the request.

Can I make it request and then print statements?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment