Skip to content

Commit

Permalink
Use a standard location for the default logging config
Browse files Browse the repository at this point in the history
  • Loading branch information
edgarrmondragon committed Jul 18, 2024
1 parent 5415876 commit 4a6f55b
Show file tree
Hide file tree
Showing 4 changed files with 71 additions and 64 deletions.
18 changes: 18 additions & 0 deletions docs/implementation/logging.md
Original file line number Diff line number Diff line change
Expand Up @@ -77,3 +77,21 @@ This will send metrics to a `metrics.log`:
2022-09-29 00:48:53,302 INFO METRIC: {"metric_type": "timer", "metric": "sync_duration", "value": 0.5258760452270508, "tags": {"stream": "countries", "context": {}, "status": "succeeded"}}
2022-09-29 00:48:53,303 INFO METRIC: {"metric_type": "counter", "metric": "record_count", "value": 250, "tags": {"stream": "countries", "context": {}}}
```
## For package developers
If you're developing a tap or target package, you can put a `default_loggging.yml` file in the package root to set the default logging configuration for your package. This file will be used if the `SINGER_SDK_LOG_CONFIG` environment variable is not set:
```
.
├── README.md
├── poetry.lock
├── pyproject.toml
└── tap_example
    ├── __init__.py
    ├── __main__.py
    ├── default_logging.yml # <-- This file will be used if SINGER_SDK_LOG_CONFIG is not set
    ├── client.py
    ├── streams.py
    └── tap.py
```
52 changes: 0 additions & 52 deletions singer_sdk/logger.py

This file was deleted.

52 changes: 52 additions & 0 deletions singer_sdk/metrics.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,21 @@
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 import types
from singer_sdk.helpers._compat import Traversable


DEFAULT_LOG_INTERVAL = 60.0
Expand Down Expand Up @@ -372,3 +379,48 @@ 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.
"""
filename = "default_logging.yml"
path = get_package_files(__package__) / filename
if path.is_file():
return _load_yaml_logging_config(path)

path = get_package_files("singer_sdk").joinpath("default_logging.yml")
return _load_yaml_logging_config(path)

Check warning on line 409 in singer_sdk/metrics.py

View check run for this annotation

Codecov / codecov/patch

singer_sdk/metrics.py#L408-L409

Added lines #L408 - L409 were not covered by tests


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: 1 addition & 12 deletions singer_sdk/plugin_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
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 @@ -24,7 +23,6 @@
)
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 @@ -164,7 +162,7 @@ def __init__(
if self._is_secret_config(k):
config_dict[k] = SecretString(v)
self._config = config_dict
singer_logger.setup_logging(self.config, self.get_default_logging_config())
metrics._setup_logging(self.config) # noqa: SLF001
self.metrics_logger = metrics.get_metrics_logger()

self._validate_config(raise_errors=validate_config)
Expand All @@ -180,15 +178,6 @@ 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 4a6f55b

Please sign in to comment.