Documentation
¶
Overview ¶
Package builder provides the runtime integration layer for the logger.
It contains the pieces most applications use directly:
- logger initialization through InitLogger
- request-scoped context creation through MiddlewareInitLogger
- HTTP log emission through MiddlewareLoggerWithConfig
- request/response body capture through MiddlewareCaptureBody
- convenience logging helpers for Gin handlers
Body capture and body emission are intentionally separate concerns: MiddlewareCaptureBody stores the raw request and response payloads in the gin.Context, while MiddlewareLoggerWithConfig decides whether those payloads are copied into the final structured log.
Request and response bodies are omitted from final log entries by default. To include them for a given request, use the HTTP or gRPC middleware helper EnableBody before the log formatter is executed.
Index ¶
- func DisableModeTest()
- func EnableModeTest()
- func InitLogger(ctx context.Context, dir string) (*sdklog.LoggerProvider, error)
- type Context
- func (c *Context) Debug(message string)
- func (c *Context) Debugf(format string, args ...any)
- func (c *Context) DisableTrace()
- func (c *Context) Error(err error)
- func (c *Context) Errorf(format string, args ...any)
- func (c *Context) Get(key any) (value any, ok bool)
- func (c *Context) GetLatency() int64
- func (c *Context) GetTraceCallerSkip() int
- func (c *Context) Info(message string)
- func (c *Context) Infof(format string, args ...any)
- func (c *Context) MustGet(key string) any
- func (c *Context) Set(key any, value any)
- func (c *Context) SetTraceCallerSkip(skip int)
- func (c *Context) SetTraceID(id string)
- func (c *Context) TraceEnd(process *formatter.Process)
- func (c *Context) TraceID() string
- func (c *Context) TraceInit(process *formatter.Process)
- func (c *Context) Warn(message string)
- func (c *Context) Warnf(format string, args ...any)
- func (c *Context) WithCancel() (*Context, context.CancelFunc)
- func (c *Context) WithTimeout(d time.Duration) (*Context, context.CancelFunc)
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func DisableModeTest ¶
func DisableModeTest()
func EnableModeTest ¶
func EnableModeTest()
func InitLogger ¶
InitLogger initializes and configures the application's logger. It builds the log file path, configures the OpenTelemetry logger provider, and returns the logger provider so it can be shut down gracefully when needed.
When running the application as a server, logging is already initialized automatically, so calling this function manually is not necessary. However, in non-server contexts, you can call InitLogger to set up logging.
Types ¶
type Context ¶
type Context struct {
context.Context // hereda Cancel, Deadline, Done, Value
Method string
Line int
Details formatter.Details
// contains filtered or unexported fields
}
Context is a custom context similar to gin.Context.
func From ¶
From attempts to extract a *logger.Context* from a context.Context. Returns (*Context, true) if it exists; otherwise (nil, false).
func New ¶
New creates or reuses a *logger.Context*.
- If the parent context already contains a *logger.Context*, it reuses it.
- Otherwise, it creates a new one, initializes the traceID and the list of services, and saves it in the context values for later use.
func (*Context) Debug ¶
Debug logs a debug-level message using the current context.
If the context already contains details base information, it copies the System, Service, and Client fields from the received data before storing it again.
If the logger is in test mode, it does not generate any output.
This function is intended for development or troubleshooting purposes and should not be relied upon for critical operational logging in production environments unless debug logging is explicitly enabled.
This function only logs the message; it does not create spans or measure latency on its own.
func (*Context) Debugf ¶ added in v0.0.41
Debugf logs a formatted debug-level message using the current context.
func (*Context) DisableTrace ¶
func (c *Context) DisableTrace()
DisableTrace disables trace logging for a specific process or trace.
func (*Context) Error ¶
Error logs an error message using `slog` with the current context.
If the context already contains details base information, it copies `System`, `Service`, and `Client` to the received details before storing them again.
If the logger is in test mode, it does not generate any output.
This function logs err.Error() and does not modify the execution flow; error handling remains the caller’s responsibility.
func (*Context) Errorf ¶ added in v0.0.42
Errorf logs a formatted error message using the current context.
func (*Context) GetLatency ¶
GetLatency returns the elapsed time since the context was created. It measures how long the operation associated with this context has been running.
func (*Context) GetTraceCallerSkip ¶ added in v0.0.21
GetTraceCallerSkip returns the number of stack frames to skip when determining the caller's method and line number for logging purposes.
func (*Context) Info ¶
Info logs an informational message using the current context.
If the context already contains details base information, it copies the System, Service, and Client fields from the received data before storing it again.
If the logger is in test mode, it does not generate any output.
This function only logs the message; it does not create spans or measure latency on its own.
func (*Context) Infof ¶ added in v0.0.41
Infof logs a formatted informational message using the current context.
func (*Context) SetTraceCallerSkip ¶ added in v0.0.21
SetTraceCallerSkip sets the number of stack frames to skip when determining the caller's method and line number for logging purposes.
func (*Context) SetTraceID ¶ added in v0.0.34
SetTraceID stores the correlation trace id used by structured logs and fallback propagation headers.
func (*Context) TraceEnd ¶
TraceEnd completes the measurement started with TraceInit.
This function:
- assigns the trace ID to the process, if applicable
- classifies the status based on the HTTP code
- calculates the process latency
- adds the process to the context's service list
- records attributes in the span and closes it
If the logger is in test mode, it performs no action.
It should normally be used with defer immediately after TraceInit.
func (*Context) TraceInit ¶
TraceInit marks the start of tracing for a process or subprocess.
It records the start time in process.TimeInit and, if tracing is enabled, creates a span associated with the process.
If the logger is in test mode, it does nothing.
Recommended usage:
process := &formatter.Services{
System: "PaymentService",
Process: Process Paymaent,
}
ctx.TraceInit(process)
defer ctx.TraceEnd(process)
func (*Context) Warn ¶
Warn logs a warning message using the current context.
If the context already contains details base information, it copies the System, Service, and Client fields from the received data before storing it again.
If the logger is in test mode, it does not generate any output.
This function only logs the message; it does not create spans or measure latency on its own.
func (*Context) Warnf ¶ added in v0.0.41
Warnf logs a formatted warning message using the current context.
func (*Context) WithCancel ¶
func (c *Context) WithCancel() (*Context, context.CancelFunc)
WithCancel creates a copy of logger.Context with support for manual cancellation.
func (*Context) WithTimeout ¶
WithTimeout creates a copy of logger.Context that automatically expires after the specified duration.