From 4576c953b5b299bf0c8afadb0e925090312b797d Mon Sep 17 00:00:00 2001 From: Grey Li Date: Wed, 12 Jun 2024 22:43:30 +0800 Subject: [PATCH] Improve logging setup --- greybook/core/logging.py | 22 ++++++++++------------ greybook/settings.py | 11 +++++++---- 2 files changed, 17 insertions(+), 16 deletions(-) diff --git a/greybook/core/logging.py b/greybook/core/logging.py index 9b97caa..619173c 100644 --- a/greybook/core/logging.py +++ b/greybook/core/logging.py @@ -1,14 +1,13 @@ import logging -import os from logging.handlers import SMTPHandler, RotatingFileHandler from flask import request -from flask.logging import default_handler - -from greybook.settings import basedir +from flask.logging import wsgi_errors_stream def register_logging(app): + formatter = logging.Formatter('[%(asctime)s] %(levelname)s in %(module)s: %(message)s') + class RequestFormatter(logging.Formatter): def format(self, record): @@ -21,10 +20,11 @@ def format(self, record): '%(levelname)s in %(module)s: %(message)s' ) - formatter = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s') - - file_handler = RotatingFileHandler(os.path.join(basedir, 'logs/greybook.log'), - maxBytes=10 * 1024 * 1024, backupCount=10) + logging_path = app.config['GREYBOOK_LOGGING_PATH'] + if logging_path == 'stream': + file_handler = logging.StreamHandler(wsgi_errors_stream) + else: + file_handler = RotatingFileHandler(logging_path, maxBytes=10 * 1024 * 1024, backupCount=10) file_handler.setFormatter(formatter) file_handler.setLevel(logging.INFO) @@ -32,7 +32,7 @@ def format(self, record): mailhost=app.config['MAIL_SERVER'], fromaddr=app.config['MAIL_USERNAME'], toaddrs=['ADMIN_EMAIL'], - subject='Greybook Application Error', + subject=app.config['GREYBOOK_ERROR_EMAIL_SUBJECT'], credentials=(app.config['MAIL_USERNAME'], app.config['MAIL_PASSWORD'])) mail_handler.setLevel(logging.ERROR) mail_handler.setFormatter(request_formatter) @@ -40,6 +40,4 @@ def format(self, record): if not app.debug: app.logger.addHandler(mail_handler) app.logger.addHandler(file_handler) - else: - app.logger.setLevel(logging.DEBUG) - app.logger.addHandler(default_handler) + app.logger.setLevel(logging.INFO) diff --git a/greybook/settings.py b/greybook/settings.py index aab0ad7..46df80f 100644 --- a/greybook/settings.py +++ b/greybook/settings.py @@ -1,7 +1,8 @@ import os import sys +from pathlib import Path -basedir = os.path.abspath(os.path.dirname(os.path.dirname(__file__))) +basedir = Path(__file__).resolve().parent.parent # SQLite URI compatible WIN = sys.platform.startswith('win') @@ -40,12 +41,14 @@ class BaseConfig: } GREYBOOK_SLOW_QUERY_THRESHOLD = 1 - GREYBOOK_UPLOAD_PATH = os.getenv('GREYBOOK_UPLOAD_PATH', os.path.join(basedir, 'uploads')) + GREYBOOK_UPLOAD_PATH = os.getenv('GREYBOOK_UPLOAD_PATH', basedir / 'uploads') GREYBOOK_ALLOWED_IMAGE_EXTENSIONS = ['png', 'jpg', 'jpeg', 'gif'] + GREYBOOK_LOGGING_PATH = os.getenv('GREYBOOK_LOGGING_PATH', basedir / 'logs/greybook.log') + GREYBOOK_ERROR_EMAIL_SUBJECT = '[Greybook] Application Error' class DevelopmentConfig(BaseConfig): - SQLALCHEMY_DATABASE_URI = prefix + os.path.join(basedir, 'data-dev.db') + SQLALCHEMY_DATABASE_URI = prefix + str(basedir / 'data-dev.db') class TestingConfig(BaseConfig): @@ -55,7 +58,7 @@ class TestingConfig(BaseConfig): class ProductionConfig(BaseConfig): - SQLALCHEMY_DATABASE_URI = os.getenv('DATABASE_URL', prefix + os.path.join(basedir, 'data.db')) + SQLALCHEMY_DATABASE_URI = os.getenv('DATABASE_URL', prefix + str(basedir / 'data.db')) config = {