Skip to content

Commit

Permalink
🐛 Airbyte CDK: transforming Python log levels to Airbyte protocol log…
Browse files Browse the repository at this point in the history
… levels

* Airbyte CDK native logger airbytehq#1279 - transforming Python log levels to Airbyte protocol log levels

* Airbyte CDK native logger airbytehq#1279 - add test for level types transforming

* CDK standard logger airbytehq#1279 - test for transform critical level to fatal
  • Loading branch information
vitaliizazmic authored and schlattk committed Jan 4, 2022
1 parent c7a59e7 commit 8f3400d
Show file tree
Hide file tree
Showing 7 changed files with 34 additions and 8 deletions.
3 changes: 3 additions & 0 deletions airbyte-cdk/python/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
# Changelog

## 0.1.31
Transforming Python log levels to Airbyte protocol log levels

## 0.1.30
Updated OAuth2Specification.rootObject type in airbyte_protocol to allow string or int

Expand Down
13 changes: 12 additions & 1 deletion airbyte-cdk/python/airbyte_cdk/logger.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,10 +42,21 @@ def init_logger(name: str = None):
class AirbyteLogFormatter(logging.Formatter):
"""Output log records using AirbyteMessage"""

# Transforming Python log levels to Airbyte protocol log levels
level_mapping = {
logging.FATAL: "FATAL",
logging.ERROR: "ERROR",
logging.WARNING: "WARN",
logging.INFO: "INFO",
logging.DEBUG: "DEBUG",
TRACE_LEVEL_NUM: "TRACE",
}

def format(self, record: logging.LogRecord) -> str:
"""Return a JSON representation of the log message"""
message = super().format(record)
log_message = AirbyteMessage(type="LOG", log=AirbyteLogMessage(level=record.levelname, message=message))
airbyte_level = self.level_mapping.get(record.levelno, "INFO")
log_message = AirbyteMessage(type="LOG", log=AirbyteLogMessage(level=airbyte_level, message=message))
return log_message.json(exclude_unset=True)


Expand Down
2 changes: 0 additions & 2 deletions airbyte-cdk/python/airbyte_cdk/models/airbyte_protocol.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,10 +44,8 @@ class Config:

class Level(Enum):
FATAL = "FATAL"
CRITICAL = "CRITICAL"
ERROR = "ERROR"
WARN = "WARN"
WARNING = "WARNING"
INFO = "INFO"
DEBUG = "DEBUG"
TRACE = "TRACE"
Expand Down
2 changes: 1 addition & 1 deletion airbyte-cdk/python/setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@

setup(
name="airbyte-cdk",
version="0.1.30",
version="0.1.31",
description="A framework for writing Airbyte Connectors.",
long_description=README,
long_description_content_type="text/markdown",
Expand Down
18 changes: 18 additions & 0 deletions airbyte-cdk/python/unit_tests/test_logger.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,24 @@ def test_formatter(logger, caplog):
assert message == "Test formatter"


def test_level_transform(logger, caplog):
formatter = AirbyteLogFormatter()
logger.warning("Test level transform warn")
logger.critical("Test level transform critical")
record_warn = caplog.records[0]
record_critical = caplog.records[1]
formatted_record_warn = formatter.format(record_warn)
formatted_record_warn_data = json.loads(formatted_record_warn)
log_warn = formatted_record_warn_data.get("log")
level_warn = log_warn.get("level")
formatted_record_critical = formatter.format(record_critical)
formatted_record_critical_data = json.loads(formatted_record_critical)
log_critical = formatted_record_critical_data.get("log")
level_critical = log_critical.get("level")
assert level_warn == "WARN"
assert level_critical == "FATAL"


def test_trace(logger, caplog):
logger.trace("Test trace 1")
record = caplog.records[0]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,10 +44,8 @@ class Config:

class Level(Enum):
FATAL = "FATAL"
CRITICAL = "CRITICAL"
ERROR = "ERROR"
WARN = "WARN"
WARNING = "WARNING"
INFO = "INFO"
DEBUG = "DEBUG"
TRACE = "TRACE"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -86,10 +86,8 @@ definitions:
type: string
enum:
- FATAL
- CRITICAL
- ERROR
- WARN
- WARNING
- INFO
- DEBUG
- TRACE
Expand Down

0 comments on commit 8f3400d

Please sign in to comment.