diff --git a/redstring/__init__.py b/redstring/__init__.py index 9a47f98..075e50d 100644 --- a/redstring/__init__.py +++ b/redstring/__init__.py @@ -1,2 +1,14 @@ +import logging + import redstring.parser -import redstring.server \ No newline at end of file +import redstring.server + +formatter = logging.Formatter('[{asctime}] [{process}] [{levelname}] {message}', '%Y-%m-%d %H:%M:%S %z', style='{') + +handler = logging.StreamHandler() +handler.setFormatter(formatter) +handler.setLevel(logging.DEBUG) + +logger = logging.getLogger(__name__) +logger.setLevel(logging.DEBUG) +logger.addHandler(handler) diff --git a/redstring/server.py b/redstring/server.py index a62baf4..6be5a28 100644 --- a/redstring/server.py +++ b/redstring/server.py @@ -3,6 +3,7 @@ Logic for serving a collection of documents through a web frontend. """ import argparse import json +import logging import os import random import string @@ -47,6 +48,8 @@ class LoginForm(FlaskForm): CONFIG_ENVVAR = 'REDSTRING_CONFIG' +logger = logging.getLogger(__name__) + app = Flask(__name__) app.secret_key = ''.join(random.choices(string.ascii_uppercase + string.digits, k=32)) @@ -60,10 +63,16 @@ def check_password(app, password): Checks if a password is correct """ password_file = app.config['password_file'] + if not os.path.isfile(password_file): + logger.debug('Authentication failed: no password file found') with open(password_file) as f: real_password = f.read().strip() correct = password == real_password del real_password + if correct: + logger.debug('Authentication successful') + else: + logger.debug('Authentication failed: password incorrect') return correct @@ -223,4 +232,6 @@ def cli(): def wsgi(): config_path = os.environ.get(CONFIG_ENVVAR) or '/etc/redstring.conf' config = read_config(app, config_path) + logger.setLevel(logging.DEBUG) + logger.debug(f'Loaded config from {config_path}: {config}') return app