Skip to content

Commit

Permalink
Improving logging + gunicorn
Browse files Browse the repository at this point in the history
  • Loading branch information
jurajmajer committed Jan 14, 2025
1 parent 528aa77 commit ce5c09f
Show file tree
Hide file tree
Showing 6 changed files with 55 additions and 4 deletions.
4 changes: 3 additions & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,7 @@ COPY ./requirements.txt /code/requirements.txt
RUN pip install --no-cache-dir --upgrade -r /code/requirements.txt

COPY ./app /code/app
COPY ./app/log_conf.k8s.yaml /code/app/log_conf.yaml

ENV PYTHONPATH=/code/app
CMD ["uvicorn", "app.api.main:app", "--host", "0.0.0.0", "--port", "8080", "--proxy-headers"]
CMD ["gunicorn", "--worker-class", "uvicorn.workers.UvicornWorker", "--bind", "0.0.0.0:8080", "--access-logfile", "/app-data/log/access.log", "--error-logfile", "/app-data/log/error.log", "app.api.main:app"]
13 changes: 13 additions & 0 deletions app/api/main.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
import logging
import os

import yaml
from fastapi import FastAPI, Response, Depends, BackgroundTasks
from sqlalchemy.orm import Session
from starlette.status import HTTP_204_NO_CONTENT
Expand All @@ -15,6 +19,15 @@
],
)

# Configure logging
log_config_file = 'app/log_conf.yaml'
if os.path.isfile(log_config_file):
with open(log_config_file, 'rt') as f:
config = yaml.safe_load(f.read())
logging.config.dictConfig(config)
logger = logging.getLogger(__name__)
logger.info('Starting API...')


@app.post('/sendEmail',
status_code=HTTP_204_NO_CONTENT,
Expand Down
2 changes: 1 addition & 1 deletion app/bl/email/email_composer.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@

from app.bl import util

log = logging.getLogger()
log = logging.getLogger(__name__)
template_root = util.read_env_var('TEMPLATE_ROOT')
email_content_root = util.read_env_var('EMAIL_CONTENT_ROOT')

Expand Down
2 changes: 1 addition & 1 deletion app/bl/orchestrator.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
from app.bl.email import email_composer
from app.db.models import EmailQueue

log = logging.getLogger()
log = logging.getLogger(__name__)


def orchestrate(db):
Expand Down
2 changes: 1 addition & 1 deletion app/bl/util.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import logging
import os

log = logging.getLogger()
log = logging.getLogger(__name__)


def read_env_var(env_name, log_error=True, default_value=None):
Expand Down
36 changes: 36 additions & 0 deletions app/log_conf.k8s.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
version: 1
disable_existing_loggers: False
formatters:
default:
# "()": uvicorn.logging.DefaultFormatter
format: '%(asctime)s - %(name)s - %(levelname)s - %(message)s'
access:
# "()": uvicorn.logging.AccessFormatter
format: '%(asctime)s - %(name)s - %(levelname)s - %(message)s'
handlers:
default:
formatter: default
class: logging.FileHandler
filename: /app-data/log/email-microservice.log
mode: a
access:
formatter: access
class: logging.FileHandler
filename: /app-data/log/email-microservice.log
mode: a
loggers:
uvicorn.error:
level: INFO
handlers:
- default
propagate: no
uvicorn.access:
level: INFO
handlers:
- access
propagate: no
root:
level: INFO
handlers:
- default
propagate: no

0 comments on commit ce5c09f

Please sign in to comment.