Documentation
¶
Overview ¶
Package logger provides structured logging (log/slog) with Google Cloud Logging-compatible JSON output: severity, time, message, trace_id, correlation_id, sourceLocation, and optional fields.
Role in architecture:
- Infrastructure: used by handlers, middleware, and other packages for request-scoped and plain logging.
Responsibilities:
- Package-level functions: Debugf, Infof, Warnf, Errorf, Fatalf and structured Debug/Info/Warn/Error with Fields.
- Context-bound logging: WithTraceID, WithCorrelationID, GetTraceID, GetCorrelationID; WithContext(ctx) returns a logger that includes IDs in JSON.
- Context-aware functions: DebugfContext, InfofContext, WarnfContext, ErrorfContext.
- Lazy writer: log file is opened on first write so init() does not block.
- SetLogLevel, GetLogger, GetWriter for configuration and testing.
Constraints:
- Single global logger instance; no per-request logger creation beyond WithContext.
- Output format is fixed (GCP-style JSON); no pluggable formatters.
This package must NOT:
- Depend on database or HTTP packages; only standard library and slog.
Index ¶
- func Debug(msg string, fields Fields)
- func Debugf(format string, args ...interface{})
- func DebugfContext(ctx context.Context, format string, args ...interface{})
- func Error(msg string, fields Fields)
- func Errorf(format string, args ...interface{})
- func ErrorfContext(ctx context.Context, format string, args ...interface{})
- func Fatalf(format string, args ...interface{})
- func GetCorrelationID(ctx context.Context) string
- func GetLogger() *slog.Logger
- func GetTraceID(ctx context.Context) string
- func GetWriter() io.Writer
- func Info(msg string, fields Fields)
- func Infof(format string, args ...interface{})
- func InfofContext(ctx context.Context, format string, args ...interface{})
- func SetLogLevel(level slog.Level)
- func Warn(msg string, fields Fields)
- func Warnf(format string, args ...interface{})
- func WarnfContext(ctx context.Context, format string, args ...interface{})
- func WithCorrelationID(ctx context.Context, correlationID string) context.Context
- func WithTraceID(ctx context.Context, traceID string) context.Context
- type Ctx
- func (c *Ctx) Debug(msg string, fields Fields)
- func (c *Ctx) Debugf(format string, args ...interface{})
- func (c *Ctx) Error(msg string, fields Fields)
- func (c *Ctx) Errorf(format string, args ...interface{})
- func (c *Ctx) Fatalf(format string, args ...interface{})
- func (c *Ctx) Info(msg string, fields Fields)
- func (c *Ctx) Infof(format string, args ...interface{})
- func (c *Ctx) Warn(msg string, fields Fields)
- func (c *Ctx) Warnf(format string, args ...interface{})
- type Fields
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func Debugf ¶
func Debugf(format string, args ...interface{})
Debugf logs at level Debug (no trace/correlation).
func DebugfContext ¶ added in v0.1.11
DebugfContext logs at level Debug with trace_id/correlation_id from ctx.
func Errorf ¶
func Errorf(format string, args ...interface{})
Errorf logs at level Error (no trace/correlation).
func ErrorfContext ¶ added in v0.1.11
ErrorfContext logs at level Error with trace_id/correlation_id from ctx.
func Fatalf ¶
func Fatalf(format string, args ...interface{})
Fatalf logs at level Error and exits with code 1.
func GetCorrelationID ¶ added in v0.1.11
GetCorrelationID returns the correlation ID from ctx if set, otherwise empty string.
func GetTraceID ¶ added in v0.1.11
GetTraceID returns the trace ID from ctx if set, otherwise empty string.
func GetWriter ¶ added in v0.2.0
GetWriter returns the io.Writer used for logging (file + stderr). Use this when an io.Writer is needed, e.g. for GORM database logger.
func Infof ¶
func Infof(format string, args ...interface{})
Infof logs at level Info (no trace/correlation).
func InfofContext ¶ added in v0.1.11
InfofContext logs at level Info with trace_id/correlation_id from ctx.
func Warnf ¶
func Warnf(format string, args ...interface{})
Warnf logs at level Warn (no trace/correlation).
func WarnfContext ¶ added in v0.1.11
WarnfContext logs at level Warn with trace_id/correlation_id from ctx.
func WithCorrelationID ¶ added in v0.1.11
WithCorrelationID returns a copy of ctx with the given correlation ID.
Types ¶
type Ctx ¶ added in v0.1.12
type Ctx struct {
// contains filtered or unexported fields
}
Ctx is a logger bound to a context. Use WithContext to create it; then Infof, Debugf, etc. automatically include trace_id and correlation_id from that context in the JSON output.
func WithContext ¶ added in v0.1.12
WithContext returns a context-bound logger. Calls to Infof, Debugf, Warnf, Errorf (and Debug, Info, Warn, Error) on the returned value will automatically include trace_id and correlation_id from ctx in the log JSON. Use this in request handlers after trace middleware has set the context, e.g.:
log := logger.WithContext(c.Request.Context())
log.Infof("user %s logged in", userID)
func (*Ctx) Debug ¶ added in v0.1.12
Debug logs at level Debug with fields; includes trace_id/correlation_id from the bound context.
func (*Ctx) Debugf ¶ added in v0.1.12
Debugf logs at level Debug with trace_id/correlation_id from the bound context.
func (*Ctx) Error ¶ added in v0.1.12
Error logs at level Error with fields; includes trace_id/correlation_id from the bound context.
func (*Ctx) Errorf ¶ added in v0.1.12
Errorf logs at level Error with trace_id/correlation_id from the bound context.
func (*Ctx) Fatalf ¶ added in v0.1.12
Fatalf logs at level Error and exits with code 1; includes trace_id/correlation_id from the bound context.
func (*Ctx) Info ¶ added in v0.1.12
Info logs at level Info with fields; includes trace_id/correlation_id from the bound context.
func (*Ctx) Infof ¶ added in v0.1.12
Infof logs at level Info with trace_id/correlation_id from the bound context.