builder

package
v0.0.12 Latest Latest
Warning

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

Go to latest
Published: May 11, 2026 License: Apache-2.0 Imports: 26 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.

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

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.KibanaData
	// 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 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

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

func (c *Context) Get(key any) (any, 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) Info

func (c *Context) Info(message string)

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) 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) TraceEnd

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

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

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

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

func (c *Context) Warn(message string)

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

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