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

Is there a way to make loguru automatically print the trace_id in logs like logging does? #3615

Open
commonfurture opened this issue Jan 5, 2024 · 4 comments

Comments

@commonfurture
Copy link

I can print out the trace_id using the default logging, but I can't do it with loguru. Is there a way to make loguru also print out the trace_id?
image

image

@commonfurture commonfurture added the bug Something isn't working label Jan 5, 2024
@commonfurture commonfurture changed the title Does logging auto instrumentation support auto printing trace_id with loguru? Is there a way to make loguru automatically print the trace_id in logs like logging does? Jan 5, 2024
@Rocksus
Copy link

Rocksus commented Jan 18, 2024

What I ended up doing was making a custom wrapper for my loguru logger, which automatically binds the traceID and spanID for me

tracing_log_format = (
    "<green>{time:YYYY-MM-DD HH:mm:ss}</green> | "
    "<level>{level: <8}</level> | "
    "<yellow>req_id:{extra[request_id]}</yellow> | "
    "<red>t:{extra[otelTraceID]} s:{extra[otelSpanID]}</red> | "
    "<cyan>{name}</cyan>:<cyan>{function}</cyan>:<cyan>{line}</cyan> - <level>{message}</level>"
    "<blue>context: {extra}</blue>"
)

logger.configure(
    extra={"otelSpanID": 0, "otelTraceID": 0}
)

logger.add(sys.stderr, format=tracing_log_format, level="INFO")

def _prepare_logger_extras() -> Dict[str, Any]:
    extra_data = {}
    span = trace.get_current_span()
    extra_data["otelSpanID"] = trace.format_span_id(
        span.get_span_context().span_id
    )
    extra_data["otelTraceID"] = trace.format_trace_id(
        span.get_span_context().trace_id
    )

    return extra_data
    
def info(__message: str, *args: Any, **kwargs: Any):
    logger.opt(depth=1).bind(**_prepare_logger_extras()).info(
        __message, *args, **kwargs
    )

Though I'm not sure if this can be considered good practice or not, maybe others can also help enlighten me on this

@lzchen
Copy link
Contributor

lzchen commented Feb 16, 2024

Only the standard Python logging library is currently supported by OpenTelemetry. Converting this to a feature request to support loguru.

@lzchen
Copy link
Contributor

lzchen commented Jul 2, 2024

Related: #3806

@phitoduck
Copy link

If anyone more knowledgable on Otel can comment, the creator of loguru is trying to implement this, but is held back by knowing if the behavior is close enough to what the opentelemetry-python-logging module does: Delgan/loguru#674 (comment)

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

4 participants