From 68687f2bdd263c6dd990a9d54903ec3f3375fd80 Mon Sep 17 00:00:00 2001 From: rjduffner Date: Thu, 7 Sep 2023 08:21:08 -0700 Subject: [PATCH] Add ability to set log format for apps that have already had basicConfig called --- .../src/opentelemetry/instrumentation/logging/__init__.py | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/instrumentation/opentelemetry-instrumentation-logging/src/opentelemetry/instrumentation/logging/__init__.py b/instrumentation/opentelemetry-instrumentation-logging/src/opentelemetry/instrumentation/logging/__init__.py index ce332d0113..fe4e4fa5d6 100644 --- a/instrumentation/opentelemetry-instrumentation-logging/src/opentelemetry/instrumentation/logging/__init__.py +++ b/instrumentation/opentelemetry-instrumentation-logging/src/opentelemetry/instrumentation/logging/__init__.py @@ -144,7 +144,12 @@ def record_factory(*args, **kwargs): ) log_level = log_level or logging.INFO - logging.basicConfig(format=log_format, level=log_level) + root_logger = logging.getLogger() + if root_logger.hasHandlers(): + for handler in root_logger.handlers: + handler.setFormatter(logging.Formatter(log_format)) + else: + logging.basicConfig(format=log_format, level=log_level) def _uninstrument(self, **kwargs): if LoggingInstrumentor._old_factory: