Created
June 23, 2018 13:42
-
-
Save Maecenas/9f78dd6d6545a1ee8064e7e7619fefc3 to your computer and use it in GitHub Desktop.
TimedRotatingLogger
This file contains hidden or 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
#!/usr/bin/env python | |
# coding: utf-8 | |
import logging.config | |
from logging.handlers import TimedRotatingFileHandler | |
from flask import Flask, request | |
app = Flask(__name__) | |
app.secret_key = "ecret_key" | |
LOG_FILE = "./logs/data.log" | |
LOG_FOTMATTER = logging.Formatter('%(asctime)s - %(name)s:%(lineno)s - %(levelname)s - %(message)s') | |
REQUEST_FORMATTER = "{} - {}" | |
@app.route('/') | |
def home(): | |
pass | |
@app.route("/data", methods=["POST"]) | |
def data_router(): | |
if request.method == 'POST': | |
host = request.headers["host"] | |
data = request.get_json() | |
if data: | |
app.logger.info(REQUEST_FORMATTER.format(host, data)) | |
return "OK", 201 | |
else: | |
return "No meaningful JSON body", 400 | |
if __name__ == "__main__": | |
handler = TimedRotatingFileHandler(LOG_FILE, when="midnight") | |
handler.setLevel(logging.DEBUG) | |
handler.setFormatter(LOG_FOTMATTER) | |
app.logger.addHandler(handler) | |
app.logger.setLevel(logging.DEBUG) | |
app.run(port=5000, debug=True) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment