Skip to content

Commit

Permalink
add logging etc
Browse files Browse the repository at this point in the history
  • Loading branch information
jsstevenson committed Jan 2, 2025
1 parent 9197e78 commit 92765e5
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 3 deletions.
8 changes: 8 additions & 0 deletions python/hooks/post_gen_project.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,16 @@
"""Provide hooks to run after project is generated."""

from pathlib import Path
import shutil


if not {{ cookiecutter.add_docs }}:
shutil.rmtree("docs")
Path(".readthedocs.yaml").unlink()


if not {{ cookiecutter.add_fastapi }}:
Path("tests/test_api.py").unlink()
Path("src/{{ cookiecutter.project_slug }}/api.py").unlink()
Path("src/{{ cookiecutter.project_slug }}/models.py").unlink()
Path("src/{{ cookiecutter.project_slug }}/logging.py").unlink()
Original file line number Diff line number Diff line change
@@ -1,16 +1,27 @@
"""Define API endpoints."""

from enum import Enum
from contextlib import asynccontextmanager
from typing import AsyncGenerator

from fastapi import FastAPI

from {{ cookiecutter.project_slug }} import __version__
from {{ cookiecutter.project_slug }}.models import ServiceInfo, ServiceOrganization, ServiceType
from {{ cookiecutter.project_slug }}.config import config
from {{ cookiecutter.project_slug }}.logging import initialize_logs


# TODO add logging configuration
# make this module add to main
@asynccontextmanager
async def lifespan(app: FastAPI) -> AsyncGenerator:
"""Perform operations that interact with the lifespan of the FastAPI instance.
See https://fastapi.tiangolo.com/advanced/events/#lifespan.
:param app: FastAPI instance
"""
initialize_logs()
yield


class _Tag(str, Enum):
Expand Down Expand Up @@ -52,7 +63,6 @@ def service_info() -> ServiceInfo:
:return: conformant service info description
"""

return ServiceInfo(
organization=ServiceOrganization(),
type=ServiceType(),
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
"""Configure application logging."""

import logging


def initialize_logs(log_level: int = logging.DEBUG) -> None:
"""Configure logging.
:param log_level: global log level to set
"""
log_filename = f"{__package__}.log"
logging.basicConfig(
filename=log_filename,
format="[%(asctime)s] - %(name)s - %(levelname)s : %(message)s",
)
logger = logging.getLogger(__package__)
logger.setLevel(log_level)

0 comments on commit 92765e5

Please sign in to comment.