Skip to content

Commit

Permalink
feat: Developers can now customize the default logging configuration …
Browse files Browse the repository at this point in the history
…for their taps/targets by overriding the `get_default_logging_config` method

Related:

* Closes #1373
  • Loading branch information
edgarrmondragon committed May 16, 2024
1 parent 5e76655 commit ba45a4d
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 49 deletions.
52 changes: 52 additions & 0 deletions singer_sdk/logger.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
"""Logging configuration for the Singer SDK."""

from __future__ import annotations

import logging
import os
import typing as t
from pathlib import Path

import yaml

from singer_sdk.metrics import METRICS_LOG_LEVEL_SETTING, METRICS_LOGGER_NAME

if t.TYPE_CHECKING:
from singer_sdk.helpers._compat import Traversable

__all__ = ["setup_logging"]


def load_yaml_logging_config(path: Traversable | Path) -> t.Any: # noqa: ANN401
"""Load the logging config from the YAML file.
Args:
path: A path to the YAML file.
Returns:
The logging config.
"""
with path.open() as f:
return yaml.safe_load(f)


def setup_logging(
config: t.Mapping[str, t.Any],
default_logging_config: dict[str, t.Any],
) -> None:
"""Setup logging.
Args:
default_logging_config: A default
:py:std:label:`Python logging configuration dictionary`.
config: A plugin configuration dictionary.
"""
logging.config.dictConfig(default_logging_config)

config = config or {}
metrics_log_level = config.get(METRICS_LOG_LEVEL_SETTING, "INFO").upper()
logging.getLogger(METRICS_LOGGER_NAME).setLevel(metrics_log_level)

if "SINGER_SDK_LOG_CONFIG" in os.environ: # pragma: no cover
log_config_path = Path(os.environ["SINGER_SDK_LOG_CONFIG"])
logging.config.dictConfig(load_yaml_logging_config(log_config_path))
48 changes: 0 additions & 48 deletions singer_sdk/metrics.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,21 +7,13 @@
import json
import logging
import logging.config
import os
import typing as t
from dataclasses import dataclass, field
from pathlib import Path
from time import time

import yaml

from singer_sdk.helpers._resources import get_package_files

if t.TYPE_CHECKING:
from types import TracebackType

from singer_sdk.helpers._compat import Traversable

DEFAULT_LOG_INTERVAL = 60.0
METRICS_LOGGER_NAME = __name__
METRICS_LOG_LEVEL_SETTING = "metrics_log_level"
Expand Down Expand Up @@ -377,43 +369,3 @@ def sync_timer(stream: str, **tags: t.Any) -> Timer:
"""
tags[Tag.STREAM] = stream
return Timer(Metric.SYNC_DURATION, tags)


def _load_yaml_logging_config(path: Traversable | Path) -> t.Any: # noqa: ANN401
"""Load the logging config from the YAML file.
Args:
path: A path to the YAML file.
Returns:
The logging config.
"""
with path.open() as f:
return yaml.safe_load(f)


def _get_default_config() -> t.Any: # noqa: ANN401
"""Get a logging configuration.
Returns:
A logging configuration.
"""
log_config_path = get_package_files("singer_sdk").joinpath("default_logging.yml")
return _load_yaml_logging_config(log_config_path)


def _setup_logging(config: t.Mapping[str, t.Any]) -> None:
"""Setup logging.
Args:
config: A plugin configuration dictionary.
"""
logging.config.dictConfig(_get_default_config())

config = config or {}
metrics_log_level = config.get(METRICS_LOG_LEVEL_SETTING, "INFO").upper()
logging.getLogger(METRICS_LOGGER_NAME).setLevel(metrics_log_level)

if "SINGER_SDK_LOG_CONFIG" in os.environ:
log_config_path = Path(os.environ["SINGER_SDK_LOG_CONFIG"])
logging.config.dictConfig(_load_yaml_logging_config(log_config_path))
13 changes: 12 additions & 1 deletion singer_sdk/plugin_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
import click
from jsonschema import Draft7Validator

import singer_sdk.logger as singer_logger
from singer_sdk import about, metrics
from singer_sdk.cli import plugin_cli
from singer_sdk.configuration._dict_config import (
Expand All @@ -23,6 +24,7 @@
)
from singer_sdk.exceptions import ConfigValidationError
from singer_sdk.helpers._classproperty import classproperty
from singer_sdk.helpers._resources import get_package_files
from singer_sdk.helpers._secrets import SecretString, is_common_secret_key
from singer_sdk.helpers._util import read_json_file
from singer_sdk.helpers.capabilities import (
Expand Down Expand Up @@ -162,7 +164,7 @@ def __init__(
if self._is_secret_config(k):
config_dict[k] = SecretString(v)
self._config = config_dict
metrics._setup_logging(self.config) # noqa: SLF001
singer_logger.setup_logging(self.config, self.get_default_logging_config())
self.metrics_logger = metrics.get_metrics_logger()

self._validate_config(raise_errors=validate_config)
Expand All @@ -178,6 +180,15 @@ def setup_mapper(self) -> None:
logger=self.logger,
)

def get_default_logging_config(self) -> t.Any: # noqa: ANN401, PLR6301
"""Get a default logging configuration for the plugin.
Returns:
A logging configuration.
"""
log_config_path = get_package_files("singer_sdk") / "default_logging.yml"
return singer_logger.load_yaml_logging_config(log_config_path)

@property
def mapper(self) -> PluginMapper:
"""Plugin mapper for this tap.
Expand Down

0 comments on commit ba45a4d

Please sign in to comment.