logger

package
v1.0.4 Latest Latest
Warning

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

Go to latest
Published: May 26, 2026 License: Apache-2.0 Imports: 11 Imported by: 0

Documentation

Overview

Package logger provides structured logging for the application. It defines LogLevel and LogFormat types, configures the global slog logger, and provides a GORM logger adapter (see gorm.go) that delegates to slog.

Index

Constants

This section is empty.

Variables

View Source
var ValidLogFormats = []LogFormat{
	LogFormatJSON,
	LogFormatText,
}

ValidLogFormats lists all accepted log format strings.

ValidLogLevels lists all accepted log level strings.

Functions

func GetSlogLevel

func GetSlogLevel(level LogLevel) slog.Level

GetSlogLevel returns the slog level for the given level string.

func InitDefaultLevel

func InitDefaultLevel()

InitDefaultLevel initializes the default logger with INFO level, JSON format, and OTel disabled. It's safe to call before config is loaded.

func IsValidLogFormat

func IsValidLogFormat(format LogFormat) bool

IsValidLogFormat reports whether the given format string is recognized.

func IsValidLogLevel

func IsValidLogLevel(level LogLevel) bool

IsValidLogLevel reports whether the given level string is recognized.

func SetGlobalLogger

func SetGlobalLogger(level LogLevel, format LogFormat, otelEnabled bool)

SetGlobalLogger sets the default slog logger. When otelEnabled is true, records are fanned out to stdout (with trace_id/span_id enrichment) and to the OTel log provider via the otelslog bridge.

Types

type GormLogger

type GormLogger struct {
	// contains filtered or unexported fields
}

GormLogger adapts slog to gorm's logger.Interface.

func NewGormLogger

func NewGormLogger(opts ...GormLoggerOption) *GormLogger

NewGormLogger creates a GORM logger that delegates to slog.

func (*GormLogger) Error

func (g *GormLogger) Error(ctx context.Context, msg string, args ...any)

Error logs an error-level message.

func (*GormLogger) Info

func (g *GormLogger) Info(ctx context.Context, msg string, args ...any)

Info logs an info-level message.

func (*GormLogger) LogMode

LogMode sets the log level for the GORM logger.

func (*GormLogger) Trace

func (g *GormLogger) Trace(ctx context.Context, begin time.Time, fc func() (string, int64), err error)

Trace logs SQL execution details including duration and row count.

func (*GormLogger) Warn

func (g *GormLogger) Warn(ctx context.Context, msg string, args ...any)

Warn logs a warning-level message.

type GormLoggerOption

type GormLoggerOption func(*GormLogger)

GormLoggerOption configures a GormLogger.

func WithIgnoreRecordNotFoundError

func WithIgnoreRecordNotFoundError() GormLoggerOption

WithIgnoreRecordNotFoundError suppresses record-not-found log entries.

func WithSlowThreshold

func WithSlowThreshold(d time.Duration) GormLoggerOption

WithSlowThreshold sets the duration above which queries are logged as slow.

type LogFormat

type LogFormat string

LogFormat represents a log output format.

const (
	// LogFormatJSON outputs log entries as JSON (one object per line).
	LogFormatJSON LogFormat = "json"
	// LogFormatText outputs log entries as human-readable text.
	LogFormatText LogFormat = "text"
)

func (LogFormat) String

func (f LogFormat) String() string

String returns the string representation of the log format.

type LogLevel

type LogLevel string

LogLevel represents a named logging severity.

const (
	// LogLevelDebug is the debug severity level.
	LogLevelDebug LogLevel = "debug"
	// LogLevelInfo is the info severity level.
	LogLevelInfo LogLevel = "info"
	// LogLevelWarn is the warning severity level.
	LogLevelWarn LogLevel = "warn"
	// LogLevelError is the error severity level.
	LogLevelError LogLevel = "error"
)

func (LogLevel) String

func (l LogLevel) String() string

String returns the string representation of the log level.

Directories

Path Synopsis
Package slogext provides slog.Handler primitives used internally by the logger package: a trace-context enricher and a fanout multiplexer.
Package slogext provides slog.Handler primitives used internally by the logger package: a trace-context enricher and a fanout multiplexer.

Jump to

Keyboard shortcuts

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