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.
To explicitly omit request and response bodies from the final log entry for a given request, call DisableBody inside the Gin handler or in a middleware that runs 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) DisableTrace()
- func (c *Context) Error(err error)
- func (c *Context) Get(key any) (any, bool)
- func (c *Context) GetLatency() int64
- func (c *Context) Info(message string)
- func (c *Context) MustGet(key string) any
- func (c *Context) Set(key any, value any)
- func (c *Context) TraceEnd(process *formatter.Service)
- func (c *Context) TraceInit(process *formatter.Service)
- func (c *Context) Warn(message string)
- 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.KibanaData
// 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 Kibana 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) 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 Kibana 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) 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) Info ¶
Info logs an informational message using the current context.
If the context already contains Kibana 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) 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{
Process: “processPayment�,
System: “payments�,
}
ctx.TraceInit(process)
defer ctx.TraceEnd(process)
func (*Context) Warn ¶
Warn logs a warning message using the current context.
If the context already contains Kibana 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) 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.