Skip to content

Commit 992c0e5

Browse files
committed
Minor refactor of basic config patch
1 parent 25b78a8 commit 992c0e5

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
@@ -251,41 +251,43 @@ def _init_logging(
251251
set_event_logger_provider(event_logger_provider)
252252

253253
if setup_logging_handler:
254-
# noinspection PyPep8Naming
255-
original_basicConfig = logging.basicConfig
254+
_patch_basic_config()
256255

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

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

267-
if has_only_otel:
268-
# Temporarily remove OTel handler
269-
otel_handler = root.handlers[0]
270-
root.handlers = []
263+
def _patch_basic_config():
264+
original_basic_config = logging.basicConfig
271265

272-
# Call original basicConfig
273-
original_basicConfig(*args, **kwargs)
266+
def patched_basic_config(*args, **kwargs):
267+
# Get the root logger
268+
root = logging.getLogger()
274269

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

281-
# Apply the monkey patch
282-
logging.basicConfig = patched_basicConfig
275+
if has_only_otel:
276+
# Temporarily remove OTel handler
277+
otel_handler = root.handlers[0]
278+
root.handlers = []
283279

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

290292

291293
def _import_exporters(

0 commit comments

Comments
 (0)