|
1 | 1 | # |
2 | 2 | # Copyright (c) 2023 Airbyte, Inc., all rights reserved. |
3 | 3 | # |
4 | | - |
5 | 4 | import json |
6 | 5 | import logging |
7 | 6 | import logging.config |
| 7 | +import os |
8 | 8 | from typing import Any, Callable, Mapping, Optional, Tuple |
9 | 9 |
|
10 | 10 | import orjson |
|
40 | 40 | } |
41 | 41 |
|
42 | 42 |
|
| 43 | +def is_platform_debug_log_enabled() -> bool: |
| 44 | + return os.environ.get("LOG_LEVEL", "info").lower() == "debug" |
| 45 | + |
| 46 | + |
43 | 47 | def init_logger(name: Optional[str] = None) -> logging.Logger: |
44 | 48 | """Initial set up of logger""" |
45 | 49 | logger = logging.getLogger(name) |
@@ -73,8 +77,22 @@ def format(self, record: logging.LogRecord) -> str: |
73 | 77 | airbyte_level = self.level_mapping.get(record.levelno, "INFO") |
74 | 78 | if airbyte_level == Level.DEBUG: |
75 | 79 | extras = self.extract_extra_args_from_record(record) |
76 | | - debug_dict = {"type": "DEBUG", "message": record.getMessage(), "data": extras} |
77 | | - return filter_secrets(json.dumps(debug_dict)) |
| 80 | + if is_platform_debug_log_enabled(): |
| 81 | + # We have a different behavior between debug logs enabled through `--debug` argument and debug logs |
| 82 | + # enabled through environment variable. The reason is that for platform logs, we need to have these |
| 83 | + # printed as AirbyteMessage which is not the case with the current previous implementation. |
| 84 | + # Why not migrate both to AirbyteMessages then? AirbyteMessages do not support having structured logs. |
| 85 | + # which means that the DX would be degraded compared to the current solution (devs will need to identify |
| 86 | + # the `log.message` field and figure out where in this field is the response while the current solution |
| 87 | + # have a specific field that is structured for extras. |
| 88 | + message = f"{filter_secrets(record.getMessage())} ///\nExtra logs: {filter_secrets(json.dumps(extras))}" |
| 89 | + log_message = AirbyteMessage( |
| 90 | + type=Type.LOG, log=AirbyteLogMessage(level=airbyte_level, message=message) |
| 91 | + ) |
| 92 | + return orjson.dumps(AirbyteMessageSerializer.dump(log_message)).decode() |
| 93 | + else: |
| 94 | + debug_dict = {"type": "DEBUG", "message": record.getMessage(), "data": extras} |
| 95 | + return filter_secrets(json.dumps(debug_dict)) |
78 | 96 | else: |
79 | 97 | message = super().format(record) |
80 | 98 | message = filter_secrets(message) |
|
0 commit comments