Skip to content

Avoid uvicorn logger and use __main__ logger and remove logger from multiprocessing code #320

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

Merged
merged 8 commits into from
Jun 19, 2024
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 CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
- Updated PR template
- Generalised issue templates to make them more user-friendly for people outside our organisation
- Moved logging setup out of app lifespan and db initialisation logic
- Switch to __main__ logger and removed unused logger from multiprocessing code
### Fixed
- Updated version of external images used in GitHub actions

Expand Down
2 changes: 1 addition & 1 deletion src/chanjo2/crud/intervals.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
TranscriptTag,
)

LOG = logging.getLogger("uvicorn.access")
LOG = logging.getLogger(__name__)


def delete_intervals_for_build(
Expand Down
2 changes: 1 addition & 1 deletion src/chanjo2/crud/samples.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from typing import List, Optional, Tuple, Union
from typing import List, Optional, Tuple

from fastapi import HTTPException, status
from sqlalchemy import delete
Expand Down
2 changes: 1 addition & 1 deletion src/chanjo2/endpoints/coverage.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@
)

router = APIRouter()
LOG = logging.getLogger("uvicorn.access")
LOG = logging.getLogger(__name__)


@router.post("/coverage/d4/interval/", response_model=IntervalCoverage)
Expand Down
3 changes: 0 additions & 3 deletions src/chanjo2/endpoints/overview.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import logging
from os import path

from fastapi import APIRouter, Depends, HTTPException, Request, status
Expand All @@ -17,8 +16,6 @@
)
from chanjo2.models.pydantic_models import GeneCoverage, GeneReportForm, ReportQuery

LOG = logging.getLogger("uvicorn.access")


def get_templates_path() -> str:
"""Returns the absolute path to the templates folder of this app."""
Expand Down
2 changes: 1 addition & 1 deletion src/chanjo2/endpoints/report.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ def get_templates_path() -> str:
return path.join(APP_ROOT, "templates")


LOG = logging.getLogger("uvicorn.access")
LOG = logging.getLogger(__name__)
templates: Jinja2Templates = Jinja2Templates(directory=get_templates_path())
router = APIRouter()

Expand Down
18 changes: 10 additions & 8 deletions src/chanjo2/logger.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,15 @@

import uvicorn

LOG = logging.getLogger("uvicorn.access")
LOG = logging.getLogger(__name__)


console_formatter = uvicorn.logging.ColourizedFormatter(
"{levelprefix} {asctime} : {message}", style="{", use_colors=True
)
if LOG.handlers:
LOG.handlers[0].setFormatter(console_formatter)
else:
logging.basicConfig()
def configure_log():
"""Configure logging."""
console_formatter = uvicorn.logging.ColourizedFormatter(
"{levelprefix} {asctime} : {message}", style="{", use_colors=True
)
if LOG.handlers:
LOG.handlers[0].setFormatter(console_formatter)
else:
logging.basicConfig()
5 changes: 4 additions & 1 deletion src/chanjo2/main.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import logging
import os
from contextlib import asynccontextmanager
from typing import List, Tuple
Expand All @@ -8,10 +9,11 @@
from chanjo2 import __version__
from chanjo2.dbutil import engine
from chanjo2.endpoints import cases, coverage, intervals, overview, report, samples
from chanjo2.logger import LOG
from chanjo2.logger import configure_log
from chanjo2.models.sql_models import Base
from chanjo2.populate_demo import load_demo_data

LOG = logging.getLogger(__name__)
APP_ROUTER_TAGS: List[Tuple] = [
(intervals.router, "intervals"),
(coverage.router, "coverage"),
Expand All @@ -28,6 +30,7 @@ def create_db_and_tables():

@asynccontextmanager
async def lifespan(app_: FastAPI):
configure_log()
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Call a function that configures the log before doing the logging

LOG.info("Starting up...")
await startup_db()
yield
Expand Down
2 changes: 0 additions & 2 deletions src/chanjo2/meta/handle_completeness_tasks.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
import logging
import subprocess
from multiprocessing import Manager, Pool
from typing import Dict, List, Tuple

LOG = logging.getLogger("uvicorn.access")
INTERVAL_CHUNKS = 50
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This wasn't used anyway

CHROM_INDEX = 0
START_INDEX = 1
Expand Down
2 changes: 0 additions & 2 deletions src/chanjo2/meta/handle_d4.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import logging
import subprocess
import tempfile
from statistics import mean
Expand All @@ -17,7 +16,6 @@
TranscriptTag,
)

LOG = logging.getLogger("uvicorn.access")
CHROM_INDEX = 0
START_INDEX = 1
STOP_INDEX = 2
Expand Down
2 changes: 1 addition & 1 deletion src/chanjo2/meta/handle_load_intervals.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
TranscriptBase,
)

LOG = logging.getLogger("uvicorn.access")
LOG = logging.getLogger(__name__)
MAX_NR_OF_RECORDS = 10_000
END_OF_PARSED_FILE: str = "[success]"

Expand Down
3 changes: 0 additions & 3 deletions src/chanjo2/meta/handle_report_contents.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import logging
from collections import OrderedDict
from typing import Dict, List, Optional, Tuple, Union

Expand All @@ -22,8 +21,6 @@
TranscriptTag,
)

LOG = logging.getLogger("uvicorn.access")

INTERVAL_TYPE_SQL_TYPE: Dict[IntervalType, Union[SQLGene, SQLTranscript, SQLExon]] = {
IntervalType.GENES: SQLGene,
IntervalType.TRANSCRIPTS: SQLTranscript,
Expand Down
3 changes: 0 additions & 3 deletions src/chanjo2/models/pydantic_models.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import json
import logging
import os
from datetime import datetime
from enum import Enum
Expand All @@ -18,8 +17,6 @@
WRONG_COVERAGE_FILE_MSG,
)

LOG = logging.getLogger("uvicorn.access")


def default_report_coverage_levels() -> List[int]:
"""Sets the coverage thresholds to be used for report metrics whenever a request doesn't contain 'completeness_thresholds' values."""
Expand Down
Loading