diff --git a/CHANGELOG.md b/CHANGELOG.md index d014b1e578a..9fb69ac302c 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -7,6 +7,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ## Unreleased +- Fix flush error when no LoggerProvider configured for LoggingHandler + ([#3608](https://github.com/open-telemetry/opentelemetry-python/pull/3608)) - Fix `OTLPMetricExporter` ignores `preferred_aggregation` property ([#3603](https://github.com/open-telemetry/opentelemetry-python/pull/3603)) - Logs: set `observed_timestamp` field diff --git a/opentelemetry-sdk/src/opentelemetry/sdk/_logs/_internal/__init__.py b/opentelemetry-sdk/src/opentelemetry/sdk/_logs/_internal/__init__.py index df23454be25..d84c9919608 100644 --- a/opentelemetry-sdk/src/opentelemetry/sdk/_logs/_internal/__init__.py +++ b/opentelemetry-sdk/src/opentelemetry/sdk/_logs/_internal/__init__.py @@ -553,9 +553,10 @@ def emit(self, record: logging.LogRecord) -> None: def flush(self) -> None: """ - Flushes the logging output. + Flushes the logging output. Skip flushing if logger is NoOp. """ - self._logger_provider.force_flush() + if not isinstance(self._logger, NoOpLogger): + self._logger_provider.force_flush() class Logger(APILogger): diff --git a/opentelemetry-sdk/tests/logs/test_handler.py b/opentelemetry-sdk/tests/logs/test_handler.py index e1ef93bf34f..0f96361712b 100644 --- a/opentelemetry-sdk/tests/logs/test_handler.py +++ b/opentelemetry-sdk/tests/logs/test_handler.py @@ -79,6 +79,19 @@ def test_log_record_emit_noop(self): logger.warning("Warning message") handler_mock._translate.assert_not_called() + def test_log_flush_noop(self): + + no_op_logger_provider = NoOpLoggerProvider() + no_op_logger_provider.force_flush = Mock() + + logger = get_logger(logger_provider=no_op_logger_provider) + + with self.assertLogs(level=logging.WARNING): + logger.warning("Warning message") + + logger.handlers[0].flush() + no_op_logger_provider.force_flush.assert_not_called() + def test_log_record_no_span_context(self): emitter_provider_mock = Mock(spec=LoggerProvider) emitter_mock = APIGetLogger(