builder

package
v0.0.55 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jul 5, 2026 License: Apache-2.0 Imports: 28 Imported by: 0

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

Constants

This section is empty.

Variables

This section is empty.

Functions

func DisableModeTest

func DisableModeTest()

func EnableModeTest

func EnableModeTest()

func InitLogger

func InitLogger(ctx context.Context, dir string) (*sdklog.LoggerProvider, error)

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

func From(parent context.Context) (*Context, bool)

From attempts to extract a *logger.Context* from a context.Context. Returns (*Context, true) if it exists; otherwise (nil, false).

func New

func New(parent context.Context) *Context

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

func (c *Context) Debug(message string)

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

func (c *Context) Debugf(format string, args ...any)

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

func (c *Context) Error(err 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

func (c *Context) Errorf(format string, args ...any)

Errorf logs a formatted error message using the current context.

func (*Context) Get

func (c *Context) Get(key any) (value any, ok bool)

Get retrieves a value from the context.

func (*Context) GetLatency

func (c *Context) GetLatency() int64

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

func (c *Context) GetTraceCallerSkip() int

GetTraceCallerSkip returns the number of stack frames to skip when determining the caller's method and line number for logging purposes.

func (*Context) Info

func (c *Context) Info(message string)

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

func (c *Context) Infof(format string, args ...any)

Infof logs a formatted informational message using the current context.

func (*Context) MustGet

func (c *Context) MustGet(key string) any

MustGet returns a value or throws an exception if one does not exist.

func (*Context) Set

func (c *Context) Set(key any, value any)

Set adds a key-value pair to the context.

func (*Context) SetTraceCallerSkip added in v0.0.21

func (c *Context) SetTraceCallerSkip(skip int)

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

func (c *Context) SetTraceID(id string)

SetTraceID stores the correlation trace id used by structured logs and fallback propagation headers.

func (*Context) TraceEnd

func (c *Context) TraceEnd(process *formatter.Process)

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) TraceID added in v0.0.34

func (c *Context) TraceID() string

TraceID returns the current correlation trace id.

func (*Context) TraceInit

func (c *Context) TraceInit(process *formatter.Process)

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

func (c *Context) Warn(message string)

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

func (c *Context) Warnf(format string, args ...any)

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

func (c *Context) WithTimeout(d time.Duration) (*Context, context.CancelFunc)

WithTimeout creates a copy of logger.Context that automatically expires after the specified duration.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL