Documentation ¶
Overview ¶
Package zerolog provides a lightweight logging library dedicated to JSON logging.
A global Logger can be use for simple logging:
import "github.com/rs/zerolog/log" log.Info().Msg("hello world") // Output: {"time":1494567715,"level":"info","message":"hello world"}
NOTE: To import the global logger, import the "log" subpackage "github.com/rs/zerolog/log".
Fields can be added to log messages:
log.Info().Str("foo", "bar").Msg("hello world") // Output: {"time":1494567715,"level":"info","message":"hello world","foo":"bar"}
Create logger instance to manage different outputs:
logger := zerolog.New(os.Stderr).With().Timestamp().Logger() logger.Info(). Str("foo", "bar"). Msg("hello world") // Output: {"time":1494567715,"level":"info","message":"hello world","foo":"bar"}
Sub-loggers let you chain loggers with additional context:
sublogger := log.With().Str("component": "foo").Logger() sublogger.Info().Msg("hello world") // Output: {"time":1494567715,"level":"info","message":"hello world","component":"foo"}
Level logging
zerolog.SetGlobalLevel(zerolog.InfoLevel) log.Debug().Msg("filtered out message") log.Info().Msg("routed message") if e := log.Debug(); e.Enabled() { // Compute log output only if enabled. value := compute() e.Str("foo": value).Msg("some debug message") } // Output: {"level":"info","time":1494567715,"routed message"}
Customize automatic field names:
log.TimestampFieldName = "t" log.LevelFieldName = "p" log.MessageFieldName = "m" log.Info().Msg("hello world") // Output: {"t":1494567715,"p":"info","m":"hello world"}
Log with no level and message:
log.Log().Str("foo","bar").Msg("") // Output: {"time":1494567715,"foo":"bar"}
Add contextual fields to global Logger:
log.Logger = log.With().Str("foo", "bar").Logger()
Sample logs:
sampled := log.Sample(10) sampled.Info().Msg("will be logged every 10 messages")
Index ¶
- Constants
- Variables
- func DisableSampling(v bool)
- func SetGlobalLevel(l Level)
- func SyncWriter(w io.Writer) io.Writer
- type Array
- func (a *Array) Bool(b bool) *Array
- func (a *Array) Bytes(val []byte) *Array
- func (a *Array) Dur(d time.Duration) *Array
- func (a *Array) Err(err error) *Array
- func (a *Array) Float32(f float32) *Array
- func (a *Array) Float64(f float64) *Array
- func (a *Array) Int(i int) *Array
- func (a *Array) Int16(i int16) *Array
- func (a *Array) Int32(i int32) *Array
- func (a *Array) Int64(i int64) *Array
- func (a *Array) Int8(i int8) *Array
- func (a *Array) Interface(i interface{}) *Array
- func (*Array) MarshalZerologArray(*Array)
- func (a *Array) Object(obj LogObjectMarshaler) *Array
- func (a *Array) Str(val string) *Array
- func (a *Array) Time(t time.Time) *Array
- func (a *Array) Uint(i uint) *Array
- func (a *Array) Uint16(i uint16) *Array
- func (a *Array) Uint32(i uint32) *Array
- func (a *Array) Uint64(i uint64) *Array
- func (a *Array) Uint8(i uint8) *Array
- type Context
- func (c Context) AnErr(key string, err error) Context
- func (c Context) Array(key string, arr LogArrayMarshaler) Context
- func (c Context) Bool(key string, b bool) Context
- func (c Context) Bools(key string, b []bool) Context
- func (c Context) Bytes(key string, val []byte) Context
- func (c Context) Dict(key string, dict *Event) Context
- func (c Context) Dur(key string, d time.Duration) Context
- func (c Context) Durs(key string, d []time.Duration) Context
- func (c Context) Err(err error) Context
- func (c Context) Errs(key string, errs []error) Context
- func (c Context) Fields(fields map[string]interface{}) Context
- func (c Context) Float32(key string, f float32) Context
- func (c Context) Float64(key string, f float64) Context
- func (c Context) Floats32(key string, f []float32) Context
- func (c Context) Floats64(key string, f []float64) Context
- func (c Context) Int(key string, i int) Context
- func (c Context) Int16(key string, i int16) Context
- func (c Context) Int32(key string, i int32) Context
- func (c Context) Int64(key string, i int64) Context
- func (c Context) Int8(key string, i int8) Context
- func (c Context) Interface(key string, i interface{}) Context
- func (c Context) Ints(key string, i []int) Context
- func (c Context) Ints16(key string, i []int16) Context
- func (c Context) Ints32(key string, i []int32) Context
- func (c Context) Ints64(key string, i []int64) Context
- func (c Context) Ints8(key string, i []int8) Context
- func (c Context) Logger() Logger
- func (c Context) Object(key string, obj LogObjectMarshaler) Context
- func (c Context) Str(key, val string) Context
- func (c Context) Strs(key string, vals []string) Context
- func (c Context) Time(key string, t time.Time) Context
- func (c Context) Times(key string, t []time.Time) Context
- func (c Context) Timestamp() Context
- func (c Context) Uint(key string, i uint) Context
- func (c Context) Uint16(key string, i uint16) Context
- func (c Context) Uint32(key string, i uint32) Context
- func (c Context) Uint64(key string, i uint64) Context
- func (c Context) Uint8(key string, i uint8) Context
- func (c Context) Uints(key string, i []uint) Context
- func (c Context) Uints16(key string, i []uint16) Context
- func (c Context) Uints32(key string, i []uint32) Context
- func (c Context) Uints64(key string, i []uint64) Context
- func (c Context) Uints8(key string, i []uint8) Context
- type Event
- func (e *Event) AnErr(key string, err error) *Event
- func (e *Event) Array(key string, arr LogArrayMarshaler) *Event
- func (e *Event) Bool(key string, b bool) *Event
- func (e *Event) Bools(key string, b []bool) *Event
- func (e *Event) Bytes(key string, val []byte) *Event
- func (e *Event) Dict(key string, dict *Event) *Event
- func (e *Event) Dur(key string, d time.Duration) *Event
- func (e *Event) Durs(key string, d []time.Duration) *Event
- func (e *Event) Enabled() bool
- func (e *Event) Err(err error) *Event
- func (e *Event) Errs(key string, errs []error) *Event
- func (e *Event) Fields(fields map[string]interface{}) *Event
- func (e *Event) Float32(key string, f float32) *Event
- func (e *Event) Float64(key string, f float64) *Event
- func (e *Event) Floats32(key string, f []float32) *Event
- func (e *Event) Floats64(key string, f []float64) *Event
- func (e *Event) Int(key string, i int) *Event
- func (e *Event) Int16(key string, i int16) *Event
- func (e *Event) Int32(key string, i int32) *Event
- func (e *Event) Int64(key string, i int64) *Event
- func (e *Event) Int8(key string, i int8) *Event
- func (e *Event) Interface(key string, i interface{}) *Event
- func (e *Event) Ints(key string, i []int) *Event
- func (e *Event) Ints16(key string, i []int16) *Event
- func (e *Event) Ints32(key string, i []int32) *Event
- func (e *Event) Ints64(key string, i []int64) *Event
- func (e *Event) Ints8(key string, i []int8) *Event
- func (e *Event) Msg(msg string)
- func (e *Event) Msgf(format string, v ...interface{})
- func (e *Event) Object(key string, obj LogObjectMarshaler) *Event
- func (e *Event) Str(key, val string) *Event
- func (e *Event) Strs(key string, vals []string) *Event
- func (e *Event) Time(key string, t time.Time) *Event
- func (e *Event) TimeDiff(key string, t time.Time, start time.Time) *Event
- func (e *Event) Times(key string, t []time.Time) *Event
- func (e *Event) Timestamp() *Event
- func (e *Event) Uint(key string, i uint) *Event
- func (e *Event) Uint16(key string, i uint16) *Event
- func (e *Event) Uint32(key string, i uint32) *Event
- func (e *Event) Uint64(key string, i uint64) *Event
- func (e *Event) Uint8(key string, i uint8) *Event
- func (e *Event) Uints(key string, i []uint) *Event
- func (e *Event) Uints16(key string, i []uint16) *Event
- func (e *Event) Uints32(key string, i []uint32) *Event
- func (e *Event) Uints64(key string, i []uint64) *Event
- func (e *Event) Uints8(key string, i []uint8) *Event
- type Level
- type LevelWriter
- type LogArrayMarshaler
- type LogObjectMarshaler
- type Logger
- func (l Logger) Debug() *Event
- func (l Logger) Error() *Event
- func (l Logger) Fatal() *Event
- func (l Logger) Info() *Event
- func (l Logger) Level(lvl Level) Logger
- func (l Logger) Log() *Event
- func (l Logger) Output(w io.Writer) Logger
- func (l Logger) Panic() *Event
- func (l Logger) Sample(every int) Logger
- func (l Logger) Warn() *Event
- func (l Logger) With() Context
- func (l Logger) WithContext(ctx context.Context) context.Context
- func (l Logger) WithLevel(level Level) *Event
- func (l Logger) Write(p []byte) (n int, err error)
- type SyslogWriter
Examples ¶
- Context.Array
- Context.Array (Object)
- Context.Dict
- Context.Dur
- Context.Durs
- Context.Interface
- Context.Object
- Event.Array
- Event.Array (Object)
- Event.Dict
- Event.Dur
- Event.Durs
- Event.Interface
- Event.Object
- Logger.Debug
- Logger.Error
- Logger.Info
- Logger.Level
- Logger.Log
- Logger.Sample
- Logger.Warn
- Logger.With
- Logger.WithLevel
- Logger.Write
- New
Constants ¶
const ( // Often samples log every 10 events. Often = 10 // Sometimes samples log every 100 events. Sometimes = 100 // Rarely samples log every 1000 events. Rarely = 1000 )
Variables ¶
var ( // TimestampFieldName is the field name used for the timestamp field. TimestampFieldName = "time" // LevelFieldName is the field name used for the level field. LevelFieldName = "level" // MessageFieldName is the field name used for the message field. MessageFieldName = "message" // ErrorFieldName is the field name used for error fields. ErrorFieldName = "error" // SampleFieldName is the name of the field used to report sampling. SampleFieldName = "sample" // TimeFieldFormat defines the time format of the Time field type. // If set to an empty string, the time is formatted as an UNIX timestamp // as integer. TimeFieldFormat = time.RFC3339 // TimestampFunc defines the function called to generate a timestamp. TimestampFunc = time.Now // DurationFieldUnit defines the unit for time.Duration type fields added // using the Dur method. DurationFieldUnit = time.Millisecond // DurationFieldInteger renders Dur fields as integer instead of float if // set to true. DurationFieldInteger = false )
Functions ¶
func DisableSampling ¶
func DisableSampling(v bool)
DisableSampling will disable sampling in all Loggers if true.
func SetGlobalLevel ¶
func SetGlobalLevel(l Level)
SetGlobalLevel sets the global override for log level. If this values is raised, all Loggers will use at least this value.
To globally disable logs, set GlobalLevel to Disabled.
func SyncWriter ¶
SyncWriter wraps w so that each call to Write is synchronized with a mutex. This syncer can be the call to writer's Write method is not thread safe. Note that os.File Write operation is using write() syscall which is supposed to be thread-safe on POSIX systems. So there is no need to use this with os.File on such systems as zerolog guaranties to issue a single Write call per log event.
Types ¶
type Array ¶ added in v1.1.0
type Array struct {
// contains filtered or unexported fields
}
func Arr ¶ added in v1.1.0
func Arr() *Array
Arr creates an array to be added to an Event or Context.
func (*Array) MarshalZerologArray ¶ added in v1.1.0
func (*Array) Object ¶ added in v1.1.0
func (a *Array) Object(obj LogObjectMarshaler) *Array
Object marshals an object that implement the LogObjectMarshaler interface and append it to the array.
func (*Array) Time ¶ added in v1.1.0
Time append t formated as string using zerolog.TimeFieldFormat.
type Context ¶
type Context struct {
// contains filtered or unexported fields
}
Context configures a new sub-logger with contextual fields.
func (Context) Array ¶ added in v1.1.0
func (c Context) Array(key string, arr LogArrayMarshaler) Context
Array adds the field key with an array to the event context. Use zerolog.Arr() to create the array or pass a type that implement the LogArrayMarshaler interface.
Example ¶
package main import ( "os" "github.com/rs/zerolog" ) func main() { log := zerolog.New(os.Stdout).With(). Str("foo", "bar"). Array("array", zerolog.Arr(). Str("baz"). Int(1), ).Logger() log.Log().Msg("hello world") }
Output: {"foo":"bar","array":["baz",1],"message":"hello world"}
Example (Object) ¶
package main import ( "os" "time" "github.com/rs/zerolog" ) type User struct { Name string Age int Created time.Time } func (u User) MarshalZerologObject(e *zerolog.Event) { e.Str("name", u.Name). Int("age", u.Age). Time("created", u.Created) } type Users []User func (uu Users) MarshalZerologArray(a *zerolog.Array) { for _, u := range uu { a.Object(u) } } func main() { // Users implements zerolog.LogArrayMarshaler u := Users{ User{"John", 35, time.Time{}}, User{"Bob", 55, time.Time{}}, } log := zerolog.New(os.Stdout).With(). Str("foo", "bar"). Array("users", u). Logger() log.Log().Msg("hello world") }
Output: {"foo":"bar","users":[{"name":"John","age":35,"created":"0001-01-01T00:00:00Z"},{"name":"Bob","age":55,"created":"0001-01-01T00:00:00Z"}],"message":"hello world"}
func (Context) Bools ¶ added in v1.0.1
Bools adds the field key with val as a []bool to the logger context.
func (Context) Bytes ¶ added in v1.0.1
Bytes adds the field key with val as a []byte to the logger context.
func (Context) Dict ¶
Dict adds the field key with the dict to the logger context.
Example ¶
package main import ( "os" "github.com/rs/zerolog" ) func main() { log := zerolog.New(os.Stdout).With(). Str("foo", "bar"). Dict("dict", zerolog.Dict(). Str("bar", "baz"). Int("n", 1), ).Logger() log.Log().Msg("hello world") }
Output: {"foo":"bar","dict":{"bar":"baz","n":1},"message":"hello world"}
func (Context) Dur ¶
Dur adds the fields key with d divided by unit and stored as a float.
Example ¶
package main import ( "os" "time" "github.com/rs/zerolog" ) func main() { d := time.Duration(10 * time.Second) log := zerolog.New(os.Stdout).With(). Str("foo", "bar"). Dur("dur", d). Logger() log.Log().Msg("hello world") }
Output: {"foo":"bar","dur":10000,"message":"hello world"}
func (Context) Durs ¶ added in v1.0.1
Durs adds the fields key with d divided by unit and stored as a float.
Example ¶
package main import ( "os" "time" "github.com/rs/zerolog" ) func main() { d := []time.Duration{ time.Duration(10 * time.Second), time.Duration(20 * time.Second), } log := zerolog.New(os.Stdout).With(). Str("foo", "bar"). Durs("durs", d). Logger() log.Log().Msg("hello world") }
Output: {"foo":"bar","durs":[10000,20000],"message":"hello world"}
func (Context) Err ¶
Err adds the field "error" with err as a string to the logger context. To customize the key name, change zerolog.ErrorFieldName.
func (Context) Errs ¶ added in v1.0.1
Errs adds the field key with errs as an array of strings to the logger context.
func (Context) Fields ¶ added in v1.0.1
Fields is a helper function to use a map to set fields using type assertion.
func (Context) Floats32 ¶ added in v1.0.1
Floats32 adds the field key with f as a []float32 to the logger context.
func (Context) Floats64 ¶ added in v1.0.1
Floats64 adds the field key with f as a []float64 to the logger context.
func (Context) Interface ¶
Interface adds the field key with obj marshaled using reflection.
Example ¶
package main import ( "os" "github.com/rs/zerolog" ) func main() { obj := struct { Name string `json:"name"` }{ Name: "john", } log := zerolog.New(os.Stdout).With(). Str("foo", "bar"). Interface("obj", obj). Logger() log.Log().Msg("hello world") }
Output: {"foo":"bar","obj":{"name":"john"},"message":"hello world"}
func (Context) Ints ¶ added in v1.0.1
Ints adds the field key with i as a []int to the logger context.
func (Context) Ints16 ¶ added in v1.0.1
Ints16 adds the field key with i as a []int16 to the logger context.
func (Context) Ints32 ¶ added in v1.0.1
Ints32 adds the field key with i as a []int32 to the logger context.
func (Context) Ints64 ¶ added in v1.0.1
Ints64 adds the field key with i as a []int64 to the logger context.
func (Context) Ints8 ¶ added in v1.0.1
Ints8 adds the field key with i as a []int8 to the logger context.
func (Context) Object ¶ added in v1.0.3
func (c Context) Object(key string, obj LogObjectMarshaler) Context
Object marshals an object that implement the LogObjectMarshaler interface.
Example ¶
package main import ( "os" "time" "github.com/rs/zerolog" ) type User struct { Name string Age int Created time.Time } func (u User) MarshalZerologObject(e *zerolog.Event) { e.Str("name", u.Name). Int("age", u.Age). Time("created", u.Created) } func main() { // User implements zerolog.LogObjectMarshaler u := User{"John", 35, time.Time{}} log := zerolog.New(os.Stdout).With(). Str("foo", "bar"). Object("user", u). Logger() log.Log().Msg("hello world") }
Output: {"foo":"bar","user":{"name":"John","age":35,"created":"0001-01-01T00:00:00Z"},"message":"hello world"}
func (Context) Strs ¶ added in v1.0.1
Strs adds the field key with val as a string to the logger context.
func (Context) Time ¶
Time adds the field key with t formated as string using zerolog.TimeFieldFormat.
func (Context) Times ¶ added in v1.0.1
Times adds the field key with t formated as string using zerolog.TimeFieldFormat.
func (Context) Timestamp ¶
Timestamp adds the current local time as UNIX timestamp to the logger context with the "time" key. To customize the key name, change zerolog.TimestampFieldName.
func (Context) Uints ¶ added in v1.0.1
Uints adds the field key with i as a []uint to the logger context.
func (Context) Uints16 ¶ added in v1.0.1
Uints16 adds the field key with i as a []uint16 to the logger context.
func (Context) Uints32 ¶ added in v1.0.1
Uints32 adds the field key with i as a []uint32 to the logger context.
type Event ¶
type Event struct {
// contains filtered or unexported fields
}
Event represents a log event. It is instanced by one of the level method of Logger and finalized by the Msg or Msgf method.
func Dict ¶
func Dict() *Event
Dict creates an Event to be used with the *Event.Dict method. Call usual field methods like Str, Int etc to add fields to this event and give it as argument the *Event.Dict method.
func (*Event) AnErr ¶
AnErr adds the field key with err as a string to the *Event context. If err is nil, no field is added.
func (*Event) Array ¶ added in v1.1.0
func (e *Event) Array(key string, arr LogArrayMarshaler) *Event
Array adds the field key with an array to the event context. Use zerolog.Arr() to create the array or pass a type that implement the LogArrayMarshaler interface.
Example ¶
package main import ( "os" "github.com/rs/zerolog" ) func main() { log := zerolog.New(os.Stdout) log.Log(). Str("foo", "bar"). Array("array", zerolog.Arr(). Str("baz"). Int(1), ). Msg("hello world") }
Output: {"foo":"bar","array":["baz",1],"message":"hello world"}
Example (Object) ¶
package main import ( "os" "time" "github.com/rs/zerolog" ) type User struct { Name string Age int Created time.Time } func (u User) MarshalZerologObject(e *zerolog.Event) { e.Str("name", u.Name). Int("age", u.Age). Time("created", u.Created) } type Users []User func (uu Users) MarshalZerologArray(a *zerolog.Array) { for _, u := range uu { a.Object(u) } } func main() { log := zerolog.New(os.Stdout) // Users implements zerolog.LogArrayMarshaler u := Users{ User{"John", 35, time.Time{}}, User{"Bob", 55, time.Time{}}, } log.Log(). Str("foo", "bar"). Array("users", u). Msg("hello world") }
Output: {"foo":"bar","users":[{"name":"John","age":35,"created":"0001-01-01T00:00:00Z"},{"name":"Bob","age":55,"created":"0001-01-01T00:00:00Z"}],"message":"hello world"}
func (*Event) Bools ¶ added in v1.0.1
Bools adds the field key with val as a []bool to the *Event context.
func (*Event) Bytes ¶ added in v1.0.1
Bytes adds the field key with val as a string to the *Event context.
Runes outside of normal ASCII ranges will be hex-encoded in the resulting JSON.
func (*Event) Dict ¶
Dict adds the field key with a dict to the event context. Use zerolog.Dict() to create the dictionary.
Example ¶
package main import ( "os" "github.com/rs/zerolog" ) func main() { log := zerolog.New(os.Stdout) log.Log(). Str("foo", "bar"). Dict("dict", zerolog.Dict(). Str("bar", "baz"). Int("n", 1), ). Msg("hello world") }
Output: {"foo":"bar","dict":{"bar":"baz","n":1},"message":"hello world"}
func (*Event) Dur ¶
Dur adds the field key with duration d stored as zerolog.DurationFieldUnit. If zerolog.DurationFieldInteger is true, durations are rendered as integer instead of float.
Example ¶
package main import ( "os" "time" "github.com/rs/zerolog" ) func main() { d := time.Duration(10 * time.Second) log := zerolog.New(os.Stdout) log.Log(). Str("foo", "bar"). Dur("dur", d). Msg("hello world") }
Output: {"foo":"bar","dur":10000,"message":"hello world"}
func (*Event) Durs ¶ added in v1.0.1
Durs adds the field key with duration d stored as zerolog.DurationFieldUnit. If zerolog.DurationFieldInteger is true, durations are rendered as integer instead of float.
Example ¶
package main import ( "os" "time" "github.com/rs/zerolog" ) func main() { d := []time.Duration{ time.Duration(10 * time.Second), time.Duration(20 * time.Second), } log := zerolog.New(os.Stdout) log.Log(). Str("foo", "bar"). Durs("durs", d). Msg("hello world") }
Output: {"foo":"bar","durs":[10000,20000],"message":"hello world"}
func (*Event) Enabled ¶
Enabled return false if the *Event is going to be filtered out by log level or sampling.
func (*Event) Err ¶
Err adds the field "error" with err as a string to the *Event context. If err is nil, no field is added. To customize the key name, change zerolog.ErrorFieldName.
func (*Event) Errs ¶ added in v1.0.1
Errs adds the field key with errs as an array of strings to the *Event context. If err is nil, no field is added.
func (*Event) Fields ¶ added in v1.0.1
Fields is a helper function to use a map to set fields using type assertion.
func (*Event) Floats32 ¶ added in v1.0.1
Floats32 adds the field key with f as a []float32 to the *Event context.
func (*Event) Floats64 ¶ added in v1.0.1
Floats64 adds the field key with f as a []float64 to the *Event context.
func (*Event) Interface ¶
Interface adds the field key with i marshaled using reflection.
Example ¶
package main import ( "os" "github.com/rs/zerolog" ) func main() { log := zerolog.New(os.Stdout) obj := struct { Name string `json:"name"` }{ Name: "john", } log.Log(). Str("foo", "bar"). Interface("obj", obj). Msg("hello world") }
Output: {"foo":"bar","obj":{"name":"john"},"message":"hello world"}
func (*Event) Ints ¶ added in v1.0.1
Ints adds the field key with i as a []int to the *Event context.
func (*Event) Ints16 ¶ added in v1.0.1
Ints16 adds the field key with i as a []int16 to the *Event context.
func (*Event) Ints32 ¶ added in v1.0.1
Ints32 adds the field key with i as a []int32 to the *Event context.
func (*Event) Ints64 ¶ added in v1.0.1
Ints64 adds the field key with i as a []int64 to the *Event context.
func (*Event) Ints8 ¶ added in v1.0.1
Ints8 adds the field key with i as a []int8 to the *Event context.
func (*Event) Msg ¶
Msg sends the *Event with msg added as the message field if not empty.
NOTICE: once this method is called, the *Event should be disposed. Calling Msg twice can have unexpected result.
func (*Event) Msgf ¶
Msgf sends the event with formated msg added as the message field if not empty.
NOTICE: once this methid is called, the *Event should be disposed. Calling Msg twice can have unexpected result.
func (*Event) Object ¶ added in v1.0.3
func (e *Event) Object(key string, obj LogObjectMarshaler) *Event
Object marshals an object that implement the LogObjectMarshaler interface.
Example ¶
package main import ( "os" "time" "github.com/rs/zerolog" ) type User struct { Name string Age int Created time.Time } func (u User) MarshalZerologObject(e *zerolog.Event) { e.Str("name", u.Name). Int("age", u.Age). Time("created", u.Created) } func main() { log := zerolog.New(os.Stdout) // User implements zerolog.LogObjectMarshaler u := User{"John", 35, time.Time{}} log.Log(). Str("foo", "bar"). Object("user", u). Msg("hello world") }
Output: {"foo":"bar","user":{"name":"John","age":35,"created":"0001-01-01T00:00:00Z"},"message":"hello world"}
func (*Event) Strs ¶ added in v1.0.1
Strs adds the field key with vals as a []string to the *Event context.
func (*Event) Time ¶
Time adds the field key with t formated as string using zerolog.TimeFieldFormat.
func (*Event) TimeDiff ¶
TimeDiff adds the field key with positive duration between time t and start. If time t is not greater than start, duration will be 0. Duration format follows the same principle as Dur().
func (*Event) Times ¶ added in v1.0.1
Times adds the field key with t formated as string using zerolog.TimeFieldFormat.
func (*Event) Timestamp ¶
Timestamp adds the current local time as UNIX timestamp to the *Event context with the "time" key. To customize the key name, change zerolog.TimestampFieldName.
func (*Event) Uints ¶ added in v1.0.1
Uints adds the field key with i as a []int to the *Event context.
func (*Event) Uints16 ¶ added in v1.0.1
Uints16 adds the field key with i as a []int16 to the *Event context.
func (*Event) Uints32 ¶ added in v1.0.1
Uints32 adds the field key with i as a []int32 to the *Event context.
type Level ¶
type Level uint8
Level defines log levels.
const ( // DebugLevel defines debug log level. DebugLevel Level = iota // InfoLevel defines info log level. InfoLevel // WarnLevel defines warn log level. WarnLevel // ErrorLevel defines error log level. ErrorLevel // FatalLevel defines fatal log level. FatalLevel // PanicLevel defines panic log level. PanicLevel // Disabled disables the logger. Disabled )
type LevelWriter ¶
LevelWriter defines as interface a writer may implement in order to receive level information with payload.
func MultiLevelWriter ¶
func MultiLevelWriter(writers ...io.Writer) LevelWriter
MultiLevelWriter creates a writer that duplicates its writes to all the provided writers, similar to the Unix tee(1) command. If some writers implement LevelWriter, their WriteLevel method will be used instead of Write.
func SyslogLevelWriter ¶
func SyslogLevelWriter(w SyslogWriter) LevelWriter
SyslogLevelWriter wraps a SyslogWriter and call the right syslog level method matching the zerolog level.
type LogArrayMarshaler ¶ added in v1.1.0
type LogArrayMarshaler interface {
MarshalZerologArray(a *Array)
}
LogArrayMarshaler provides a strongly-typed and encoding-agnostic interface to be implemented by types used with Event/Context's Array methods.
type LogObjectMarshaler ¶ added in v1.0.3
type LogObjectMarshaler interface {
MarshalZerologObject(e *Event)
}
LogObjectMarshaler provides a strongly-typed and encoding-agnostic interface to be implemented by types used with Event/Context's Object methods.
type Logger ¶
type Logger struct {
// contains filtered or unexported fields
}
A Logger represents an active logging object that generates lines of JSON output to an io.Writer. Each logging operation makes a single call to the Writer's Write method. There is no guaranty on access serialization to the Writer. If your Writer is not thread safe, you may consider a sync wrapper.
func Ctx ¶
Ctx returns the Logger associated with the ctx. If no logger is associated, a disabled logger is returned.
func New ¶
New creates a root logger with given output writer. If the output writer implements the LevelWriter interface, the WriteLevel method will be called instead of the Write one.
Each logging operation makes a single call to the Writer's Write method. There is no guaranty on access serialization to the Writer. If your Writer is not thread safe, you may consider using sync wrapper.
Example ¶
package main import ( "os" "github.com/rs/zerolog" ) func main() { log := zerolog.New(os.Stdout) log.Info().Msg("hello world") }
Output: {"level":"info","message":"hello world"}
func (Logger) Debug ¶
Debug starts a new message with debug level.
You must call Msg on the returned event in order to send the event.
Example ¶
package main import ( "os" "github.com/rs/zerolog" ) func main() { log := zerolog.New(os.Stdout) log.Debug(). Str("foo", "bar"). Int("n", 123). Msg("hello world") }
Output: {"level":"debug","foo":"bar","n":123,"message":"hello world"}
func (Logger) Error ¶
Error starts a new message with error level.
You must call Msg on the returned event in order to send the event.
Example ¶
package main import ( "errors" "os" "github.com/rs/zerolog" ) func main() { log := zerolog.New(os.Stdout) log.Error(). Err(errors.New("some error")). Msg("error doing something") }
Output: {"level":"error","error":"some error","message":"error doing something"}
func (Logger) Fatal ¶
Fatal starts a new message with fatal level. The os.Exit(1) function is called by the Msg method.
You must call Msg on the returned event in order to send the event.
func (Logger) Info ¶
Info starts a new message with info level.
You must call Msg on the returned event in order to send the event.
Example ¶
package main import ( "os" "github.com/rs/zerolog" ) func main() { log := zerolog.New(os.Stdout) log.Info(). Str("foo", "bar"). Int("n", 123). Msg("hello world") }
Output: {"level":"info","foo":"bar","n":123,"message":"hello world"}
func (Logger) Level ¶
Level creates a child logger with the minimum accepted level set to level.
Example ¶
package main import ( "os" "github.com/rs/zerolog" ) func main() { log := zerolog.New(os.Stdout).Level(zerolog.WarnLevel) log.Info().Msg("filtered out message") log.Error().Msg("kept message") }
Output: {"level":"error","message":"kept message"}
func (Logger) Log ¶
Log starts a new message with no level. Setting GlobalLevel to Disabled will still disable events produced by this method.
You must call Msg on the returned event in order to send the event.
Example ¶
package main import ( "os" "github.com/rs/zerolog" ) func main() { log := zerolog.New(os.Stdout) log.Log(). Str("foo", "bar"). Str("bar", "baz"). Msg("") }
Output: {"foo":"bar","bar":"baz"}
func (Logger) Output ¶ added in v1.2.0
Output duplicates the current logger and sets w as its output.
func (Logger) Panic ¶
Panic starts a new message with panic level. The message is also sent to the panic function.
You must call Msg on the returned event in order to send the event.
func (Logger) Sample ¶
Sample returns a logger that only let one message out of every to pass thru.
Example ¶
package main import ( "os" "github.com/rs/zerolog" ) func main() { log := zerolog.New(os.Stdout).Sample(2) log.Info().Msg("message 1") log.Info().Msg("message 2") log.Info().Msg("message 3") log.Info().Msg("message 4") }
Output: {"level":"info","sample":2,"message":"message 2"} {"level":"info","sample":2,"message":"message 4"}
func (Logger) Warn ¶
Warn starts a new message with warn level.
You must call Msg on the returned event in order to send the event.
Example ¶
package main import ( "os" "github.com/rs/zerolog" ) func main() { log := zerolog.New(os.Stdout) log.Warn(). Str("foo", "bar"). Msg("a warning message") }
Output: {"level":"warn","foo":"bar","message":"a warning message"}
func (Logger) With ¶
With creates a child logger with the field added to its context.
Example ¶
package main import ( "os" "github.com/rs/zerolog" ) func main() { log := zerolog.New(os.Stdout). With(). Str("foo", "bar"). Logger() log.Info().Msg("hello world") }
Output: {"level":"info","foo":"bar","message":"hello world"}
func (Logger) WithContext ¶
WithContext returns a copy of ctx with l associated.
func (Logger) WithLevel ¶ added in v1.0.1
WithLevel starts a new message with level.
You must call Msg on the returned event in order to send the event.
Example ¶
package main import ( "os" "github.com/rs/zerolog" ) func main() { log := zerolog.New(os.Stdout) log.WithLevel(zerolog.InfoLevel). Msg("hello world") }
Output: {"level":"info","message":"hello world"}
func (Logger) Write ¶
Write implements the io.Writer interface. This is useful to set as a writer for the standard library log.
Example ¶
package main import ( stdlog "log" "os" "github.com/rs/zerolog" ) func main() { log := zerolog.New(os.Stdout).With(). Str("foo", "bar"). Logger() stdlog.SetFlags(0) stdlog.SetOutput(log) stdlog.Print("hello world") }
Output: {"foo":"bar","message":"hello world"}