add log to file

This commit is contained in:
sergey 2024-10-09 20:34:38 +03:00
parent 15a3aeb6f5
commit 7d11cd5c50
4 changed files with 17 additions and 3 deletions

3
.gitignore vendored
View File

@ -3,7 +3,8 @@ __pycache__
.env
.vscode/
.venv/
.log
.log/
*.log
.idea/
*.idea
docker/local_redis_file/data

View File

@ -1,7 +1,9 @@
OAA_CFG__RUN__HOST=0.0.0.0
OAA_CFG__RUN__PORT=8000
OAA_CFG__RUN__RELOAD=True
OAA_CFG__LOG__LEVEL=30
OAA_CFG__LOG__LEVEL_TO_FILE=30
OAA_CFG__TOKEN__ADMIN=string
OAA_CFG__TOKEN__USER=string

View File

@ -22,9 +22,10 @@ class RunConfig(BaseModel):
class LogConfig(BaseModel):
level: int = 30
level_to_file: int = 30
dateformat: str = "%Y-%m-%d %H:%M:%S"
format: str = (
"[%(asctime)s.%(msecs)03d] %(module)-25s:%(lineno)4d | %(funcName)-20s| %(levelname)-8s | %(message)s"
"[%(asctime)s.%(msecs)03d] %(module)-15s:%(lineno)4d | %(funcName)-20s| %(levelname)-8s | %(message)s"
)
@ -87,8 +88,18 @@ class Settings(BaseSettings):
conf = Settings()
logging.basicConfig(
level=conf.log.level,
datefmt=conf.log.dateformat,
format=conf.log.format,
)
file_handler = logging.FileHandler(BASE_DIR / "logfile.log")
file_handler.setLevel(conf.log.level_to_file)
file_handler.setFormatter(
logging.Formatter(
"[%(asctime)s.%(msecs)03d] %(module)s:%(lineno)4d | %(funcName)s| %(levelname)s | %(message)s"
)
)
logging.getLogger().addHandler(file_handler)

View File

@ -20,7 +20,7 @@ main_app = FastAPI(
lifespan=lifespan,
)
log.warning("test")
main_app.include_router(router)
main_app.mount("/static", StaticFiles(directory=STATIC_DIR), name="static")