Skip to content

Commit

Permalink
Fix flush exception thrown when LoggerProvider not configured
Browse files Browse the repository at this point in the history
  • Loading branch information
iurisilvio committed Jan 1, 2024
1 parent da48e0b commit 853bd75
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 2 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@ 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))

## Version 1.22.0/0.43b0 (2023-12-15)

- Prometheus exporter sanitize info metric ([#3572](https://github.com/open-telemetry/opentelemetry-python/pull/3572))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -543,9 +543,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):
Expand Down
6 changes: 6 additions & 0 deletions opentelemetry-sdk/tests/logs/test_handler.py
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,12 @@ def test_log_record_emit_noop(self):
logger.warning("Warning message")
handler_mock._translate.assert_not_called()

def test_log_flush_noop(self):
emitter_provider_mock = Mock(spec=NoOpLoggerProvider)
logger = get_logger(logger_provider=emitter_provider_mock)
with self.assertLogs(level=logging.WARNING):
logger.warning("Warning message")

def test_log_record_no_span_context(self):
emitter_provider_mock = Mock(spec=LoggerProvider)
emitter_mock = APIGetLogger(
Expand Down

0 comments on commit 853bd75

Please sign in to comment.