Skip to content

Commit

Permalink
add adapter logging interface
Browse files Browse the repository at this point in the history
  • Loading branch information
Nathaniel May committed Oct 29, 2021
1 parent dd45637 commit 98c692f
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 0 deletions.
13 changes: 13 additions & 0 deletions core/dbt/events/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,16 @@ The event module provides types that represent what is happening in dbt in `even

# Adding a New Event
In `events.types` add a new class that represents the new event. This may be a simple class with no values, or it may be a dataclass with some values to construct downstream messaging. Only include the data necessary to construct this message within this class. You must extend all destinations (e.g. - if your log message belongs on the cli, extend `CliEventABC`) as well as the loglevel this event belongs to.

# Adapter Maintainers
To integrate existing log messages from adapters, you likely have a line of code like this in your adapter already:
```python
from dbt.logger import GLOBAL_LOGGER as logger
```

Simply change it to these two lines with your adapter's database name, and all your existing call sites will now use the new system for v1.0:
```python
from dbt.events import AdapterLogger
logger = AdapterLogger("<database name>")
# e.g. AdapterLogger("Snowflake")
```
1 change: 1 addition & 0 deletions core/dbt/events/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
from .adapter_endpoint import AdapterLogger # noqa: F401
22 changes: 22 additions & 0 deletions core/dbt/events/adapter_endpoint.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
from dataclasses import dataclass
from dbt.events.functions import fire_event
from dbt.events.types import (
AdapterEventDebug, AdapterEventInfo, AdapterEventWarning, AdapterEventError
)


@dataclass
class AdapterLogger():
name: str

def debug(self, msg: str):
fire_event(AdapterEventDebug(name=self.name, raw_msg=msg))

def info(self, msg: str):
fire_event(AdapterEventInfo(name=self.name, raw_msg=msg))

def warning(self, msg: str):
fire_event(AdapterEventWarning(name=self.name, raw_msg=msg))

def error(self, msg: str):
fire_event(AdapterEventError(name=self.name, raw_msg=msg))
25 changes: 25 additions & 0 deletions core/dbt/events/types.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,31 @@ def cli_msg(self) -> str:
raise Exception("cli_msg not implemented for cli event")


@dataclass
class AdapterEventBase():
name: str
raw_msg: str

def cli_msg(self) -> str:
return f"{self.name} adapter: {self.raw_msg}"


class AdapterEventDebug(DebugLevel, AdapterEventBase, CliEventABC):
pass


class AdapterEventInfo(InfoLevel, AdapterEventBase, CliEventABC):
pass


class AdapterEventWarning(WarnLevel, AdapterEventBase, CliEventABC):
pass


class AdapterEventError(ErrorLevel, AdapterEventBase, CliEventABC):
pass


class ParsingStart(InfoLevel, CliEventABC):
def cli_msg(self) -> str:
return "Start parsing."
Expand Down

0 comments on commit 98c692f

Please sign in to comment.