Created
August 3, 2016 09:01
-
-
Save kerma/a43526cc6f2ff1f07895b0b215d05931 to your computer and use it in GitHub Desktop.
Tornado access log split to STDOUT
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
[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 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Thanks, Very good use of logging with the supervisor.
But it is logging print statements first and request line later.
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?