Documentation
¶
Overview ¶
Package logger implements a logging package.
Example usage:
log := logger.New(os.Stdout, logger.LogfmtFormat(), logger.Info)
// Logger can have scoped context
log = log.With(ctx.Str("env", "prod"))
// All messages can have a context
log.Error("connection error", ctx.Str("redis", conn.Name()), ctx.Int("timeout", conn.Timeout()))
Index ¶
- Constants
- Variables
- func WithContext(ctx context.Context, log *Logger, fields ...Field) context.Context
- type Event
- func (e *Event) AppendBool(k string, b bool)
- func (e *Event) AppendBytes(k string, p []byte)
- func (e *Event) AppendDuration(k string, d time.Duration)
- func (e *Event) AppendFloat(k string, f float64)
- func (e *Event) AppendInt(k string, i int64)
- func (e *Event) AppendInterface(k string, v any)
- func (e *Event) AppendInts(k string, a []int)
- func (e *Event) AppendString(k, s string)
- func (e *Event) AppendStrings(k string, s []string)
- func (e *Event) AppendTime(k string, d time.Time)
- func (e *Event) AppendUint(k string, i uint64)
- type Field
- type Formatter
- type Level
- type Logger
- func (l *Logger) Crit(msg string, ctx ...Field)
- func (l *Logger) Debug(msg string, ctx ...Field)
- func (l *Logger) Error(msg string, ctx ...Field)
- func (l *Logger) FromContext(ctx context.Context) *Logger
- func (l *Logger) Info(msg string, ctx ...Field)
- func (l *Logger) Trace(msg string, ctx ...Field)
- func (l *Logger) Warn(msg string, ctx ...Field)
- func (l *Logger) With(ctx ...Field) *Logger
- func (l *Logger) WithTimestamp() (cancel func())
- func (l *Logger) Writer(lvl Level) io.Writer
- type SyncWriter
Examples ¶
Constants ¶
const ( // TimestampKey is the key used for timestamps. TimestampKey = "ts" // LevelKey is the key used for message levels. LevelKey = "lvl" // MessageKey is the key used for message descriptions. MessageKey = "msg" )
const ( TimeFormatUnix = "" TimeFormatISO8601 = "2006-01-02T15:04:05-0700" )
Time formats.
Variables ¶
var TimeFormat = TimeFormatUnix
TimeFormat is the format that times will be added in.
TimeFormat defaults to unix time.
Functions ¶
func WithContext ¶ added in v2.10.0
WithContext returns a new context carrying the given fields. Any fields previously attached via WithContext are preserved; the new fields are appended after them. Any fields already added to the logger are not attached to the context.
If the logger discards output or no fields are given, ctx is returned unchanged.
Example ¶
package main
import (
"context"
"os"
"github.com/hamba/logger/v2"
"github.com/hamba/logger/v2/ctx"
)
func main() {
log := logger.New(os.Stdout, logger.LogfmtFormat(), logger.Info).With(ctx.Str("svc", "api"))
reqCtx := logger.WithContext(context.Background(), log, ctx.Str("req_id", "abc-123"), ctx.Str("method", "GET"))
reqLog := log.FromContext(reqCtx)
reqLog.Info("request handled", ctx.Int("status", 200))
}
Output:
Types ¶
type Event ¶
type Event struct {
// contains filtered or unexported fields
}
Event is a log event.
func (*Event) AppendBool ¶
AppendBool appends a bool to the event.
func (*Event) AppendBytes ¶
AppendBytes appends bytes to the event.
func (*Event) AppendDuration ¶
AppendDuration appends a duration to the event.
func (*Event) AppendFloat ¶
AppendFloat appends a float to the event.
func (*Event) AppendInterface ¶
AppendInterface appends a interface to the event.
func (*Event) AppendInts ¶
AppendInts appends ints to the event.
func (*Event) AppendString ¶
AppendString appends a string to the event.
func (*Event) AppendStrings ¶
AppendStrings appends strings to the event.
func (*Event) AppendTime ¶
AppendTime appends a time to the event.
func (*Event) AppendUint ¶
AppendUint appends a uint to the event.
type Formatter ¶
type Formatter interface {
WriteMessage(buf *bytes.Buffer, ts time.Time, lvl Level, msg string)
AppendBeginMarker(buf *bytes.Buffer)
AppendEndMarker(buf *bytes.Buffer)
AppendLineBreak(buf *bytes.Buffer)
AppendArrayStart(buf *bytes.Buffer)
AppendArraySep(buf *bytes.Buffer)
AppendArrayEnd(buf *bytes.Buffer)
AppendKey(buf *bytes.Buffer, key string)
AppendString(buf *bytes.Buffer, s string)
AppendBool(buf *bytes.Buffer, b bool)
AppendInt(buf *bytes.Buffer, i int64)
AppendUint(buf *bytes.Buffer, i uint64)
AppendFloat(buf *bytes.Buffer, f float64)
AppendTime(buf *bytes.Buffer, t time.Time)
AppendDuration(buf *bytes.Buffer, d time.Duration)
AppendInterface(buf *bytes.Buffer, v any)
}
Formatter represents a log message formatter.
func ConsoleFormat ¶
func ConsoleFormat() Formatter
ConsoleFormat formats a log line in a console format.
type Level ¶
type Level int
Level represents the predefined log level.
func LevelFromString ¶
LevelFromString converts a string to Level.
type Logger ¶
type Logger struct {
// contains filtered or unexported fields
}
Logger is a logger.
func New ¶
New creates a new Logger.
Example ¶
package main
import (
"os"
"github.com/hamba/logger/v2"
"github.com/hamba/logger/v2/ctx"
)
func main() {
log := logger.New(os.Stdout, logger.LogfmtFormat(), logger.Info).With(ctx.Str("env", "prod"))
log.Info("redis connection", ctx.Str("redis", "some redis name"), ctx.Int("timeout", 10))
}
Output:
func (*Logger) FromContext ¶ added in v2.10.0
FromContext returns a new Logger extended with any fields attached to ctx via WithContext. The context fields appear after the logger's own pre-rendered fields (set via With) and before per-call fields.
If the logger discards output or ctx carries no fields, the receiver is returned unchanged.
func (*Logger) Trace ¶ added in v2.6.0
Trace logs a trace message, intended for fine grained debug messages.
func (*Logger) WithTimestamp ¶ added in v2.2.0
func (l *Logger) WithTimestamp() (cancel func())
WithTimestamp adds a timestamp to each log lone. Sub-loggers will inherit the timestamp.
WithTimestamp is not thread safe.
type SyncWriter ¶
type SyncWriter struct {
// contains filtered or unexported fields
}
SyncWriter implements a writer that is synchronised with a lock.
Example ¶
package main
import (
"os"
"github.com/hamba/logger/v2"
"github.com/hamba/logger/v2/ctx"
)
func main() {
log := logger.New(logger.NewSyncWriter(os.Stdout), logger.LogfmtFormat(), logger.Info).With(ctx.Str("env", "prod"))
log.Info("redis connection", ctx.Str("redis", "some redis name"), ctx.Int("timeout", 10))
}
Output:
func NewSyncWriter ¶
func NewSyncWriter(w io.Writer) *SyncWriter
NewSyncWriter returns a synchronised writer.