Skip to content

Commit

Permalink
Improve logging setup
Browse files Browse the repository at this point in the history
  • Loading branch information
greyli committed Jun 12, 2024
1 parent bcf6334 commit 4576c95
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 16 deletions.
22 changes: 10 additions & 12 deletions greybook/core/logging.py
Original file line number Diff line number Diff line change
@@ -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):
Expand All @@ -21,25 +20,24 @@ 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)

mail_handler = SMTPHandler(
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)

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)
11 changes: 7 additions & 4 deletions greybook/settings.py
Original file line number Diff line number Diff line change
@@ -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')
Expand Down Expand Up @@ -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):
Expand All @@ -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 = {
Expand Down

0 comments on commit 4576c95

Please sign in to comment.