Skip to content

Instantly share code, notes, and snippets.

@tsenart
Created April 24, 2015 15:40
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tsenart/377563923d300c5a5f64 to your computer and use it in GitHub Desktop.
Save tsenart/377563923d300c5a5f64 to your computer and use it in GitHub Desktop.
diff --git a/log/value.go b/log/value.go
index f2c55b7..ba622da 100644
--- a/log/value.go
+++ b/log/value.go
@@ -11,12 +11,10 @@ type Value interface{}
// A Valuer generates a log value. When passed to With, it represents a
// dynamic value which is re-evaluated with each log event.
-type Valuer interface {
- // Value returns a log Value instead of an interface{} to avoid
- // inadvertently matching types from packages not intended for use with
- // gokit/log.
- Value() Value
-}
+// It returns a log Value instead of an interface{} to avoid
+// inadvertently matching types from packages not intended for use with
+// gokit/log.
+type Valuer func() Value
// BindValues returns a slice with all value elements (odd indexes) that
// implement Valuer replaced with the result of calling their Value method. If
@@ -30,7 +28,7 @@ func BindValues(keyvals ...interface{}) []interface{} {
copy(bound, keyvals)
for i := 1; i < len(bound); i += 2 {
if v, ok := bound[i].(Valuer); ok {
- bound[i] = v.Value()
+ bound[i] = v()
}
}
@@ -46,32 +44,28 @@ func containsValuer(keyvals []interface{}) bool {
return false
}
-// Timestamp is a Valuer that invokes the underlying function when bound,
-// returning a time.Time. Users will probably want to use DefaultTimestamp or
-// DefaultTimestampUTC.
-type Timestamp func() time.Time
-
-// Value implements Valuer.
-func (t Timestamp) Value() Value { return t() }
-
-// Caller is a Valuer that returns a file and line from a specified depth in
-// the callstack. Users will probably want to use DefaultCaller.
-type Caller int
-
-// Value implements Valuer.
-func (c Caller) Value() Value { return stack.Caller(int(c)) }
-
var (
+ // Timestamp returns a Valuer that invokes the underlying function when bound,
+ // returning a time.Time. Users will probably want to use DefaultTimestamp or
+ // DefaultTimestampUTC.
+ Timestamp = func(t func() time.Time) Valuer {
+ return func() Value { return t() }
+ }
// DefaultTimestamp is a Timestamp Valuer that returns the current wallclock
// time, respecting time zones, when bound.
- DefaultTimestamp Timestamp = time.Now
-
+ DefaultTimestamp = Timestamp(time.Now)
// DefaultTimestampUTC wraps DefaultTimestamp but ensures the returned
// time is always in UTC. Note that it invokes DefaultTimestamp, and so
// reflects any changes to the DefaultTimestamp package global.
- DefaultTimestampUTC Timestamp = func() time.Time { return DefaultTimestamp().UTC() }
-
+ DefaultTimestampUTC = Timestamp(func() time.Time {
+ return DefaultTimestamp().(time.Time).UTC()
+ })
+ // Caller is a Valuer that returns a file and line from a specified depth in
+ // the callstack. Users will probably want to use DefaultCaller.
+ Caller = func(depth int) Valuer {
+ return func() Value { return stack.Caller(depth) }
+ }
// DefaultCaller is a Valuer that returns the file and line where the Log
// method was invoked.
- DefaultCaller = Caller(4)
+ DefaultCaller = Caller(3)
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment