Overview
The v2 context functions accept acontext.Context parameter for distributed tracing and request correlation. These functions are part of the legacy v2 API and are maintained for backward compatibility.
The v2 API is legacy. For new code, use the v3 Logger API with
LogCtx for full context propagation support.Basic Context Functions
InfoLogCtx
Logs an informational message with context support.ctx- The context (intended for tracing, cancellation, etc.)message- The informational message to log
- Accepts context but does not use it (TODO: extract trace ID)
- Calls
InfoLog(message)internally - Displays in yellow color
api.go:123-139
ErrorLogCtx
Logs an error message with context support.ctx- The context (intended for tracing, cancellation, etc.)message- The error message to log
- Accepts context but does not use it (TODO: extract trace ID)
- Calls
ErrorLog(message)internally - Displays in red color
api.go:141-157
WarningLogCtx
Logs a warning message with context support.ctx- The context (intended for tracing, cancellation, etc.)message- The warning message to log
- Accepts context but does not use it (TODO: extract trace ID)
- Calls
WarningLog(message)internally - Displays in yellow color
api.go:159-175
SuccessLogCtx
Logs a success message with context support.ctx- The context (intended for tracing, cancellation, etc.)message- The success message to log
- Accepts context but does not use it (TODO: extract trace ID)
- Calls
SuccessLog(message)internally - Displays in green color
api.go:177-193
Formatted Context Functions
InfoLogCtxf
Logs an informational message with context and formatting.ctx- The context (intended for tracing, cancellation, etc.)format- The format string (followsfmt.Sprintfsyntax)args- Arguments for the format string
- Formats message using
fmt.Sprintf(format, args...) - Calls
InfoLogCtx(ctx, message)internally
api.go:195-211
ErrorLogCtxf
Logs an error message with context and formatting.ctx- The context (intended for tracing, cancellation, etc.)format- The format string (followsfmt.Sprintfsyntax)args- Arguments for the format string
- Formats message using
fmt.Sprintf(format, args...) - Calls
ErrorLogCtx(ctx, message)internally
api.go:213-229
WarningLogCtxf
Logs a warning message with context and formatting.ctx- The context (intended for tracing, cancellation, etc.)format- The format string (followsfmt.Sprintfsyntax)args- Arguments for the format string
- Formats message using
fmt.Sprintf(format, args...) - Calls
WarningLogCtx(ctx, message)internally
api.go:231-247
SuccessLogCtxf
Logs a success message with context and formatting.ctx- The context (intended for tracing, cancellation, etc.)format- The format string (followsfmt.Sprintfsyntax)args- Arguments for the format string
- Formats message using
fmt.Sprintf(format, args...) - Calls
SuccessLogCtx(ctx, message)internally
api.go:249-265
Usage Examples
HTTP Handler with Context
gRPC Service with Context
Background Job with Context
Current Limitations
- Context is not used: The
ctxparameter is accepted but ignored - No trace ID extraction: Cannot extract
trace_id,span_id, etc. from context - No automatic field propagation: Context values are not included in logs
- TODO markers in code: Source code contains
// TODO: Extract trace ID from context
Migration to V3
The v3 API provides full context support with automatic field extraction: V2 Context (Limited):- Automatic extraction of
trace_id,span_id,request_id,user_id - Structured fields instead of string formatting
- Full OpenTelemetry integration (future)
- Better performance (zero allocations)
Future Enhancements
Planned improvements for v2 context functions:- Extract trace IDs from context and include in log messages
- Support for OpenTelemetry context propagation
- Automatic correlation with distributed tracing systems
Related API
- V2 Global Functions - Basic logging functions
- V2 Formatted Functions - Formatted logging without context
- Context Propagation - V3 context propagation (recommended)
- Context Helpers - V3 context extraction (recommended)