Documentation
¶
Overview ¶
Package log provides a simple, leveled, fast, zero allocation, json structured logging package for Go
Example ¶
config := DefaultConfig
config.Level = DEBUG
config.Format = FormatText // Enable text logging
// New logger with added context
l := New(os.Stdout, config).
With(func(e Entry) {
e.String("app", "app1")
})
// Simple logging
l.Info("info message").String("key", "value").Write()
// Text format:
// time="2021-03-25T13:32:50.391Z" level="info" caller="_local/main.go:26" app="app1" message="info message" key="value"
// JSON format:
// {"time":"2021-03-25T13:33:20.547Z", "level":"info", "caller":"_local/main.go:26", "app":"app1", "message":"info message", "key":"value"}
l.Warn("warn message").Bool("flag", false).Write()
// Text format:
// time="2021-03-25T13:32:50.391Z" level="warn" caller="_local/main.go:29" app="app1" message="warn message" flag=false
// JSON format:
// {"time":"2021-03-25T13:33:20.547Z", "level":"warn", "caller":"_local/main.go:29", "app":"app1", "message":"warn message", "flag":false}
l.Error("caught an error").String("error", "request error").Write()
// Text format:
// time="2021-03-25T13:32:50.391Z" level="error" caller="_local/main.go:32" app="app1" message="caught an error" error="request error"
// JSON format:
// {"time":"2021-03-25T13:33:20.547Z", "level":"error", "caller":"_local/main.go:32", "app":"app1", "message":"caught an error", "error":"request error"}
l.Fatal("caught an unrecoverable error").Error("error", errors.New("some error")).Write()
// Text format:
// time="2021-03-25T13:32:50.391Z" level="fatal" caller="_local/main.go:35" app="app1" message="caught an unrecoverable error" error="some error"
// JSON format:
// {"time":"2021-03-25T13:33:20.547Z", "level":"fatal", "caller":"_local/main.go:35", "app":"app1", "message":"caught an unrecoverable error", "error":"some error"}
Index ¶
- Constants
- Variables
- func SetFormat(f Format)
- func SetLevel(l Level)
- type Array
- func (a Array) AppendBool(value bool) (array Array)
- func (a Array) AppendFloat(value float64) (array Array)
- func (a Array) AppendInt(value int64) (array Array)
- func (a Array) AppendNull() (array Array)
- func (a Array) AppendString(value string) (array Array)
- func (a Array) AppendUint(value uint64) (array Array)
- func (a Array) Array(key string, fn func(Array)) (array Array)
- func (a Array) Object(fn func(Object)) (array Array)
- type Config
- type Entry
- func (e Entry) Bool(key string, value bool) (entry Entry)
- func (e Entry) Bytes() (data []byte)
- func (e Entry) Duration(key string, value time.Duration) (entry Entry)
- func (e Entry) Error(key string, value error) (entry Entry)
- func (e Entry) Float32(key string, value float32) (entry Entry)
- func (e Entry) Float64(key string, value float64) (entry Entry)
- func (e Entry) Int(key string, value int) (entry Entry)
- func (e Entry) Int16(key string, value int16) (entry Entry)
- func (e Entry) Int32(key string, value int32) (entry Entry)
- func (e Entry) Int64(key string, value int64) (entry Entry)
- func (e Entry) Int8(key string, value int8) (entry Entry)
- func (e Entry) Level() (level Level)
- func (e Entry) Null(key string) (entry Entry)
- func (e Entry) Printf(key, format string, args ...interface{}) (entry Entry)
- func (e Entry) String(key string, value string) (entry Entry)
- func (e Entry) Time(key string, value time.Time) (entry Entry)
- func (e Entry) Uint(key string, value uint) (entry Entry)
- func (e Entry) Uint16(key string, value uint16) (entry Entry)
- func (e Entry) Uint32(key string, value uint32) (entry Entry)
- func (e Entry) Uint64(key string, value uint64) (entry Entry)
- func (e Entry) Uint8(key string, value uint8) (entry Entry)
- func (e Entry) Write()
- type Format
- type Level
- type Logger
- func (l *Logger) Debug(message string) (entry Entry)
- func (l *Logger) Error(message string) (entry Entry)
- func (l *Logger) Fatal(message string) (entry Entry)
- func (l *Logger) Hooks(f ...func(Entry)) (logger *Logger)
- func (l *Logger) Info(message string) (entry Entry)
- func (l *Logger) SetFormat(f Format)
- func (l *Logger) SetLevel(lv Level)
- func (l *Logger) Warn(message string) (entry Entry)
- func (l *Logger) With(f ...func(Entry)) (logger *Logger)
- type Object
- func (o Object) Bool(key string, value bool) (object Object)
- func (o Object) Error(key string, err error) (object Object)
- func (o Object) Float64(key string, value float64) (object Object)
- func (o Object) Int64(key string, value int64) (object Object)
- func (o Object) Null(key string) (object Object)
- func (o Object) String(key string, value string) (object Object)
- func (o Object) Uint64(key string, value uint64) (object Object)
Examples ¶
Constants ¶
const ( // DEBUG log level DEBUG = Level(1) // INFO log level INFO = Level(2) // WARN log level WARN = Level(3) // ERROR log level ERROR = Level(4) // FATAL log level FATAL = Level(5) )
const ( // ISO8601 time format ISO8601 = "2006-01-02T15:04:05.999Z07:00" // Unix time in seconds Unix = "unix" // UnixMilli time in milliseconds UnixMilli = "unix_milli" // UnixNano time in nanoseconds UnixNano = "unix_nano" )
Variables ¶
var ( // DefaultConfig for logger DefaultConfig = Config{ Format: FormatJSON, Level: INFO, EnableCaller: true, CallerSkip: 0, EnableTime: true, TimeField: "time", TimeFormat: ISO8601, MessageField: "message", LevelField: "level", EnableSampling: true, SamplingTick: time.Second, SamplingStart: 100, SamplingFactor: 100, } )
Functions ¶
Types ¶
type Array ¶ added in v0.0.2
type Array struct {
// contains filtered or unexported fields
}
Array value
func (Array) AppendBool ¶ added in v0.0.2
AppendBool value to array
func (Array) AppendFloat ¶ added in v0.0.2
AppendFloat value to array
func (Array) AppendNull ¶ added in v0.1.1
AppendNull value to array
func (Array) AppendString ¶ added in v0.0.2
AppendString value to array
func (Array) AppendUint ¶ added in v0.0.2
AppendUint value to array
type Config ¶
type Config struct {
Format Format // Log format
Level Level // Log level
EnableCaller bool // Enable caller info
CallerSkip int // Skip level of callers, useful if wrapping the logger
EnableTime bool // Enable log timestamps
TimeField string // Field name for the log timestamp
TimeFormat string // Time Format for log timestamp
MessageField string // Field name for the log message
LevelField string // Field name for the log level
EnableSampling bool // Enable log sampling to reduce CPU and I/O load
SamplingTick time.Duration // Resolution at which entries will be sampled
SamplingStart int // Start sampling after this number of similar entries within SamplingTick
SamplingFactor int // Reduction factor when sampling
}
Config type for logger
type Entry ¶
type Entry struct {
// contains filtered or unexported fields
}
Entry is a structured log entry. A entry is not safe for concurrent use. A entry must be logged by calling Log(), and cannot be reused after.
func Debug ¶ added in v0.2.3
Debug creates a new log entry with the given message with the default package logger.
func Error ¶ added in v0.2.3
Error creates a new log entry with the given message with the default package logger.
func Fatal ¶ added in v0.5.1
Fatal creates a new log entry with the given message with the default package logger. After write, Fatal calls os.Exit(1) terminating the running program
func Info ¶ added in v0.2.3
Info creates a new log entry with the given message with the default package logger.
func Warn ¶ added in v0.2.3
Warn creates a new log entry with the given message with the default package logger.
func (Entry) Bytes ¶
Bytes return the current entry bytes. This is intended to be used in hooks That will be applied after calling Log(). The returned []byte is not a copy and must not be modified directly.
func (Entry) Printf ¶ added in v0.5.4
Printf parses the format and args adding it as a key/string value in the log entry. This method is helpful to avoid allocations and extra work when logging with a lower log level than the logger is working with.
type Format ¶ added in v0.5.1
type Format uint32
Format is the logging format
func ParseFormat ¶ added in v0.5.2
ParseFormat parses the log format from a string
type Level ¶
type Level uint32
Level represents the supported log levels
func ParseLevel ¶ added in v0.3.0
ParseLevel parses the log level from a string
type Logger ¶
type Logger struct {
// contains filtered or unexported fields
}
Logger type
func New ¶
New creates a new logger with the give config and writer. A nill writer will be set to ioutil.Discard.
func (*Logger) Fatal ¶ added in v0.4.0
Fatal creates a new log entry with the given message. After write, Fatal calls os.Exit(1) terminating the running program
func (*Logger) Hooks ¶
Hooks creates a new logger with functions to apply after the entry is written. Hooks are cumulative and useful for shipping log data to other systems.
type Object ¶ added in v0.0.2
type Object struct {
// contains filtered or unexported fields
}
Object value