Skip to content

Commit

Permalink
Merge pull request #360 from LACMTA:2023-api-optimization
Browse files Browse the repository at this point in the history
Update logging configuration and dependencies
  • Loading branch information
albertkun authored Nov 13, 2023
2 parents c54b4de + 81e9fe0 commit a37aa09
Show file tree
Hide file tree
Showing 3 changed files with 5 additions and 20 deletions.
22 changes: 5 additions & 17 deletions fastapi/app/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
from distutils.command.config import config
from typing import Union, List, Dict, Optional
from versiontag import get_version
import queue

import http
import json
import requests
Expand Down Expand Up @@ -62,11 +62,7 @@
from .config import Config
from pathlib import Path

from pythonjsonlogger import jsonlogger
import logging.handlers

from logzio.handler import LogzioHandler

import logging
import typing as t

Expand Down Expand Up @@ -688,16 +684,12 @@ def read_user(username: str, db: Session = Depends(get_db),token: str = Depends(
async def get_all_routes():
return [route.path for route in app.routes]



@app.on_event("startup")
async def startup_event():
log_queue = queue.Queue(-1)
uvicorn_access_logger = logging.getLogger("uvicorn.access")
uvicorn_error_logger = logging.getLogger("uvicorn.error")
logger = logging.getLogger("uvicorn.app")

logzio_formatter = jsonlogger.JsonFormatter("%(message)s")
logzio_formatter = logging.Formatter("%(message)s")
logzio_uvicorn_access_handler = LogzioHandler(Config.LOGZIO_TOKEN, 'uvicorn.access', 5, Config.LOGZIO_URL)
logzio_uvicorn_access_handler.setLevel(logging.INFO)
logzio_uvicorn_access_handler.setFormatter(logzio_formatter)
Expand All @@ -710,18 +702,14 @@ async def startup_event():
logzio_app_handler.setLevel(logging.INFO)
logzio_app_handler.setFormatter(logzio_formatter)

uvicorn_access_logger.addHandler(logging.handlers.QueueHandler(log_queue))
uvicorn_error_logger.addHandler(logging.handlers.QueueHandler(log_queue))
logger.addHandler(logging.handlers.QueueHandler(log_queue))
uvicorn_access_logger.addHandler(logzio_uvicorn_access_handler)
uvicorn_error_logger.addHandler(logzio_uvicorn_error_handler)
logger.addHandler(logzio_app_handler)

uvicorn_access_logger.addFilter(LogFilter())
uvicorn_error_logger.addFilter(LogFilter())
logger.addFilter(LogFilter())

queue_listener = logging.handlers.QueueListener(
log_queue, logzio_uvicorn_access_handler, logzio_uvicorn_error_handler, logzio_app_handler
)
queue_listener.start()

app.add_middleware(
CORSMiddleware,
Expand Down
1 change: 0 additions & 1 deletion fastapi/app/requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ pandas
polyline
websockets
fastapi-cache2[redis]
python-json-logger
pymemcache
asyncio
asyncpg
Expand Down
2 changes: 0 additions & 2 deletions fastapi/requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,6 @@ sqlalchemy[asyncio]
versiontag
gitpython
fastapi-pagination
prometheus-fastapi-instrumentator
prometheus_client
geojson
pytest
asyncpg

0 comments on commit a37aa09

Please sign in to comment.