Documentation
¶
Index ¶
- Constants
- Variables
- func Debug(ctx context.Context, msg string)
- func Error(ctx context.Context, msg string)
- func GetClusterID(ctx context.Context) (string, bool)
- func GetLogger() *slog.Logger
- func GetRequestID(ctx context.Context) (string, bool)
- func GetResourceID(ctx context.Context) (string, bool)
- func GetResourceType(ctx context.Context) (string, bool)
- func GetSpanID(ctx context.Context) (string, bool)
- func GetTraceID(ctx context.Context) (string, bool)
- func GetTransactionID(ctx context.Context) (int64, bool)
- func HTTPDuration(d time.Duration) slog.Attr
- func HTTPMethod(method string) slog.Attr
- func HTTPPath(path string) slog.Attr
- func HTTPRequestAttrs(r *http.Request) []slog.Attr
- func HTTPResponseAttrs(statusCode int, duration time.Duration) []slog.Attr
- func HTTPStatusCode(code int) slog.Attr
- func HTTPUserAgent(ua string) slog.Attr
- func Info(ctx context.Context, msg string)
- func InitGlobalLogger(cfg *LogConfig)
- func NewHyperFleetHandler(cfg *LogConfig) slog.Handler
- func NewOCMLoggerBridge() sdk.Logger
- func ParseLogLevel(level string) (slog.Level, error)
- func ParseLogOutput(output string) (io.Writer, error)
- func ReconfigureGlobalLogger(cfg *LogConfig)
- func RequestIDMiddleware(handler http.Handler) http.Handler
- func Warn(ctx context.Context, msg string)
- func WithClusterID(ctx context.Context, clusterID string) context.Context
- func WithRequestID(ctx context.Context) context.Context
- func WithResourceID(ctx context.Context, resourceID string) context.Context
- func WithResourceType(ctx context.Context, resourceType string) context.Context
- func WithSpanID(ctx context.Context, spanID string) context.Context
- func WithTraceID(ctx context.Context, traceID string) context.Context
- func WithTransactionID(ctx context.Context, transactionID int64) context.Context
- type ContextField
- type ContextLogger
- type GormLogger
- func (l *GormLogger) Error(ctx context.Context, msg string, data ...interface{})
- func (l *GormLogger) Info(ctx context.Context, msg string, data ...interface{})
- func (l *GormLogger) LogMode(level gormlogger.LogLevel) gormlogger.Interface
- func (l *GormLogger) Trace(ctx context.Context, begin time.Time, ...)
- func (l *GormLogger) Warn(ctx context.Context, msg string, data ...interface{})
- type HyperFleetHandler
- type HyperFleetTextHandler
- func (h *HyperFleetTextHandler) Enabled(_ context.Context, level slog.Level) bool
- func (h *HyperFleetTextHandler) Handle(ctx context.Context, r slog.Record) error
- func (h *HyperFleetTextHandler) WithAttrs(attrs []slog.Attr) slog.Handler
- func (h *HyperFleetTextHandler) WithGroup(name string) slog.Handler
- type LogConfig
- type LogFormat
- type OCMLoggerBridge
- func (b *OCMLoggerBridge) Debug(ctx context.Context, format string, args ...interface{})
- func (b *OCMLoggerBridge) DebugEnabled() bool
- func (b *OCMLoggerBridge) Error(ctx context.Context, format string, args ...interface{})
- func (b *OCMLoggerBridge) ErrorEnabled() bool
- func (b *OCMLoggerBridge) Fatal(ctx context.Context, format string, args ...interface{})
- func (b *OCMLoggerBridge) Info(ctx context.Context, format string, args ...interface{})
- func (b *OCMLoggerBridge) InfoEnabled() bool
- func (b *OCMLoggerBridge) Warn(ctx context.Context, format string, args ...interface{})
- func (b *OCMLoggerBridge) WarnEnabled() bool
Constants ¶
const ( ReqIDKey contextKey = "request_id" TransactionIDCtxKey contextKey = "transaction_id" // Database transaction ID TraceIDCtxKey contextKey = "trace_id" SpanIDCtxKey contextKey = "span_id" ClusterIDCtxKey contextKey = "cluster_id" ResourceTypeCtxKey contextKey = "resource_type" ResourceIDCtxKey contextKey = "resource_id" )
Context keys for storing correlation fields
const ( FieldBindAddress = "bind_address" FieldEnvironment = "environment" FieldLogLevel = "level" FieldLogFormat = "format" FieldLogOutput = "output" )
Server/Config related fields
const ( FieldMigrationID = "migration_id" // FieldConnectionString - WARNING: Always sanitize connection strings before logging // to prevent exposing passwords. Never log raw connection strings. FieldConnectionString = "connection_string" FieldTable = "table" FieldChannel = "channel" )
Database related fields
const ( FieldOTelEnabled = "otel_enabled" FieldSamplingRate = "sampling_rate" FieldExporterEndpoint = "exporter_endpoint" )
OpenTelemetry related fields
const ( FieldAdapter = "adapter" FieldErrorCode = "error_code" FieldFlag = "flag" FieldData = "data" )
Generic fields
const ( FieldHTTPMethod = "method" FieldHTTPPath = "path" FieldHTTPStatusCode = "status_code" FieldHTTPDuration = "duration_ms" FieldHTTPUserAgent = "user_agent" )
HTTP field name constants
const (
FieldEndpoint = "endpoint"
)
Endpoint related fields (used in handlers)
const (
FieldNodePoolID = "nodepool_id"
)
Resource related fields
const (
FieldSchemaPath = "schema_path"
)
Schema related fields
const (
ReqIDHeader = "X-Request-ID"
)
HTTP header names
Variables ¶
var ContextFieldsRegistry = []ContextField{ {ReqIDKey, "request_id", GetRequestID}, {TraceIDCtxKey, "trace_id", GetTraceID}, {SpanIDCtxKey, "span_id", GetSpanID}, {ClusterIDCtxKey, "cluster_id", GetClusterID}, {ResourceTypeCtxKey, "resource_type", GetResourceType}, {ResourceIDCtxKey, "resource_id", GetResourceID}, }
ContextFieldsRegistry defines all string-type context fields for logging This is the single source of truth for string field management Fields are ordered as per HyperFleet Logging Specification (docs/logging.md:384) Note: transaction_id (int64) is handled separately in logger.go
Functions ¶
func GetClusterID ¶
GetClusterID retrieves cluster ID from context
func GetRequestID ¶
GetRequestID retrieves request ID from context
func GetResourceID ¶
GetResourceID retrieves resource ID from context
func GetResourceType ¶
GetResourceType retrieves resource type from context
func GetTraceID ¶
GetTraceID retrieves trace ID from context
func GetTransactionID ¶
GetTransactionID retrieves transaction ID from context
func HTTPDuration ¶
HTTPDuration returns a slog attribute for HTTP request duration in milliseconds
func HTTPMethod ¶
HTTPMethod returns a slog attribute for HTTP method
func HTTPRequestAttrs ¶
HTTPRequestAttrs returns a slice of slog attributes for HTTP request
func HTTPResponseAttrs ¶
HTTPResponseAttrs returns a slice of slog attributes for HTTP response
func HTTPStatusCode ¶
HTTPStatusCode returns a slog attribute for HTTP status code
func HTTPUserAgent ¶
HTTPUserAgent returns a slog attribute for HTTP user agent
func InitGlobalLogger ¶
func InitGlobalLogger(cfg *LogConfig)
InitGlobalLogger initializes the global logger with the given configuration. This function is idempotent (safe to call multiple times).
Concurrency note: This function uses atomic.Value without sync.Once intentionally. Current call sites are serialized (single-threaded main() and sync.Once in tests). If concurrent initialization occurs, the last Store() wins, which is acceptable. For stricter guarantees in future use cases, callers should wrap this with sync.Once or equivalent synchronization.
func NewHyperFleetHandler ¶
NewHyperFleetHandler creates a HyperFleet logger handler Returns slog.Handler interface to support both HyperFleetHandler (JSON) and HyperFleetTextHandler (Text)
func NewOCMLoggerBridge ¶
NewOCMLoggerBridge creates an OCM SDK logger bridge
func ParseLogLevel ¶
ParseLogLevel converts string to slog.Level
func ParseLogOutput ¶
ParseLogOutput converts string to io.Writer
func ReconfigureGlobalLogger ¶
func ReconfigureGlobalLogger(cfg *LogConfig)
ReconfigureGlobalLogger reconfigures the global logger with new configuration Unlike InitGlobalLogger, this can be called multiple times to update configuration This is useful when environment configuration is loaded after initial logger setup
func RequestIDMiddleware ¶
RequestIDMiddleware Middleware wraps the given HTTP handler so that the details of the request are sent to the log.
func WithClusterID ¶
WithClusterID adds cluster ID to context
func WithRequestID ¶
WithRequestID adds request ID to context If request ID already exists in context, it returns the context unchanged Otherwise, it generates a new KSUID and adds it to the context
func WithResourceID ¶
WithResourceID adds resource ID to context
func WithResourceType ¶
WithResourceType adds resource type to context
func WithSpanID ¶
WithSpanID adds span ID to context
func WithTraceID ¶
WithTraceID adds trace ID to context
Types ¶
type ContextField ¶
ContextField defines metadata for a string-type context log field
type ContextLogger ¶
type ContextLogger struct {
// contains filtered or unexported fields
}
ContextLogger wraps a context with additional temporary key-value pairs for logging.
func With ¶
func With(ctx context.Context, args ...any) *ContextLogger
With creates a new ContextLogger with temporary key-value pairs.
func WithError ¶
func WithError(ctx context.Context, err error) *ContextLogger
WithError is a convenience function for adding error field to logs.
func (*ContextLogger) Debug ¶
func (l *ContextLogger) Debug(msg string)
func (*ContextLogger) Error ¶
func (l *ContextLogger) Error(msg string)
func (*ContextLogger) Info ¶
func (l *ContextLogger) Info(msg string)
func (*ContextLogger) Warn ¶
func (l *ContextLogger) Warn(msg string)
func (*ContextLogger) With ¶
func (l *ContextLogger) With(args ...any) *ContextLogger
With adds additional temporary fields to the logger and can be chained.
func (*ContextLogger) WithError ¶
func (l *ContextLogger) WithError(err error) *ContextLogger
WithError adds an error field to the logger and can be chained.
type GormLogger ¶
type GormLogger struct {
// contains filtered or unexported fields
}
func NewGormLogger ¶
func NewGormLogger(logLevel gormlogger.LogLevel, slowThreshold time.Duration) *GormLogger
func (*GormLogger) Error ¶
func (l *GormLogger) Error(ctx context.Context, msg string, data ...interface{})
func (*GormLogger) Info ¶
func (l *GormLogger) Info(ctx context.Context, msg string, data ...interface{})
func (*GormLogger) LogMode ¶
func (l *GormLogger) LogMode(level gormlogger.LogLevel) gormlogger.Interface
type HyperFleetHandler ¶
type HyperFleetHandler struct {
// contains filtered or unexported fields
}
HyperFleetHandler implements slog.Handler interface Adds HyperFleet-specific fields: component, version, hostname, trace_id, span_id, etc.
type HyperFleetTextHandler ¶
type HyperFleetTextHandler struct {
// contains filtered or unexported fields
}
HyperFleetTextHandler implements the HyperFleet Logging Specification text format: {timestamp} {LEVEL} [{component}] [{version}] [{hostname}] {message} {key=value}...
Example output: 2026-01-09T12:30:45Z INFO [hyperfleet-api] [v1.2.3] [pod-abc] Processing request request_id=xyz cluster_id=abc123
func NewHyperFleetTextHandler ¶
func NewHyperFleetTextHandler(w io.Writer, component, version, hostname string, level slog.Level) *HyperFleetTextHandler
NewHyperFleetTextHandler creates a new text handler conforming to HyperFleet Logging Specification
func (*HyperFleetTextHandler) Enabled ¶
Enabled reports whether the handler handles records at the given level
type LogConfig ¶
type LogConfig struct {
Level slog.Level
Format LogFormat
Output io.Writer
Component string
Version string
Hostname string
}
LogConfig holds the configuration for the logger
type LogFormat ¶
type LogFormat int
LogFormat enumeration for output format
func ParseLogFormat ¶
ParseLogFormat converts string to LogFormat
type OCMLoggerBridge ¶
type OCMLoggerBridge struct{}
OCMLoggerBridge bridges slog to OCM SDK's Logger interface
func (*OCMLoggerBridge) Debug ¶
func (b *OCMLoggerBridge) Debug(ctx context.Context, format string, args ...interface{})
Debug implements sdk.Logger
func (*OCMLoggerBridge) DebugEnabled ¶
func (b *OCMLoggerBridge) DebugEnabled() bool
DebugEnabled implements sdk.Logger
func (*OCMLoggerBridge) Error ¶
func (b *OCMLoggerBridge) Error(ctx context.Context, format string, args ...interface{})
Error implements sdk.Logger
func (*OCMLoggerBridge) ErrorEnabled ¶
func (b *OCMLoggerBridge) ErrorEnabled() bool
ErrorEnabled implements sdk.Logger
func (*OCMLoggerBridge) Fatal ¶
func (b *OCMLoggerBridge) Fatal(ctx context.Context, format string, args ...interface{})
Fatal implements sdk.Logger
func (*OCMLoggerBridge) Info ¶
func (b *OCMLoggerBridge) Info(ctx context.Context, format string, args ...interface{})
Info implements sdk.Logger
func (*OCMLoggerBridge) InfoEnabled ¶
func (b *OCMLoggerBridge) InfoEnabled() bool
InfoEnabled implements sdk.Logger
func (*OCMLoggerBridge) Warn ¶
func (b *OCMLoggerBridge) Warn(ctx context.Context, format string, args ...interface{})
Warn implements sdk.Logger
func (*OCMLoggerBridge) WarnEnabled ¶
func (b *OCMLoggerBridge) WarnEnabled() bool
WarnEnabled implements sdk.Logger