Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix flush exception thrown when LoggerProvider not configured #3608

Merged
merged 3 commits into from
Feb 14, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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):
ocelotl marked this conversation as resolved.
Show resolved Hide resolved
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):
ocelotl marked this conversation as resolved.
Show resolved Hide resolved

no_op_logger_provider = NoOpLoggerProvider()
no_op_logger_provider.force_flush = Mock()

logger = get_logger(logger_provider=NoOpLoggerProvider())
ocelotl marked this conversation as resolved.
Show resolved Hide resolved

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
Loading