Skip to content

Commit

Permalink
docs: recommend to use named loggers instead of the root logger as de…
Browse files Browse the repository at this point in the history
…scribed in #34787 (#35771)
  • Loading branch information
johschmidt42 committed May 28, 2024
1 parent d335a04 commit a8ed00e
Showing 1 changed file with 20 additions and 15 deletions.
35 changes: 20 additions & 15 deletions sdk/monitor/azure-monitor-opentelemetry-exporter/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,14 @@ The following sections provide several code snippets covering some of the most c

Review the [OpenTelemetry Logging SDK][ot_logging_sdk] to learn how to use OpenTelemetry components to collect logs.

When integrating the `AzureMonitorLogExporter`, it's **strongly advised to utilize a named logger** rather
than the root logger.
This recommendation stems from the exporter's dependency on `azure-core` for constructing and dispatching requests.
Since `azure-core` itself uses a Python logger, attaching the handler to the root logger would
inadvertently capture and export these internal log messages as well.
This triggers a recursive loop of logging and exporting, leading to an unnecessary proliferation of log data.
To avoid this, configure a named logger for your application's logging needs or set up your logging handler to filter out logs originating from the SDK library.

#### Export Hello World Log

```python
Expand Down Expand Up @@ -181,13 +189,12 @@ exporter = AzureMonitorLogExporter(
)

logger_provider.add_log_record_processor(BatchLogRecordProcessor(exporter))
handler = LoggingHandler()

# Attach LoggingHandler to root logger
logging.getLogger().addHandler(handler)
logging.getLogger().setLevel(logging.NOTSET)

# Attach LoggingHandler to namespaced logger
handler = LoggingHandler()
logger = logging.getLogger(__name__)
logger.addHandler(handler)
logger.setLevel(logging.NOTSET)

logger.warning("Hello World!")

Expand Down Expand Up @@ -226,13 +233,12 @@ exporter = AzureMonitorLogExporter(
)

logger_provider.add_log_record_processor(BatchLogRecordProcessor(exporter))
handler = LoggingHandler()

# Attach LoggingHandler to root logger
logging.getLogger().addHandler(handler)
logging.getLogger().setLevel(logging.NOTSET)

# Attach LoggingHandler to namespaced logger
handler = LoggingHandler()
logger = logging.getLogger(__name__)
logger.addHandler(handler)
logger.setLevel(logging.NOTSET)

logger.info("INFO: Outside of span")
with tracer.start_as_current_span("foo"):
Expand Down Expand Up @@ -266,13 +272,12 @@ exporter = AzureMonitorLogExporter(
)

logger_provider.add_log_record_processor(BatchLogRecordProcessor(exporter))
handler = LoggingHandler()

# Attach LoggingHandler to root logger
logging.getLogger().addHandler(handler)
logging.getLogger().setLevel(logging.NOTSET)

# Attach LoggingHandler to namespaced logger
handler = LoggingHandler()
logger = logging.getLogger(__name__)
logger.addHandler(handler)
logger.setLevel(logging.NOTSET)

# Custom properties
logger.debug("DEBUG: Debug with properties", extra={"debug": "true"})
Expand Down

0 comments on commit a8ed00e

Please sign in to comment.