Skip to content

Commit

Permalink
fix: logging on gunicorn
Browse files Browse the repository at this point in the history
fixes #4
  • Loading branch information
vugonz committed Nov 17, 2024
1 parent c23671f commit 42111cf
Show file tree
Hide file tree
Showing 5 changed files with 19 additions and 18 deletions.
1 change: 0 additions & 1 deletion .env.example
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ ROLES_PATH="data/roles.json"
STATIC_DIR="data/static/"
MAX_FILE_UPLOAD_LENGTH="16777216"

LOG_LEVEL="INFO"
LOGS_PATH="data/logs/app.log"

ADMIN_USERNAME="admin"
Expand Down
2 changes: 1 addition & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,4 @@ EXPOSE 8000
RUN chmod u+x entrypoint.sh

ENTRYPOINT [ "./entrypoint.sh" ]
CMD ["gunicorn", "--bind", "0.0.0.0:8000", "app:create_app()"]
CMD ["gunicorn", "--bind", "0.0.0.0:8000", "app:create_app()", "--log-level", "info", "--access-logfile", "/hs-api/data/logs/access.log", "--error-logfile", "/hs-api/data/logs/error.log"]
26 changes: 15 additions & 11 deletions app/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
from app.extensions import roles_handler
from app.extensions import logos_handler


def create_app(config_class=Config):
flask_app = Flask(__name__)
flask_app.config.from_object(config_class)
Expand Down Expand Up @@ -76,19 +77,22 @@ def register_commands(app: Flask):
register_create_admin_user_command(app)

def setup_logger(app: Flask):
"""
If ran with gunicorn simply sets Flask logger to gunicorn ones, configured through cli arguments.
If ran with Flask development server simply logs at debug level or default to stdout. """
if app.debug:
app.logger.setLevel(logging.DEBUG)
return

# create logs folder
logs_path = app.config.get("LOGS_PATH")
if app.debug or logs_path == basedir: # don't set logger in debug or if not log file
return

levels = {"DEBUG": logging.DEBUG, "INFO": logging.INFO, "WARNING": logging.WARNING}

log_dir = os.path.dirname(logs_path)
if not os.path.exists(log_dir):
os.makedirs(log_dir)

app.logger.setLevel(levels[app.config.get("LOG_LEVEL")])
handler = logging.FileHandler(logs_path)
BASIC_FORMAT = "[%(asctime)s] %(levelname)s [%(name)s.%(funcName)s:%(lineno)d] %(message)s"
handler.setFormatter(logging.Formatter(BASIC_FORMAT))
app.logger.addHandler(handler)
logging.getLogger("werkzeug").addHandler(handler) # Root logger for all logs
# ran in gunicorn, use gunicorn handlers set through cli arguments
if __name__ != '__main__':
gunicorn_logger = logging.getLogger('gunicorn.error')
app.logger.handlers = gunicorn_logger.handlers
app.logger.setLevel(gunicorn_logger.level)
return
5 changes: 2 additions & 3 deletions app/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,12 +41,11 @@ class Config:
MAX_CONTENT_LENGTH = _get_int_env_or_default("MAX_FILE_UPLOAD_LENGTH", 16 * 1024 * 1024)

LOGS_PATH = os.path.join(basedir, _get_env_or_default("LOGS_PATH", ""))
LOG_LEVEL = _get_env_or_default("LOG_LEVEL", "INFO")

FRONTEND_ORIGIN = _get_env_or_default("FRONTEND_ORIGIN", "http://localhost:3000")

ADMIN_USERNAME = _get_env_or_default("ADMIN_USERNAME", "admin")
ADMIN_PASSWORD = _get_env_or_default("ADMIN_PASSWORD", "admin")
ADMIN_USERNAME = _get_env_or_default("ADMIN_USERNAME", "")
ADMIN_PASSWORD = _get_env_or_default("ADMIN_PASSWORD", "")

CLIENT_ID = _get_env_or_default("CLIENT_ID", "")
CLIENT_SECRET = _get_env_or_default("CLIENT_SECRET", "")
Expand Down
3 changes: 1 addition & 2 deletions docker-compose.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,7 @@ services:
- STATIC_DIR=data/static/
- ROLES_PATH=data/roles.json
- SESSION_DIR=data/flask_sessions/
- LOGS_PATH=data/logs/app.log
- LOG_LEVEL=INFO
- LOGS_PATH=data/logs/
- MAX_FILE_UPLOAD_LENGTH=16777216
- CLIENT_ID=
- CLIENT_SECRET=
Expand Down

0 comments on commit 42111cf

Please sign in to comment.