Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add logging config #139

Merged
merged 4 commits into from
Sep 6, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions logs/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
*.log*
16 changes: 8 additions & 8 deletions studio/__main_unit__.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import argparse
import logging

import uvicorn
from fastapi import Depends, FastAPI, Request
Expand Down Expand Up @@ -71,6 +72,11 @@
templates = Jinja2Templates(directory=f"{FRONTEND_DIRPATH}/build")


@app.on_event("startup")
async def startup_event():
logging.info('"Studio" application startup complete.')


@app.get("/")
async def root(request: Request):
return templates.TemplateResponse("index.html", {"request": request})
Expand All @@ -83,19 +89,13 @@ def main(develop_mode: bool = False):
parser.add_argument("--reload", action="store_true")
args = parser.parse_args()

# set fastapi@uvicorn logging config.
log_format = "%(asctime)s %(levelprefix)s %(message)s"
fastapi_logging_config = uvicorn.config.LOGGING_CONFIG
fastapi_logging_config["formatters"]["default"]["fmt"] = log_format
fastapi_logging_config["formatters"]["access"]["fmt"] = log_format

if develop_mode:
reload_options = {"reload_dirs": ["studio"]} if args.reload else {}
uvicorn.run(
"studio.__main_unit__:app",
host=args.host,
port=args.port,
log_config=fastapi_logging_config,
log_config=f"{DIRPATH.CONFIG_DIR}/logging.yaml",
itutu-tienday marked this conversation as resolved.
Show resolved Hide resolved
reload=args.reload,
**reload_options,
)
Expand All @@ -104,6 +104,6 @@ def main(develop_mode: bool = False):
"studio.__main_unit__:app",
host=args.host,
port=args.port,
log_config=fastapi_logging_config,
log_config=f"{DIRPATH.CONFIG_DIR}/logging.yaml",
reload=False,
)
30 changes: 30 additions & 0 deletions studio/config/logging.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
version: 1
itutu-tienday marked this conversation as resolved.
Show resolved Hide resolved
disable_existing_loggers: true
formatters:
default:
(): "uvicorn.logging.DefaultFormatter"
fmt: "%(asctime)s %(levelprefix)s %(funcName)s():%(lineno)d - %(message)s"
access:
(): "uvicorn.logging.AccessFormatter"
fmt: "%(asctime)s %(levelprefix)s %(funcName)s():%(lineno)d - %(message)s"
handlers:
console:
class: logging.StreamHandler
level: DEBUG
formatter: default
rotating_file:
class: logging.handlers.TimedRotatingFileHandler
level: DEBUG
formatter: default
filename: logs/studio.log
when: midnight
interval: 1
backupCount: 365
loggers:
uvicorn:
level: INFO
handlers: [console, rotating_file]
propagate: false
root:
level: INFO
handlers: [console, rotating_file]