2017-05-12 05:24:39 +00:00
|
|
|
package zerolog
|
|
|
|
|
2017-07-26 01:41:05 +00:00
|
|
|
import (
|
|
|
|
"io/ioutil"
|
2018-04-03 21:07:18 +00:00
|
|
|
"net"
|
2017-07-26 01:41:05 +00:00
|
|
|
"time"
|
|
|
|
)
|
2017-05-12 05:24:39 +00:00
|
|
|
|
|
|
|
// 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-07-10 09:56:44 +00:00
|
|
|
// Fields is a helper function to use a map to set fields using type assertion.
|
|
|
|
func (c Context) Fields(fields map[string]interface{}) Context {
|
|
|
|
c.l.context = appendFields(c.l.context, fields)
|
|
|
|
return c
|
|
|
|
}
|
|
|
|
|
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 {
|
2018-05-10 22:01:41 +00:00
|
|
|
dict.buf = enc.AppendEndMarker(dict.buf)
|
|
|
|
c.l.context = append(enc.AppendKey(c.l.context, key), dict.buf...)
|
2017-05-20 02:43:59 +00:00
|
|
|
eventPool.Put(dict)
|
|
|
|
return c
|
2017-05-18 07:10:45 +00:00
|
|
|
}
|
|
|
|
|
2017-07-26 07:14:43 +00:00
|
|
|
// 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.
|
|
|
|
func (c Context) Array(key string, arr LogArrayMarshaler) Context {
|
2018-05-10 22:01:41 +00:00
|
|
|
c.l.context = enc.AppendKey(c.l.context, key)
|
2017-07-26 07:14:43 +00:00
|
|
|
if arr, ok := arr.(*Array); ok {
|
|
|
|
c.l.context = arr.write(c.l.context)
|
|
|
|
return c
|
|
|
|
}
|
|
|
|
var a *Array
|
|
|
|
if aa, ok := arr.(*Array); ok {
|
|
|
|
a = aa
|
|
|
|
} else {
|
|
|
|
a = Arr()
|
|
|
|
arr.MarshalZerologArray(a)
|
|
|
|
}
|
|
|
|
c.l.context = a.write(c.l.context)
|
|
|
|
return c
|
|
|
|
}
|
|
|
|
|
2017-07-26 01:41:05 +00:00
|
|
|
// Object marshals an object that implement the LogObjectMarshaler interface.
|
|
|
|
func (c Context) Object(key string, obj LogObjectMarshaler) Context {
|
2018-05-09 10:51:52 +00:00
|
|
|
e := newEvent(levelWriterAdapter{ioutil.Discard}, 0)
|
2017-07-26 01:41:05 +00:00
|
|
|
e.Object(key, obj)
|
2018-05-10 22:01:41 +00:00
|
|
|
c.l.context = enc.AppendObjectData(c.l.context, e.buf)
|
2017-07-26 01:41:05 +00:00
|
|
|
eventPool.Put(e)
|
|
|
|
return c
|
|
|
|
}
|
|
|
|
|
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 {
|
2018-05-10 22:01:41 +00:00
|
|
|
c.l.context = enc.AppendString(enc.AppendKey(c.l.context, key), val)
|
2017-05-20 02:43:59 +00:00
|
|
|
return c
|
2017-05-12 05:24:39 +00:00
|
|
|
}
|
|
|
|
|
2017-07-25 19:50:35 +00:00
|
|
|
// Strs adds the field key with val as a string to the logger context.
|
|
|
|
func (c Context) Strs(key string, vals []string) Context {
|
2018-05-10 22:01:41 +00:00
|
|
|
c.l.context = enc.AppendStrings(enc.AppendKey(c.l.context, key), vals)
|
2017-07-25 19:50:35 +00:00
|
|
|
return c
|
|
|
|
}
|
|
|
|
|
2017-07-01 19:47:19 +00:00
|
|
|
// Bytes adds the field key with val as a []byte to the logger context.
|
|
|
|
func (c Context) Bytes(key string, val []byte) Context {
|
2018-05-10 22:01:41 +00:00
|
|
|
c.l.context = enc.AppendBytes(enc.AppendKey(c.l.context, key), val)
|
2017-07-01 19:47:19 +00:00
|
|
|
return c
|
|
|
|
}
|
|
|
|
|
2018-03-15 17:29:26 +00:00
|
|
|
// Hex adds the field key with val as a hex string to the logger context.
|
|
|
|
func (c Context) Hex(key string, val []byte) Context {
|
2018-05-10 22:01:41 +00:00
|
|
|
c.l.context = enc.AppendHex(enc.AppendKey(c.l.context, key), val)
|
2018-03-15 17:29:26 +00:00
|
|
|
return c
|
|
|
|
}
|
|
|
|
|
2018-02-13 00:05:27 +00:00
|
|
|
// RawJSON adds already encoded JSON to context.
|
|
|
|
//
|
|
|
|
// No sanity check is performed on b; it must not contain carriage returns and
|
|
|
|
// be valid JSON.
|
|
|
|
func (c Context) RawJSON(key string, b []byte) Context {
|
2018-05-10 22:01:41 +00:00
|
|
|
c.l.context = appendJSON(enc.AppendKey(c.l.context, key), b)
|
2018-02-13 00:05:27 +00:00
|
|
|
return c
|
|
|
|
}
|
|
|
|
|
2017-06-02 07:56:14 +00:00
|
|
|
// AnErr adds the field key with err as a string to the logger context.
|
|
|
|
func (c Context) AnErr(key string, err error) Context {
|
2017-07-26 03:51:49 +00:00
|
|
|
if err != nil {
|
2018-05-10 22:01:41 +00:00
|
|
|
c.l.context = enc.AppendError(enc.AppendKey(c.l.context, key), err)
|
2017-07-26 03:51:49 +00:00
|
|
|
}
|
2017-06-02 07:56:14 +00:00
|
|
|
return c
|
|
|
|
}
|
|
|
|
|
2017-07-25 19:50:35 +00:00
|
|
|
// Errs adds the field key with errs as an array of strings to the logger context.
|
|
|
|
func (c Context) Errs(key string, errs []error) Context {
|
2018-05-10 22:01:41 +00:00
|
|
|
c.l.context = enc.AppendErrors(enc.AppendKey(c.l.context, key), errs)
|
2017-07-25 19:50:35 +00:00
|
|
|
return c
|
|
|
|
}
|
|
|
|
|
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-07-26 03:51:49 +00:00
|
|
|
if err != nil {
|
2018-05-10 22:01:41 +00:00
|
|
|
c.l.context = enc.AppendError(enc.AppendKey(c.l.context, ErrorFieldName), err)
|
2017-07-26 03:51:49 +00:00
|
|
|
}
|
2017-05-20 02:43:59 +00:00
|
|
|
return c
|
2017-05-12 05:24:39 +00:00
|
|
|
}
|
|
|
|
|
2017-07-25 19:50:35 +00:00
|
|
|
// Bool adds the field key with val as a bool to the logger context.
|
2017-05-12 05:24:39 +00:00
|
|
|
func (c Context) Bool(key string, b bool) Context {
|
2018-05-10 22:01:41 +00:00
|
|
|
c.l.context = enc.AppendBool(enc.AppendKey(c.l.context, key), b)
|
2017-05-20 02:43:59 +00:00
|
|
|
return c
|
2017-05-12 05:24:39 +00:00
|
|
|
}
|
|
|
|
|
2017-07-25 19:50:35 +00:00
|
|
|
// Bools adds the field key with val as a []bool to the logger context.
|
|
|
|
func (c Context) Bools(key string, b []bool) Context {
|
2018-05-10 22:01:41 +00:00
|
|
|
c.l.context = enc.AppendBools(enc.AppendKey(c.l.context, key), b)
|
2017-07-25 19:50:35 +00:00
|
|
|
return c
|
|
|
|
}
|
|
|
|
|
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 {
|
2018-05-10 22:01:41 +00:00
|
|
|
c.l.context = enc.AppendInt(enc.AppendKey(c.l.context, key), i)
|
2017-05-20 02:43:59 +00:00
|
|
|
return c
|
2017-05-12 05:24:39 +00:00
|
|
|
}
|
|
|
|
|
2017-07-25 19:50:35 +00:00
|
|
|
// Ints adds the field key with i as a []int to the logger context.
|
|
|
|
func (c Context) Ints(key string, i []int) Context {
|
2018-05-10 22:01:41 +00:00
|
|
|
c.l.context = enc.AppendInts(enc.AppendKey(c.l.context, key), i)
|
2017-07-25 19:50:35 +00:00
|
|
|
return c
|
|
|
|
}
|
|
|
|
|
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 {
|
2018-05-10 22:01:41 +00:00
|
|
|
c.l.context = enc.AppendInt8(enc.AppendKey(c.l.context, key), i)
|
2017-05-20 02:43:59 +00:00
|
|
|
return c
|
2017-05-12 05:24:39 +00:00
|
|
|
}
|
|
|
|
|
2017-07-25 19:50:35 +00:00
|
|
|
// Ints8 adds the field key with i as a []int8 to the logger context.
|
|
|
|
func (c Context) Ints8(key string, i []int8) Context {
|
2018-05-10 22:01:41 +00:00
|
|
|
c.l.context = enc.AppendInts8(enc.AppendKey(c.l.context, key), i)
|
2017-07-25 19:50:35 +00:00
|
|
|
return c
|
|
|
|
}
|
|
|
|
|
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 {
|
2018-05-10 22:01:41 +00:00
|
|
|
c.l.context = enc.AppendInt16(enc.AppendKey(c.l.context, key), i)
|
2017-05-20 02:43:59 +00:00
|
|
|
return c
|
2017-05-12 05:24:39 +00:00
|
|
|
}
|
|
|
|
|
2017-07-25 19:50:35 +00:00
|
|
|
// Ints16 adds the field key with i as a []int16 to the logger context.
|
|
|
|
func (c Context) Ints16(key string, i []int16) Context {
|
2018-05-10 22:01:41 +00:00
|
|
|
c.l.context = enc.AppendInts16(enc.AppendKey(c.l.context, key), i)
|
2017-07-25 19:50:35 +00:00
|
|
|
return c
|
|
|
|
}
|
|
|
|
|
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 {
|
2018-05-10 22:01:41 +00:00
|
|
|
c.l.context = enc.AppendInt32(enc.AppendKey(c.l.context, key), i)
|
2017-05-20 02:43:59 +00:00
|
|
|
return c
|
2017-05-12 05:24:39 +00:00
|
|
|
}
|
|
|
|
|
2017-07-25 19:50:35 +00:00
|
|
|
// Ints32 adds the field key with i as a []int32 to the logger context.
|
|
|
|
func (c Context) Ints32(key string, i []int32) Context {
|
2018-05-10 22:01:41 +00:00
|
|
|
c.l.context = enc.AppendInts32(enc.AppendKey(c.l.context, key), i)
|
2017-07-25 19:50:35 +00:00
|
|
|
return c
|
|
|
|
}
|
|
|
|
|
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 {
|
2018-05-10 22:01:41 +00:00
|
|
|
c.l.context = enc.AppendInt64(enc.AppendKey(c.l.context, key), i)
|
2017-05-20 02:43:59 +00:00
|
|
|
return c
|
2017-05-12 05:24:39 +00:00
|
|
|
}
|
|
|
|
|
2017-07-25 19:50:35 +00:00
|
|
|
// Ints64 adds the field key with i as a []int64 to the logger context.
|
|
|
|
func (c Context) Ints64(key string, i []int64) Context {
|
2018-05-10 22:01:41 +00:00
|
|
|
c.l.context = enc.AppendInts64(enc.AppendKey(c.l.context, key), i)
|
2017-07-25 19:50:35 +00:00
|
|
|
return c
|
|
|
|
}
|
|
|
|
|
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 {
|
2018-05-10 22:01:41 +00:00
|
|
|
c.l.context = enc.AppendUint(enc.AppendKey(c.l.context, key), i)
|
2017-05-20 02:43:59 +00:00
|
|
|
return c
|
2017-05-12 05:24:39 +00:00
|
|
|
}
|
|
|
|
|
2017-07-25 19:50:35 +00:00
|
|
|
// Uints adds the field key with i as a []uint to the logger context.
|
|
|
|
func (c Context) Uints(key string, i []uint) Context {
|
2018-05-10 22:01:41 +00:00
|
|
|
c.l.context = enc.AppendUints(enc.AppendKey(c.l.context, key), i)
|
2017-07-25 19:50:35 +00:00
|
|
|
return c
|
|
|
|
}
|
|
|
|
|
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 {
|
2018-05-10 22:01:41 +00:00
|
|
|
c.l.context = enc.AppendUint8(enc.AppendKey(c.l.context, key), i)
|
2017-05-20 02:43:59 +00:00
|
|
|
return c
|
2017-05-12 05:24:39 +00:00
|
|
|
}
|
|
|
|
|
2017-07-25 19:50:35 +00:00
|
|
|
// Uints8 adds the field key with i as a []uint8 to the logger context.
|
|
|
|
func (c Context) Uints8(key string, i []uint8) Context {
|
2018-05-10 22:01:41 +00:00
|
|
|
c.l.context = enc.AppendUints8(enc.AppendKey(c.l.context, key), i)
|
2017-07-25 19:50:35 +00:00
|
|
|
return c
|
|
|
|
}
|
|
|
|
|
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 {
|
2018-05-10 22:01:41 +00:00
|
|
|
c.l.context = enc.AppendUint16(enc.AppendKey(c.l.context, key), i)
|
2017-05-20 02:43:59 +00:00
|
|
|
return c
|
2017-05-12 05:24:39 +00:00
|
|
|
}
|
|
|
|
|
2017-07-25 19:50:35 +00:00
|
|
|
// Uints16 adds the field key with i as a []uint16 to the logger context.
|
|
|
|
func (c Context) Uints16(key string, i []uint16) Context {
|
2018-05-10 22:01:41 +00:00
|
|
|
c.l.context = enc.AppendUints16(enc.AppendKey(c.l.context, key), i)
|
2017-07-25 19:50:35 +00:00
|
|
|
return c
|
|
|
|
}
|
|
|
|
|
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 {
|
2018-05-10 22:01:41 +00:00
|
|
|
c.l.context = enc.AppendUint32(enc.AppendKey(c.l.context, key), i)
|
2017-05-20 02:43:59 +00:00
|
|
|
return c
|
2017-05-12 05:24:39 +00:00
|
|
|
}
|
|
|
|
|
2017-07-25 19:50:35 +00:00
|
|
|
// Uints32 adds the field key with i as a []uint32 to the logger context.
|
|
|
|
func (c Context) Uints32(key string, i []uint32) Context {
|
2018-05-10 22:01:41 +00:00
|
|
|
c.l.context = enc.AppendUints32(enc.AppendKey(c.l.context, key), i)
|
2017-07-25 19:50:35 +00:00
|
|
|
return c
|
|
|
|
}
|
|
|
|
|
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 {
|
2018-05-10 22:01:41 +00:00
|
|
|
c.l.context = enc.AppendUint64(enc.AppendKey(c.l.context, key), i)
|
2017-05-20 02:43:59 +00:00
|
|
|
return c
|
2017-05-12 05:24:39 +00:00
|
|
|
}
|
|
|
|
|
2017-07-25 19:50:35 +00:00
|
|
|
// Uints64 adds the field key with i as a []uint64 to the logger context.
|
|
|
|
func (c Context) Uints64(key string, i []uint64) Context {
|
2018-05-10 22:01:41 +00:00
|
|
|
c.l.context = enc.AppendUints64(enc.AppendKey(c.l.context, key), i)
|
2017-07-25 19:50:35 +00:00
|
|
|
return c
|
|
|
|
}
|
|
|
|
|
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 {
|
2018-05-10 22:01:41 +00:00
|
|
|
c.l.context = enc.AppendFloat32(enc.AppendKey(c.l.context, key), f)
|
2017-05-20 02:43:59 +00:00
|
|
|
return c
|
2017-05-12 05:24:39 +00:00
|
|
|
}
|
|
|
|
|
2017-07-25 19:50:35 +00:00
|
|
|
// Floats32 adds the field key with f as a []float32 to the logger context.
|
|
|
|
func (c Context) Floats32(key string, f []float32) Context {
|
2018-05-10 22:01:41 +00:00
|
|
|
c.l.context = enc.AppendFloats32(enc.AppendKey(c.l.context, key), f)
|
2017-07-25 19:50:35 +00:00
|
|
|
return c
|
|
|
|
}
|
|
|
|
|
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 {
|
2018-05-10 22:01:41 +00:00
|
|
|
c.l.context = enc.AppendFloat64(enc.AppendKey(c.l.context, key), f)
|
2017-05-20 02:43:59 +00:00
|
|
|
return c
|
2017-05-12 05:24:39 +00:00
|
|
|
}
|
|
|
|
|
2017-07-25 19:50:35 +00:00
|
|
|
// Floats64 adds the field key with f as a []float64 to the logger context.
|
|
|
|
func (c Context) Floats64(key string, f []float64) Context {
|
2018-05-10 22:01:41 +00:00
|
|
|
c.l.context = enc.AppendFloats64(enc.AppendKey(c.l.context, key), f)
|
2017-07-25 19:50:35 +00:00
|
|
|
return c
|
|
|
|
}
|
|
|
|
|
2018-02-07 21:24:48 +00:00
|
|
|
type timestampHook struct{}
|
|
|
|
|
|
|
|
func (ts timestampHook) Run(e *Event, level Level, msg string) {
|
|
|
|
e.Timestamp()
|
|
|
|
}
|
|
|
|
|
|
|
|
var th = timestampHook{}
|
|
|
|
|
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.
|
2018-03-08 15:41:28 +00:00
|
|
|
//
|
|
|
|
// NOTE: It won't dedupe the "time" key if the *Context has one already.
|
2017-05-12 05:24:39 +00:00
|
|
|
func (c Context) Timestamp() Context {
|
2018-02-07 21:24:48 +00:00
|
|
|
c.l = c.l.Hook(th)
|
2017-05-20 02:43:59 +00:00
|
|
|
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 {
|
2018-05-10 22:01:41 +00:00
|
|
|
c.l.context = enc.AppendTime(enc.AppendKey(c.l.context, key), t, TimeFieldFormat)
|
2017-05-20 02:43:59 +00:00
|
|
|
return c
|
2017-05-12 05:24:39 +00:00
|
|
|
}
|
2017-05-20 02:56:18 +00:00
|
|
|
|
2017-07-25 19:50:35 +00:00
|
|
|
// Times adds the field key with t formated as string using zerolog.TimeFieldFormat.
|
|
|
|
func (c Context) Times(key string, t []time.Time) Context {
|
2018-05-10 22:01:41 +00:00
|
|
|
c.l.context = enc.AppendTimes(enc.AppendKey(c.l.context, key), t, TimeFieldFormat)
|
2017-07-25 19:50:35 +00:00
|
|
|
return c
|
|
|
|
}
|
|
|
|
|
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 {
|
2018-05-10 22:01:41 +00:00
|
|
|
c.l.context = enc.AppendDuration(enc.AppendKey(c.l.context, key), d, DurationFieldUnit, DurationFieldInteger)
|
2017-05-20 05:43:10 +00:00
|
|
|
return c
|
|
|
|
}
|
|
|
|
|
2017-07-25 19:50:35 +00:00
|
|
|
// Durs adds the fields key with d divided by unit and stored as a float.
|
|
|
|
func (c Context) Durs(key string, d []time.Duration) Context {
|
2018-05-10 22:01:41 +00:00
|
|
|
c.l.context = enc.AppendDurations(enc.AppendKey(c.l.context, key), d, DurationFieldUnit, DurationFieldInteger)
|
2017-07-25 19:50:35 +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 {
|
2018-05-10 22:01:41 +00:00
|
|
|
c.l.context = enc.AppendInterface(enc.AppendKey(c.l.context, key), i)
|
2017-05-20 02:56:18 +00:00
|
|
|
return c
|
|
|
|
}
|
2018-02-07 21:54:26 +00:00
|
|
|
|
|
|
|
type callerHook struct{}
|
|
|
|
|
|
|
|
func (ch callerHook) Run(e *Event, level Level, msg string) {
|
|
|
|
e.caller(4)
|
|
|
|
}
|
|
|
|
|
|
|
|
var ch = callerHook{}
|
|
|
|
|
|
|
|
// Caller adds the file:line of the caller with the zerolog.CallerFieldName key.
|
|
|
|
func (c Context) Caller() Context {
|
|
|
|
c.l = c.l.Hook(ch)
|
|
|
|
return c
|
|
|
|
}
|
2018-04-03 21:07:18 +00:00
|
|
|
|
|
|
|
// IPAddr adds IPv4 or IPv6 Address to the context
|
|
|
|
func (c Context) IPAddr(key string, ip net.IP) Context {
|
2018-05-10 22:01:41 +00:00
|
|
|
c.l.context = enc.AppendIPAddr(enc.AppendKey(c.l.context, key), ip)
|
2018-04-03 21:07:18 +00:00
|
|
|
return c
|
|
|
|
}
|
|
|
|
|
|
|
|
// IPPrefix adds IPv4 or IPv6 Prefix (address and mask) to the context
|
|
|
|
func (c Context) IPPrefix(key string, pfx net.IPNet) Context {
|
2018-05-10 22:01:41 +00:00
|
|
|
c.l.context = enc.AppendIPPrefix(enc.AppendKey(c.l.context, key), pfx)
|
2018-04-03 21:07:18 +00:00
|
|
|
return c
|
|
|
|
}
|
|
|
|
|
|
|
|
// MACAddr adds MAC address to the context
|
|
|
|
func (c Context) MACAddr(key string, ha net.HardwareAddr) Context {
|
2018-05-10 22:01:41 +00:00
|
|
|
c.l.context = enc.AppendMACAddr(enc.AppendKey(c.l.context, key), ha)
|
2018-04-03 21:07:18 +00:00
|
|
|
return c
|
|
|
|
}
|