Skip to content

Commit

Permalink
Use logger instead of click.secho
Browse files Browse the repository at this point in the history
  • Loading branch information
edgarrmondragon committed Nov 17, 2023
1 parent 96b8193 commit 383ec52
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 7 deletions.
10 changes: 8 additions & 2 deletions samples/sample_tap_countries/countries_tap.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,20 @@
CountriesStream,
)
from singer_sdk import Stream, Tap
from singer_sdk.typing import PropertiesList
from singer_sdk.typing import PropertiesList, Property, StringType


class SampleTapCountries(Tap):
"""Sample tap for Countries GraphQL API."""

name: str = "sample-tap-countries"
config_jsonschema = PropertiesList().to_dict()
config_jsonschema = PropertiesList(
Property(
"required",
StringType,
required=True,
),
).to_dict()

def discover_streams(self) -> list[Stream]:
"""Return a list of discovered streams."""
Expand Down
25 changes: 21 additions & 4 deletions singer_sdk/plugin_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,22 @@ def __init__(self) -> None:
class SingerCommand(click.Command):
"""Custom click command class for Singer packages."""

def __init__(
self,
*args: t.Any,
logger: logging.Logger,
**kwargs: t.Any,
) -> None:
"""Initialize the command.
Args:
*args: Positional `click.Command` arguments.
logger: A logger instance.
**kwargs: Keyword `click.Command` arguments.
"""
super().__init__(*args, **kwargs)
self.logger = logger

def invoke(self, ctx: click.Context) -> t.Any: # noqa: ANN401
"""Invoke the command, capturing warnings and logging them.
Expand All @@ -89,7 +105,7 @@ def invoke(self, ctx: click.Context) -> t.Any: # noqa: ANN401
return super().invoke(ctx)
except ConfigValidationError as exc:
for error in exc.errors:
click.secho(f"Error: {error}", err=True)
self.logger.error("Config validation error: %s", error)
sys.exit(1)


Expand Down Expand Up @@ -171,12 +187,12 @@ def __init__(
if self._is_secret_config(k):
config_dict[k] = SecretString(v)
self._config = config_dict
self._validate_config(raise_errors=validate_config)
self._mapper: PluginMapper | None = None

metrics._setup_logging(self.config)
self.metrics_logger = metrics.get_metrics_logger()

self._validate_config(raise_errors=validate_config)
self._mapper: PluginMapper | None = None

# Initialization timestamp
self.__initialized_at = int(time.time() * 1000)

Expand Down Expand Up @@ -601,6 +617,7 @@ def get_singer_command(cls: type[PluginBase]) -> click.Command:
is_eager=True,
),
],
logger=cls.logger,
)

@plugin_cli
Expand Down
1 change: 0 additions & 1 deletion tests/core/test_tap_class.py
Original file line number Diff line number Diff line change
Expand Up @@ -90,4 +90,3 @@ def test_cli_discover(tap_class: type[Tap], tmp_path):
)
assert result.exit_code == 0
assert "streams" in json.loads(result.stdout)
assert not result.stderr

0 comments on commit 383ec52

Please sign in to comment.