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

Ability to set different log level for the console and logfire #900

Open
stemarks opened this issue Feb 25, 2025 · 1 comment
Open

Ability to set different log level for the console and logfire #900

stemarks opened this issue Feb 25, 2025 · 1 comment

Comments

@stemarks
Copy link

Description

We have a scenario where we dont need all logs to go to logfire, for example debug logs, but we do want debug logs to be on the console.

Currently if sampling is implemented it impacts both the console and logfire logs without a workaround that im aware of.

@alexmojaki
Copy link
Contributor

alexmojaki commented Feb 26, 2025

Here's a workaround:

from opentelemetry.exporter.otlp.proto.http.trace_exporter import OTLPSpanExporter
from opentelemetry.sdk.trace.export import BatchSpanProcessor

import logfire
from logfire._internal.exporters.wrapper import WrapperSpanExporter


class DontSendDebugLogs(WrapperSpanExporter):
    def export(self, spans):
        spans = [span for span in spans if span.attributes.get('logfire.level_num', 9) >= 9]
        return super().export(spans)


logfire.configure(
    send_to_logfire=False,
    console=logfire.ConsoleOptions(min_log_level='debug'),
    additional_span_processors=[
        BatchSpanProcessor(
            DontSendDebugLogs(
                OTLPSpanExporter(
                    endpoint='https://logfire-api.pydantic.dev/v1/traces',
                    headers={'Authorization': 'your-write-token'},
                )
            )
        )
    ],
)

logfire.debug('debug')
with logfire.span('my-span'):
    logfire.error('error')

However this misses some of the features of the exporter that logfire.configure(send_to_logfire=True) creates, namely pending spans and retrying.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants