Skip to content

Commit

Permalink
Config service app from env vars
Browse files Browse the repository at this point in the history
  • Loading branch information
kagami-l committed Jul 11, 2024
1 parent ab7e71d commit a6fc258
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 24 deletions.
4 changes: 2 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -22,5 +22,5 @@ run-dev-svc:
@uvicorn devchat._service.main:api_app --reload --port 22222

run-svc:
@echo "Running service on port 22222..."
@gunicorn devchat._service.main:api_app --workers 4 --worker-class uvicorn.workers.UvicornWorker --bind 0.0.0.0:22222
@echo "Running service..."
@python devchat/_service/main.py
19 changes: 19 additions & 0 deletions devchat/_service/config.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
from typing import Optional

from pydantic import BaseSettings


class Settings(BaseSettings):
PORT: int = 22222
WORKERS: int = 3
WORKSPACE: Optional[str] = None
LOG_LEVEL: str = "INFO"
LOG_FILE: Optional[str] = "dc_svc.log"
JSON_LOGS: bool = False

class Config:
env_prefix = "DC_SVC_"
case_sensitive = True


config = Settings()
Original file line number Diff line number Diff line change
@@ -1,14 +1,11 @@
import os
import logging
import sys

from gunicorn.app.base import BaseApplication
from gunicorn.glogging import Logger
from loguru import logger

LOG_LEVEL = logging.getLevelName(os.environ.get("LOG_LEVEL", "DEBUG"))
JSON_LOGS = True if os.environ.get("JSON_LOGS", "0") == "1" else False
WORKERS = int(os.environ.get("GUNICORN_WORKERS", "5"))
from devchat._service.config import config


class InterceptHandler(logging.Handler):
Expand All @@ -25,9 +22,7 @@ def emit(self, record):
frame = frame.f_back
depth += 1

logger.opt(depth=depth, exception=record.exc_info).log(
level, record.getMessage()
)
logger.opt(depth=depth, exception=record.exc_info).log(level, record.getMessage())


class StubbedGunicornLogger(Logger):
Expand All @@ -37,8 +32,8 @@ def setup(self, cfg):
self.error_logger.addHandler(handler)
self.access_logger = logging.getLogger("gunicorn.access")
self.access_logger.addHandler(handler)
self.error_logger.setLevel(LOG_LEVEL)
self.access_logger.setLevel(LOG_LEVEL)
self.error_logger.setLevel(config.LOG_LEVEL)
self.access_logger.setLevel(config.LOG_LEVEL)


class StandaloneApplication(BaseApplication):
Expand All @@ -62,3 +57,4 @@ def load(self):
return self.application


# https://pawamoy.github.io/posts/unify-logging-for-a-gunicorn-uvicorn-app/
43 changes: 30 additions & 13 deletions devchat/_service/main.py
Original file line number Diff line number Diff line change
@@ -1,19 +1,18 @@
from fastapi import FastAPI

from devchat._service.route import router

import logging
import os
import sys

from fastapi import FastAPI
from loguru import logger

from devchat._service.logger_util import (
from devchat._service.config import config
from devchat._service.custom_logging import (
InterceptHandler,
JSON_LOGS,
LOG_LEVEL,
WORKERS,
StandaloneApplication,
StubbedGunicornLogger,
)
from devchat._service.route import router
from devchat.workspace_util import get_workspace_chat_dir

api_app = FastAPI(
title="DevChat Local Service",
Expand All @@ -30,11 +29,11 @@
# https://github.com/miguelgrinberg/python-socketio/blob/main/examples/server/asgi/fastapi-fiddle.py


if __name__ == "__main__":
def main():
intercept_handler = InterceptHandler()
# logging.basicConfig(handlers=[intercept_handler], level=LOG_LEVEL)
# logging.root.handlers = [intercept_handler]
logging.root.setLevel(LOG_LEVEL)
logging.root.setLevel(config.LOG_LEVEL)

seen = set()
for name in [
Expand All @@ -50,15 +49,33 @@
seen.add(name.split(".")[0])
logging.getLogger(name).handlers = [intercept_handler]

logger.configure(handlers=[{"sink": sys.stdout, "serialize": JSON_LOGS}])
workspace_chat_dir = get_workspace_chat_dir(config.WORKSPACE)
log_file = os.path.join(workspace_chat_dir, config.LOG_FILE)

logger.configure(
handlers=[
{"sink": sys.stdout, "serialize": config.JSON_LOGS},
{
"sink": log_file,
"serialize": config.JSON_LOGS,
"rotation": "10 days",
"retention": "30 days",
"enqueue": True,
},
]
)

options = {
"bind": "0.0.0.0:22222",
"workers": WORKERS,
"bind": f"0.0.0.0:{config.PORT}",
"workers": config.WORKERS,
"accesslog": "-",
"errorlog": "-",
"worker_class": "uvicorn.workers.UvicornWorker",
"logger_class": StubbedGunicornLogger,
}

StandaloneApplication(api_app, options).run()


if __name__ == "__main__":
main()

0 comments on commit a6fc258

Please sign in to comment.