Add logger config

This commit is contained in:
Tyler Perkins 2023-06-12 18:05:36 -04:00
parent 95936798d1
commit f9f1982442
7 changed files with 124 additions and 2 deletions

37
src/config.py Normal file
View File

@ -0,0 +1,37 @@
import envyaml
import os
import logging
from logging import Formatter, StreamHandler
config = envyaml.EnvYAML(os.environ.get('CONFIG_PATH', 'config.yaml'))
def initLogger() -> None:
"""
Initializes the logger
"""
# style the logger output to look like uvicorn for root app
FORMAT = '%(asctime)-15s %(levelname)s %(message)s'
DATE_FORMAT = '%b %d %H:%M:%S'
formatter = Formatter(fmt=FORMAT, datefmt=DATE_FORMAT)
handler = StreamHandler()
handler.setFormatter(formatter)
logger = logging.getLogger()
# remove default handler
logger.handlers = []
logger.addHandler(handler)
if config["app.log.level"] == 'debug':
logging.basicConfig(level=logging.DEBUG)
elif config["app.log.level"] == 'info':
logging.basicConfig(level=logging.INFO)
elif config["app.log.level"] == 'warning':
logging.basicConfig(level=logging.WARNING)
elif config["app.log.level"] == 'error':
logging.basicConfig(level=logging.ERROR)
elif config["app.log.level"] == 'critical':
logging.basicConfig(level=logging.CRITICAL)
else:
logging.basicConfig(level=logging.INFO)
logging.warning("Invalid log level. Using INFO as default")

6
src/config.yaml Normal file
View File

@ -0,0 +1,6 @@
weaviate:
url: "localhost:"
app:
log:
level: ""

0
src/internal/__init__.py Normal file
View File

0
src/internal/chain.py Normal file
View File

View File

@ -0,0 +1,3 @@
from langchain.text_splitter import SpacyTextSplitter

View File

@ -1,8 +1,24 @@
from fastapi import FastAPI
from fastapi import FastAPI, status
from fastapi.responses import RedirectResponse
from routers import question
from routers import question, documents
import logging
from config import config, initLogger
initLogger()
app = FastAPI()
app.include_router(question.router)
app.include_router(documents.router)
logging.warn("Test message")
@app.get("/")
async def root():
"""
Redirects to the docs page
"""
return RedirectResponse(url="/docs", status_code=status.HTTP_303_SEE_OTHER)

60
src/routers/documents.py Normal file
View File

@ -0,0 +1,60 @@
from fastapi import APIRouter
from pydantic import BaseModel
from enum import Enum
router = APIRouter(
prefix="/documents",
tags=["documents"],
responses={404: {"description": "Not found"}},
)
class Document(BaseModel):
id: int
title: str
content: str
# Document Type enum
class DocumentType(str, Enum):
markdown = "md"
#html = "html"
#pdf = "pdf"
epub = "epub"
odt = "odt"
docx = "docx"
@router.get("/{document_type}")
async def read_documents(document_type: DocumentType):
"""
Get all documents
"""
pass
@router.get("/{document_id}")
async def read_document(document_id: int):
"""
Get a specific document
"""
pass
@router.post("/")
async def create_document(document: Document):
"""
Create a new document
"""
pass
@router.put("/{document_id}")
async def update_document(document_id: int, document: Document):
"""
Update a document
"""
pass
@router.delete("/{document_id}")
async def delete_document(document_id: int):
"""
Delete a document
"""
pass