Source File
value.go
Belonging Package
github.com/go-kit/log
package logimport ()// A Valuer generates a log value. When passed to With, WithPrefix, or// WithSuffix in a value element (odd indexes), it represents a dynamic// value which is re-evaluated with each log event.type Valuer func() interface{}// bindValues replaces all value elements (odd indexes) containing a Valuer// with their generated value.func bindValues( []interface{}) {for := 1; < len(); += 2 {if , := [].(Valuer); {[] = ()}}}// containsValuer returns true if any of the value elements (odd indexes)// contain a Valuer.func containsValuer( []interface{}) bool {for := 1; < len(); += 2 {if , := [].(Valuer); {return true}}return false}// Timestamp returns a timestamp Valuer. It invokes the t function to get the// time; unless you are doing something tricky, pass time.Now.//// Most users will want to use DefaultTimestamp or DefaultTimestampUTC, which// are TimestampFormats that use the RFC3339Nano format.func ( func() time.Time) Valuer {return func() interface{} { return () }}// TimestampFormat returns a timestamp Valuer with a custom time format. It// invokes the t function to get the time to format; unless you are doing// something tricky, pass time.Now. The layout string is passed to// Time.Format.//// Most users will want to use DefaultTimestamp or DefaultTimestampUTC, which// are TimestampFormats that use the RFC3339Nano format.func ( func() time.Time, string) Valuer {return func() interface{} {return timeFormat{time: (),layout: ,}}}// A timeFormat represents an instant in time and a layout used when// marshaling to a text format.type timeFormat struct {time time.Timelayout string}func ( timeFormat) () string {return .time.Format(.layout)}// MarshalText implements encoding.TextMarshaller.func ( timeFormat) () ( []byte, error) {// The following code adapted from the standard library time.Time.Format// method. Using the same undocumented magic constant to extend the size// of the buffer as seen there.:= make([]byte, 0, len(.layout)+10)= .time.AppendFormat(, .layout)return , nil}// Caller returns a Valuer that returns a file and line from a specified depth// in the callstack. Users will probably want to use DefaultCaller.func ( int) Valuer {return func() interface{} {, , , := runtime.Caller():= strings.LastIndexByte(, '/')// using idx+1 below handles both of following cases:// idx == -1 because no "/" was found, or// idx >= 0 and we want to start at the character after the found "/".return [+1:] + ":" + strconv.Itoa()}}var (// DefaultTimestamp is a Valuer that returns the current wallclock time,// respecting time zones, when bound.DefaultTimestamp = TimestampFormat(time.Now, time.RFC3339Nano)// DefaultTimestampUTC is a Valuer that returns the current time in UTC// when bound.DefaultTimestampUTC = TimestampFormat(func() time.Time { return time.Now().UTC() },time.RFC3339Nano,)// DefaultCaller is a Valuer that returns the file and line where the Log// method was invoked. It can only be used with log.With.DefaultCaller = Caller(3))
![]() |
The pages are generated with Golds v0.8.2. (GOOS=linux GOARCH=amd64) Golds is a Go 101 project developed by Tapir Liu. PR and bug reports are welcome and can be submitted to the issue list. Please follow @zigo_101 (reachable from the left QR code) to get the latest news of Golds. |