From e13a789f15e020d7250036da5f481545c7b1c92d Mon Sep 17 00:00:00 2001 From: James Wang Date: Sat, 26 Dec 2015 20:21:15 -0500 Subject: [PATCH] Add separate module for handling logs --- .gitignore | 1 + server/api/__init__.py | 1 + server/api/logger.py | 41 +++++++++++++++++++++++++++++++++++++++++ server/api/user.py | 5 ++--- server/app.py | 3 ++- 5 files changed, 47 insertions(+), 4 deletions(-) create mode 100644 server/api/logger.py diff --git a/.gitignore b/.gitignore index c9b09a2..66f8da0 100644 --- a/.gitignore +++ b/.gitignore @@ -1,6 +1,7 @@ .vagrant .secret_key .bundle/config +logs/ # Object files *.o diff --git a/server/api/__init__.py b/server/api/__init__.py index f0dd712..cd35ad4 100644 --- a/server/api/__init__.py +++ b/server/api/__init__.py @@ -1,4 +1,5 @@ import admin +import logger import models import user import utils diff --git a/server/api/logger.py b/server/api/logger.py new file mode 100644 index 0000000..5ba55a1 --- /dev/null +++ b/server/api/logger.py @@ -0,0 +1,41 @@ +import datetime +import logging +import logging.handlers +import os + +NOTSET = 0 +DEBUG = 10 +INFO = 20 +WARNING = 30 +ERROR = 40 +CRITICAL = 50 + +def initialize_logs(): + registration_logger = logging.getLogger("registrations") + login_logger = logging.getLogger("logins") + submission_logger = logging.getLogger("submissions") + + registration_logger.setLevel(logging.INFO) + login_logger.setLevel(logging.INFO) + submission_logger.setLevel(logging.INFO) + + base = os.path.dirname(__file__).strip("api") + log_path = os.path.join(base, "logs") + + if not os.path.exists(log_path): + os.mkdir(log_path) + + logs = [os.path.join(log_path, "registrations.log"), os.path.join(log_path, "logins.log"), os.path.join(log_path, "submissions.log")] + + registration_log = logging.handlers.RotatingFileHandler(logs[0], maxBytes=10000) + login_log = logging.handlers.RotatingFileHandler(logs[1], maxBytes=10000) + submission_log = logging.handlers.RotatingFileHandler(logs[2], maxBytes=10000) + + registration_logger.addHandler(registration_log) + login_logger.addHandler(login_log) + submission_logger.addHandler(submission_log) + +def log(log, level, message): + logger = logging.getLogger(log) + message = "[%s] %s" % (datetime.datetime.now().strftime("%m/%d/%Y %X"), message) + logger.log(level, message) diff --git a/server/api/user.py b/server/api/user.py index 17de908..ca7103b 100644 --- a/server/api/user.py +++ b/server/api/user.py @@ -4,7 +4,7 @@ from flask import current_app as app from models import db, Users from decorators import api_wrapper -import logging +import logger import requests import utils @@ -39,8 +39,7 @@ def user_register(): return { "success": 0, "message": "Email has already been used." } add_user(name, username, email, password) - logger = logging.getLogger("regs") - logger.warn("[{0}] {1} registered with {2}".format(time.strftime("%m/%d/%Y %X"), name.encode("utf-8"), email.encode("utf-8"))) + logger.log("registrations", logger.INFO, "%s registered with %s" % (name.encode("utf-8"), email.encode("utf-8"))) return { "success": 1, "message": "Success!" } diff --git a/server/app.py b/server/app.py index e5d34df..b4ce5f3 100644 --- a/server/app.py +++ b/server/app.py @@ -1,9 +1,9 @@ from argparse import ArgumentParser from flask import Flask +import api import config import json -import api app = Flask(__name__) @@ -18,6 +18,7 @@ app.config["SQLALCHEMY_TRACK_MODIFICATIONS"] = config.SQLALCHEMY_TRACK_MODIFICAT app.register_blueprint(api.admin.blueprint, url_prefix="/api/admin") app.register_blueprint(api.user.blueprint, url_prefix="/api/user") +api.logger.initialize_logs() @app.route("/api") def api_main():