Skip to content

Commit 206bfa2

Browse files
feat: WebClient expose logger property
1 parent 897449a commit 206bfa2

File tree

4 files changed

+56
-0
lines changed

4 files changed

+56
-0
lines changed

slack_sdk/web/async_base_client.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,17 @@ def __init__(
8888
if env_variable is not None:
8989
self.proxy = env_variable
9090

91+
# -------------------------
92+
# accessors
93+
94+
@property
95+
def logger(self) -> logging.Logger:
96+
"""The logger this client uses."""
97+
return self._logger
98+
99+
# -------------------------
100+
# api call
101+
91102
async def api_call(
92103
self,
93104
api_method: str,

slack_sdk/web/base_client.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,17 @@ def __init__(
9292
if env_variable is not None:
9393
self.proxy = env_variable
9494

95+
# -------------------------
96+
# accessors
97+
98+
@property
99+
def logger(self) -> logging.Logger:
100+
"""The logger this client uses."""
101+
return self._logger
102+
103+
# -------------------------
104+
# api call
105+
95106
def api_call(
96107
self,
97108
api_method: str,

tests/slack_sdk/web/test_web_client_issue_921_custom_logger.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,23 @@ def test_if_it_uses_custom_logger(self):
2323
client.chat_postMessage(channel="C111", text="hello")
2424
self.assertTrue(logger.called)
2525

26+
def test_if_property_returns_custom_logger(self):
27+
logger = CustomLogger("test-logger")
28+
client = WebClient(
29+
base_url="http://localhost:8888",
30+
token="xoxb-api_test",
31+
logger=logger,
32+
)
33+
self.assertEqual(client.logger, logger)
34+
35+
def test_logger_property_has_no_setter(self):
36+
client = WebClient(
37+
base_url="http://localhost:8888",
38+
token="xoxb-api_test",
39+
)
40+
with self.assertRaises(AttributeError):
41+
client.logger = CustomLogger("test-logger")
42+
2643

2744
class CustomLogger(Logger):
2845
called: bool

tests/slack_sdk_async/web/test_web_client_issue_921_custom_logger.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,23 @@ async def test_if_it_uses_custom_logger(self):
2525
await client.chat_postMessage(channel="C111", text="hello")
2626
self.assertTrue(logger.called)
2727

28+
def test_if_property_returns_custom_logger(self):
29+
logger = CustomLogger("test-logger")
30+
client = AsyncWebClient(
31+
base_url="http://localhost:8888",
32+
token="xoxb-api_test",
33+
logger=logger,
34+
)
35+
self.assertEqual(client.logger, logger)
36+
37+
def test_logger_property_has_no_setter(self):
38+
client = AsyncWebClient(
39+
base_url="http://localhost:8888",
40+
token="xoxb-api_test",
41+
)
42+
with self.assertRaises(AttributeError):
43+
client.logger = CustomLogger("test-logger")
44+
2845

2946
class CustomLogger(Logger):
3047
called: bool

0 commit comments

Comments
 (0)