Skip to content

Commit f836a72

Browse files
committed
fix: duplicated debug output
1 parent fab076c commit f836a72

File tree

2 files changed

+18
-1
lines changed

2 files changed

+18
-1
lines changed

influxdb_client/configuration.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -171,7 +171,10 @@ def debug(self, value):
171171
for name, logger in self.loggers.items():
172172
logger.setLevel(logging.DEBUG)
173173
if name == 'influxdb_client.client.http':
174-
logger.addHandler(logging.StreamHandler(sys.stdout))
174+
# makes sure to do not duplicate stdout handler
175+
if not any(map(lambda h: isinstance(h, logging.StreamHandler) and h.stream == sys.stdout,
176+
logger.handlers)):
177+
logger.addHandler(logging.StreamHandler(sys.stdout))
175178
# we use 'influxdb_client.client.http' logger instead of this
176179
# httplib.HTTPConnection.debuglevel = 1
177180
else:

tests/test_InfluxDBClient.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -391,6 +391,20 @@ def test_redacted_auth_header(self):
391391

392392
self.assertIn("Authorization: ***", log_stream.getvalue())
393393

394+
def test_duplicate_debug_logging_handler(self):
395+
logging.getLogger('influxdb_client.client.http').handlers.clear()
396+
self.influxdb_client = InfluxDBClient("http://localhost", "my-token", debug=True)
397+
self.influxdb_client = InfluxDBClient("http://localhost", "my-token", debug=True)
398+
logger = logging.getLogger('influxdb_client.client.http')
399+
self.assertEqual(1, len(logger.handlers))
400+
401+
def test_custom_debug_logging_handler(self):
402+
logging.getLogger('influxdb_client.client.http').addHandler(logging.FileHandler('logs.log'))
403+
self.influxdb_client = InfluxDBClient("http://localhost", "my-token", debug=True)
404+
self.influxdb_client = InfluxDBClient("http://localhost", "my-token", debug=True)
405+
logger = logging.getLogger('influxdb_client.client.http')
406+
self.assertEqual(2, len(logger.handlers))
407+
394408

395409
class ServerWithSelfSingedSSL(http.server.SimpleHTTPRequestHandler):
396410
def _set_headers(self):

0 commit comments

Comments
 (0)