Skip to content

Commit

Permalink
Fix flush exception thrown when LoggerProvider not configured (#3608)
Browse files Browse the repository at this point in the history
* Fix flush exception thrown when LoggerProvider not configured

* Fix test case

* Use right object in test case

---------

Co-authored-by: Diego Hurtado <[email protected]>
  • Loading branch information
iurisilvio and ocelotl authored Feb 14, 2024
1 parent 55bf100 commit fc9368f
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 2 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down
13 changes: 13 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,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(
Expand Down

0 comments on commit fc9368f

Please sign in to comment.