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 d14f51f
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 2 deletions.
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 d14f51f

Please sign in to comment.