Skip to content

Instantly share code, notes, and snippets.

@tsenart
Created April 24, 2015 15:35
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/80fdfa09206d5d46f1ca to your computer and use it in GitHub Desktop.
Save tsenart/80fdfa09206d5d46f1ca to your computer and use it in GitHub Desktop.
diff --git a/log/value.go b/log/value.go
index f2c55b7..4351586 100644
--- a/log/value.go
+++ b/log/value.go
@@ -18,6 +18,12 @@ type Valuer interface {
Value() Value
}
+// ValuerFunc is an adapter func type that implements the Valuer interface.
+type ValuerFunc func() Value
+
+// Value implements the Value method of the Valuer interface.
+func (f ValuerFunc) Value() Value { return f() }
+
// BindValues returns a slice with all value elements (odd indexes) that
// implement Valuer replaced with the result of calling their Value method. If
// no value elements implement Valuer, the original slice is returned.
@@ -46,31 +52,27 @@ 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 ValuerFunc(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.Value().(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 ValuerFunc(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)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment