Skip to content

Commit

Permalink
[FEATURE] - make setup_logging independent
Browse files Browse the repository at this point in the history
  • Loading branch information
maxim-lixakov committed Dec 9, 2024
1 parent ffe0180 commit 1012a09
Show file tree
Hide file tree
Showing 8 changed files with 102 additions and 123 deletions.
147 changes: 74 additions & 73 deletions poetry.lock

Large diffs are not rendered by default.

3 changes: 3 additions & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,8 @@ worker = [
"pydantic-settings",
"sqlalchemy",
"sqlalchemy-utils",
"python-multipart",
"python-jose",
"celery",
"onetl",
"asgi-correlation-id",
Expand All @@ -108,6 +110,7 @@ worker = [
"uuid6",
"coloredlogs",
"python-json-logger",
"pyyaml",
]

scheduler = [
Expand Down
2 changes: 1 addition & 1 deletion syncmaster/backend/middlewares/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
from fastapi import FastAPI

from syncmaster.backend.middlewares.cors import apply_cors_middleware
from syncmaster.backend.middlewares.logging import setup_logging
from syncmaster.backend.middlewares.monitoring.metrics import (
apply_monitoring_metrics_middleware,
)
Expand All @@ -13,6 +12,7 @@
from syncmaster.backend.middlewares.session import apply_session_middleware
from syncmaster.backend.middlewares.static_files import apply_static_files
from syncmaster.backend.settings import ServerAppSettings as Settings
from syncmaster.settings.log import setup_logging


def apply_middlewares(
Expand Down
26 changes: 0 additions & 26 deletions syncmaster/backend/middlewares/logging.py

This file was deleted.

2 changes: 1 addition & 1 deletion syncmaster/scheduler/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@
import asyncio
import logging

from syncmaster.scheduler.logging import setup_logging
from syncmaster.scheduler.settings import SchedulerAppSettings as Settings
from syncmaster.scheduler.transfer_fetcher import TransferFetcher
from syncmaster.scheduler.transfer_job_manager import TransferJobManager
from syncmaster.settings.log import setup_logging

logger = logging.getLogger(__name__)

Expand Down
18 changes: 0 additions & 18 deletions syncmaster/scheduler/logging.py

This file was deleted.

20 changes: 20 additions & 0 deletions syncmaster/settings/log/__init__.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,33 @@
# SPDX-FileCopyrightText: 2023-2024 MTS PJSC
# SPDX-License-Identifier: Apache-2.0

import logging
import textwrap
from logging.config import dictConfig
from pathlib import Path
from typing import Literal

import yaml
from pydantic import BaseModel, Field

LOG_PATH = Path(__file__).parent.resolve()
logger = logging.getLogger(__name__)


class LoggingSetupError(Exception):
pass


def setup_logging(config_path: Path) -> None:
"""Parse file with logging configuration, and setup logging accordingly"""
if not config_path.exists():
raise OSError(f"Logging configuration file '{config_path}' does not exist")

try:
config = yaml.safe_load(config_path.read_text())
dictConfig(config)
except Exception as e:
raise LoggingSetupError(f"Error reading logging configuration '{config_path}'") from e


class LoggingSettings(BaseModel):
Expand Down
7 changes: 3 additions & 4 deletions syncmaster/worker/transfer.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,10 @@
from sqlalchemy import select
from sqlalchemy.orm import Session, selectinload

from syncmaster.backend.middlewares.logging import setup_logging
from syncmaster.backend.settings import ServerAppSettings as Settings
from syncmaster.db.models import AuthData, Run, Status, Transfer
from syncmaster.db.repositories.utils import decrypt_auth_data
from syncmaster.exceptions.run import RunNotFoundError
from syncmaster.settings.log import setup_logging
from syncmaster.worker import celery
from syncmaster.worker.controller import TransferController
from syncmaster.worker.settings import WorkerAppSettings
Expand All @@ -37,7 +36,7 @@ def run_transfer_task(self: Celery, run_id: int) -> None:
)


def run_transfer(session: Session, run_id: int, settings: Settings):
def run_transfer(session: Session, run_id: int, settings: WorkerAppSettings):
logger.info("Start transfer")
run = session.get(
Run,
Expand Down Expand Up @@ -86,7 +85,7 @@ def run_transfer(session: Session, run_id: int, settings: Settings):

@after_setup_logger.connect
def setup_loggers(*args, **kwargs):
setup_logging(Settings().logging.get_log_config_path())
setup_logging(WorkerAppSettings().logging.get_log_config_path())


@before_task_publish.connect()
Expand Down

0 comments on commit 1012a09

Please sign in to comment.