Skip to content

Commit c452204

Browse files
committed
Overwrite logging config functions with patched versions..
1 parent 20c92a3 commit c452204

File tree

2 files changed

+76
-21
lines changed

2 files changed

+76
-21
lines changed

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

Lines changed: 21 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
from __future__ import annotations
2121

2222
import logging
23+
import logging.config
2324
import os
2425
from abc import ABC, abstractmethod
2526
from os import environ
@@ -253,31 +254,31 @@ def _init_logging(
253254
set_event_logger_provider(event_logger_provider)
254255

255256
if setup_logging_handler:
256-
_patch_basic_config()
257-
258257
# Add OTel handler
259258
handler = LoggingHandler(
260259
level=logging.NOTSET, logger_provider=provider
261260
)
262261
logging.getLogger().addHandler(handler)
263-
264-
265-
def _patch_basic_config():
266-
original_basic_config = logging.basicConfig
267-
268-
def patched_basic_config(*args, **kwargs):
269-
root = logging.getLogger()
270-
has_only_otel = len(root.handlers) == 1 and isinstance(
271-
root.handlers[0], LoggingHandler
272-
)
273-
if has_only_otel:
274-
otel_handler = root.handlers.pop()
275-
original_basic_config(*args, **kwargs)
276-
root.addHandler(otel_handler)
277-
else:
278-
original_basic_config(*args, **kwargs)
279-
280-
logging.basicConfig = patched_basic_config
262+
_overwrite_logging_config_fns(handler)
263+
264+
265+
def _overwrite_logging_config_fns(handler):
266+
root = logging.getLogger()
267+
268+
def wrapper(config_fn):
269+
def overwritten_config_fn(*args, **kwargs):
270+
# This is needed for basicConfig only. basicConfig when called by
271+
# the user's program will be a no-op if the root handler was configured.
272+
if len(root.handlers) == 1 and root.handlers[0] == handler:
273+
root.handlers.pop()
274+
config_fn(*args, **kwargs)
275+
if handler not in root.handlers:
276+
root.addHandler(handler)
277+
return overwritten_config_fn
278+
279+
logging.config.fileConfig = wrapper(logging.config.fileConfig)
280+
logging.config.dictConfig = wrapper(logging.config.dictConfig)
281+
logging.basicConfig = wrapper(logging.basicConfig)
281282

282283

283284
def _import_exporters(

opentelemetry-sdk/tests/test_configurator.py

Lines changed: 55 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,12 +16,14 @@
1616
from __future__ import annotations
1717

1818
import logging
19+
import logging.config
1920
from logging import WARNING, getLogger
2021
from os import environ
2122
from typing import Iterable, Optional, Sequence
2223
from unittest import TestCase, mock
2324
from unittest.mock import Mock, patch
2425

26+
import pytest
2527
from pytest import raises
2628

2729
from opentelemetry import trace
@@ -881,7 +883,6 @@ def test_basicConfig_preserves_otel_handler(self):
881883
)
882884
handler = root_logger.handlers[0]
883885
self.assertIsInstance(handler, LoggingHandler)
884-
885886
logging.basicConfig()
886887

887888
self.assertGreater(len(root_logger.handlers), 1)
@@ -897,6 +898,51 @@ def test_basicConfig_preserves_otel_handler(self):
897898
"Should still have exactly one OpenTelemetry LoggingHandler",
898899
)
899900

901+
def test_dictConfig_preserves_otel_handler(self):
902+
with ClearLoggingHandlers():
903+
_init_logging(
904+
{"otlp": DummyOTLPLogExporter},
905+
Resource.create({}),
906+
setup_logging_handler=True,
907+
)
908+
909+
root = logging.getLogger()
910+
self.assertEqual(
911+
len(root.handlers),
912+
1,
913+
"Should be exactly one OpenTelemetry LoggingHandler",
914+
)
915+
logging.config.dictConfig(
916+
{
917+
"version": 1,
918+
"handlers": {
919+
"default": {
920+
"level": "INFO",
921+
"class": "logging.StreamHandler",
922+
"stream": "ext://sys.stdout",
923+
},
924+
},
925+
"loggers": {
926+
"": { # root logger
927+
"handlers": ["default"],
928+
"level": "WARNING",
929+
"propagate": False,
930+
},
931+
},
932+
}
933+
)
934+
print(root.handlers)
935+
self.assertEqual(len(root.handlers), 2)
936+
937+
logging_handlers = [
938+
h for h in root.handlers if isinstance(h, LoggingHandler)
939+
]
940+
self.assertEqual(
941+
len(logging_handlers),
942+
1,
943+
"Should still have exactly one OpenTelemetry LoggingHandler",
944+
)
945+
900946

901947
class TestMetricsInit(TestCase):
902948
def setUp(self):
@@ -1169,3 +1215,11 @@ def test_preserves_handlers(self):
11691215
self.assertIs(h1, h2)
11701216

11711217
root_logger.removeHandler(test_handler)
1218+
1219+
1220+
pytest.main(
1221+
[
1222+
"-x",
1223+
"/usr/local/google/home/dylanrussell/archivist_backup_250529_133018/reinstate/opentelemetry-python/opentelemetry-sdk/tests/test_configurator.py",
1224+
]
1225+
)

0 commit comments

Comments
 (0)