--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/logger.py Sun Jul 30 09:31:46 2017 +0000
+from datetime import datetime
+from os.path import abspath, dirname, join
+from tornado.ioloop import IOLoop
+from tornado.options import define, options
+from tornado.web import Application, HTTPError, RequestHandler
+class LogHandler(RequestHandler):
+ data = json.loads(self.request.body.decode('utf-8'))
+ raise HTTPError(400, 'Malformed JSON')
+ logdir = abspath(join(dirname(__file__), 'log'))
+ makedirs(logdir, exist_ok=True)
+ time = datetime.utcnow().strftime('%Y-%m-%dT%H:%M:%S.%fZ')
+ fn = join(logdir, time + '.json')
+ with open(fn, 'w') as f:
+ json.dump(data, f, ensure_ascii=False, indent=4, sort_keys=True)
+class Logger(Application):
+ def __init__(self, debug=False):
+ super().__init__(handlers, debug=debug)
+ def listen(self, port, address='', **kwargs):
+ name = self.__class__.__name__
+ logging.info('%s is serving on http://%s:%d/', name, address, port)
+ super().listen(port, address, **kwargs)
+ define('listen', metavar='IP', default='127.0.0.1')
+ define('port', metavar='PORT', default=8111, type=int)
+ define('xheaders', metavar='True|False', default=False, type=bool)
+ define('debug', metavar='True|False', default=False, type=bool)
+ options.parse_command_line()
+ application = Logger(options.debug)
+ application.listen(options.port, options.listen, xheaders=options.xheaders)
+ IOLoop.current().start()
+if __name__ == '__main__':