2017-05-12 05:24:39 +00:00
|
|
|
package zerolog
|
|
|
|
|
|
|
|
import "time"
|
|
|
|
|
|
|
|
// Context configures a new sub-logger with contextual fields.
|
|
|
|
type Context struct {
|
|
|
|
l Logger
|
|
|
|
}
|
|
|
|
|
|
|
|
// Logger returns the logger with the context previously set.
|
|
|
|
func (c Context) Logger() Logger {
|
|
|
|
return c.l
|
|
|
|
}
|
|
|
|
|
2017-05-18 07:10:45 +00:00
|
|
|
// Dict adds the field key with the dict to the logger context.
|
2017-05-20 02:43:59 +00:00
|
|
|
func (c Context) Dict(key string, dict *Event) Context {
|
|
|
|
dict.buf = append(dict.buf, '}')
|
|
|
|
c.l.context = append(appendKey(c.l.context, key), dict.buf...)
|
|
|
|
eventPool.Put(dict)
|
|
|
|
return c
|
2017-05-18 07:10:45 +00:00
|
|
|
}
|
|
|
|
|
2017-05-17 04:52:22 +00:00
|
|
|
// Str adds the field key with val as a string to the logger context.
|
2017-05-12 05:24:39 +00:00
|
|
|
func (c Context) Str(key, val string) Context {
|
2017-05-20 02:43:59 +00:00
|
|
|
c.l.context = appendString(c.l.context, key, val)
|
|
|
|
return c
|
2017-05-12 05:24:39 +00:00
|
|
|
}
|
|
|
|
|
2017-05-17 04:52:22 +00:00
|
|
|
// Err adds the field "error" with err as a string to the logger context.
|
|
|
|
// To customize the key name, change zerolog.ErrorFieldName.
|
2017-05-12 05:24:39 +00:00
|
|
|
func (c Context) Err(err error) Context {
|
2017-05-20 02:43:59 +00:00
|
|
|
c.l.context = appendError(c.l.context, err)
|
|
|
|
return c
|
2017-05-12 05:24:39 +00:00
|
|
|
}
|
|
|
|
|
2017-05-17 04:52:22 +00:00
|
|
|
// Bool adds the field key with val as a Boolean to the logger context.
|
2017-05-12 05:24:39 +00:00
|
|
|
func (c Context) Bool(key string, b bool) Context {
|
2017-05-20 02:43:59 +00:00
|
|
|
c.l.context = appendBool(c.l.context, key, b)
|
|
|
|
return c
|
2017-05-12 05:24:39 +00:00
|
|
|
}
|
|
|
|
|
2017-05-17 04:52:22 +00:00
|
|
|
// Int adds the field key with i as a int to the logger context.
|
2017-05-12 05:24:39 +00:00
|
|
|
func (c Context) Int(key string, i int) Context {
|
2017-05-20 02:43:59 +00:00
|
|
|
c.l.context = appendInt(c.l.context, key, i)
|
|
|
|
return c
|
2017-05-12 05:24:39 +00:00
|
|
|
}
|
|
|
|
|
2017-05-17 04:52:22 +00:00
|
|
|
// Int8 adds the field key with i as a int8 to the logger context.
|
2017-05-12 05:24:39 +00:00
|
|
|
func (c Context) Int8(key string, i int8) Context {
|
2017-05-20 02:43:59 +00:00
|
|
|
c.l.context = appendInt8(c.l.context, key, i)
|
|
|
|
return c
|
2017-05-12 05:24:39 +00:00
|
|
|
}
|
|
|
|
|
2017-05-17 04:52:22 +00:00
|
|
|
// Int16 adds the field key with i as a int16 to the logger context.
|
2017-05-12 05:24:39 +00:00
|
|
|
func (c Context) Int16(key string, i int16) Context {
|
2017-05-20 02:43:59 +00:00
|
|
|
c.l.context = appendInt16(c.l.context, key, i)
|
|
|
|
return c
|
2017-05-12 05:24:39 +00:00
|
|
|
}
|
|
|
|
|
2017-05-17 04:52:22 +00:00
|
|
|
// Int32 adds the field key with i as a int32 to the logger context.
|
2017-05-12 05:24:39 +00:00
|
|
|
func (c Context) Int32(key string, i int32) Context {
|
2017-05-20 02:43:59 +00:00
|
|
|
c.l.context = appendInt32(c.l.context, key, i)
|
|
|
|
return c
|
2017-05-12 05:24:39 +00:00
|
|
|
}
|
|
|
|
|
2017-05-17 04:52:22 +00:00
|
|
|
// Int64 adds the field key with i as a int64 to the logger context.
|
2017-05-12 05:24:39 +00:00
|
|
|
func (c Context) Int64(key string, i int64) Context {
|
2017-05-20 02:43:59 +00:00
|
|
|
c.l.context = appendInt64(c.l.context, key, i)
|
|
|
|
return c
|
2017-05-12 05:24:39 +00:00
|
|
|
}
|
|
|
|
|
2017-05-17 04:52:22 +00:00
|
|
|
// Uint adds the field key with i as a uint to the logger context.
|
2017-05-12 05:24:39 +00:00
|
|
|
func (c Context) Uint(key string, i uint) Context {
|
2017-05-20 02:43:59 +00:00
|
|
|
c.l.context = appendUint(c.l.context, key, i)
|
|
|
|
return c
|
2017-05-12 05:24:39 +00:00
|
|
|
}
|
|
|
|
|
2017-05-17 04:52:22 +00:00
|
|
|
// Uint8 adds the field key with i as a uint8 to the logger context.
|
2017-05-12 05:24:39 +00:00
|
|
|
func (c Context) Uint8(key string, i uint8) Context {
|
2017-05-20 02:43:59 +00:00
|
|
|
c.l.context = appendUint8(c.l.context, key, i)
|
|
|
|
return c
|
2017-05-12 05:24:39 +00:00
|
|
|
}
|
|
|
|
|
2017-05-17 04:52:22 +00:00
|
|
|
// Uint16 adds the field key with i as a uint16 to the logger context.
|
2017-05-12 05:24:39 +00:00
|
|
|
func (c Context) Uint16(key string, i uint16) Context {
|
2017-05-20 02:43:59 +00:00
|
|
|
c.l.context = appendUint16(c.l.context, key, i)
|
|
|
|
return c
|
2017-05-12 05:24:39 +00:00
|
|
|
}
|
|
|
|
|
2017-05-17 04:52:22 +00:00
|
|
|
// Uint32 adds the field key with i as a uint32 to the logger context.
|
2017-05-12 05:24:39 +00:00
|
|
|
func (c Context) Uint32(key string, i uint32) Context {
|
2017-05-20 02:43:59 +00:00
|
|
|
c.l.context = appendUint32(c.l.context, key, i)
|
|
|
|
return c
|
2017-05-12 05:24:39 +00:00
|
|
|
}
|
|
|
|
|
2017-05-17 04:52:22 +00:00
|
|
|
// Uint64 adds the field key with i as a uint64 to the logger context.
|
2017-05-12 05:24:39 +00:00
|
|
|
func (c Context) Uint64(key string, i uint64) Context {
|
2017-05-20 02:43:59 +00:00
|
|
|
c.l.context = appendUint64(c.l.context, key, i)
|
|
|
|
return c
|
2017-05-12 05:24:39 +00:00
|
|
|
}
|
|
|
|
|
2017-05-17 04:52:22 +00:00
|
|
|
// Float32 adds the field key with f as a float32 to the logger context.
|
2017-05-12 05:24:39 +00:00
|
|
|
func (c Context) Float32(key string, f float32) Context {
|
2017-05-20 02:43:59 +00:00
|
|
|
c.l.context = appendFloat32(c.l.context, key, f)
|
|
|
|
return c
|
2017-05-12 05:24:39 +00:00
|
|
|
}
|
|
|
|
|
2017-05-17 04:52:22 +00:00
|
|
|
// Float64 adds the field key with f as a float64 to the logger context.
|
2017-05-12 05:24:39 +00:00
|
|
|
func (c Context) Float64(key string, f float64) Context {
|
2017-05-20 02:43:59 +00:00
|
|
|
c.l.context = appendFloat64(c.l.context, key, f)
|
|
|
|
return c
|
2017-05-12 05:24:39 +00:00
|
|
|
}
|
|
|
|
|
2017-05-17 04:52:22 +00:00
|
|
|
// 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.
|
2017-05-12 05:24:39 +00:00
|
|
|
func (c Context) Timestamp() Context {
|
2017-05-20 02:43:59 +00:00
|
|
|
if len(c.l.context) > 0 {
|
|
|
|
c.l.context[0] = 1
|
|
|
|
} else {
|
|
|
|
c.l.context = append(c.l.context, 1)
|
|
|
|
}
|
|
|
|
return c
|
2017-05-12 05:24:39 +00:00
|
|
|
}
|
|
|
|
|
2017-05-17 04:52:22 +00:00
|
|
|
// Time adds the field key with t formated as string using zerolog.TimeFieldFormat.
|
2017-05-12 05:24:39 +00:00
|
|
|
func (c Context) Time(key string, t time.Time) Context {
|
2017-05-20 02:43:59 +00:00
|
|
|
c.l.context = appendTime(c.l.context, key, t)
|
|
|
|
return c
|
2017-05-12 05:24:39 +00:00
|
|
|
}
|
2017-05-20 02:56:18 +00:00
|
|
|
|
2017-05-20 05:43:10 +00:00
|
|
|
// Dur adds the fields key with d divided by unit and stored as a float.
|
2017-05-21 04:08:42 +00:00
|
|
|
func (c Context) Dur(key string, d time.Duration) Context {
|
|
|
|
c.l.context = appendDuration(c.l.context, key, d)
|
2017-05-20 05:43:10 +00:00
|
|
|
return c
|
|
|
|
}
|
|
|
|
|
2017-05-20 05:25:37 +00:00
|
|
|
// Interface adds the field key with obj marshaled using reflection.
|
|
|
|
func (c Context) Interface(key string, i interface{}) Context {
|
|
|
|
c.l.context = appendInterface(c.l.context, key, i)
|
2017-05-20 02:56:18 +00:00
|
|
|
return c
|
|
|
|
}
|