Skip to content

Commit 0abb2fc

Browse files
committed
Minor refactor of basic config patch
1 parent 3cf2362 commit 0abb2fc

File tree

1 file changed

+30
-28
lines changed
  • opentelemetry-sdk/src/opentelemetry/sdk/_configuration

1 file changed

+30
-28
lines changed

opentelemetry-sdk/src/opentelemetry/sdk/_configuration/__init__.py

Lines changed: 30 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -253,41 +253,43 @@ def _init_logging(
253253
set_event_logger_provider(event_logger_provider)
254254

255255
if setup_logging_handler:
256-
# noinspection PyPep8Naming
257-
original_basicConfig = logging.basicConfig
256+
_patch_basic_config()
258257

259-
# noinspection PyPep8Naming
260-
def patched_basicConfig(*args, **kwargs):
261-
# Get the root logger
262-
root = logging.getLogger()
258+
# Add OTel handler
259+
handler = LoggingHandler(
260+
level=logging.NOTSET, logger_provider=provider
261+
)
262+
logging.getLogger().addHandler(handler)
263263

264-
# Check if the only handler is our OTel handler
265-
has_only_otel = len(root.handlers) == 1 and isinstance(
266-
root.handlers[0], LoggingHandler
267-
)
268264

269-
if has_only_otel:
270-
# Temporarily remove OTel handler
271-
otel_handler = root.handlers[0]
272-
root.handlers = []
265+
def _patch_basic_config():
266+
original_basic_config = logging.basicConfig
273267

274-
# Call original basicConfig
275-
original_basicConfig(*args, **kwargs)
268+
def patched_basic_config(*args, **kwargs):
269+
# Get the root logger
270+
root = logging.getLogger()
276271

277-
# Add OTel handler back
278-
root.addHandler(otel_handler)
279-
else:
280-
# Normal behavior
281-
original_basicConfig(*args, **kwargs)
272+
# Check if the only handler is our OTel handler
273+
has_only_otel = len(root.handlers) == 1 and isinstance(
274+
root.handlers[0], LoggingHandler
275+
)
282276

283-
# Apply the monkey patch
284-
logging.basicConfig = patched_basicConfig
277+
if has_only_otel:
278+
# Temporarily remove OTel handler
279+
otel_handler = root.handlers[0]
280+
root.handlers = []
285281

286-
# Add OTel handler
287-
handler = LoggingHandler(
288-
level=logging.NOTSET, logger_provider=provider
289-
)
290-
logging.getLogger().addHandler(handler)
282+
# Call original basicConfig
283+
original_basic_config(*args, **kwargs)
284+
285+
# Add OTel handler back
286+
root.addHandler(otel_handler)
287+
else:
288+
# Normal behavior
289+
original_basic_config(*args, **kwargs)
290+
291+
# Apply the monkey patch
292+
logging.basicConfig = patched_basic_config
291293

292294

293295
def _import_exporters(

0 commit comments

Comments
 (0)