-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathkv.go
More file actions
55 lines (45 loc) · 1016 Bytes
/
kv.go
File metadata and controls
55 lines (45 loc) · 1016 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
package mo
import (
"context"
"fmt"
"os"
"runtime"
"strconv"
"strings"
"time"
)
type Field [2]interface{}
func (v Field) Key() string {
if v, ok := v[0].(string); ok {
return v
}
return fmt.Sprint(v[0])
}
func (v Field) Value() interface{} {
return v[1]
}
func Value(key string, value interface{}) Field {
return Field{key, value}
}
type Valuer func(ctx context.Context) interface{}
var callerAbs, _ = strconv.ParseBool(os.Getenv("MO_CALLER_ABS"))
func Caller(skip int) Valuer {
return func(ctx context.Context) interface{} {
_, file, line, _ := runtime.Caller(skip)
if callerAbs {
return file + ":" + strconv.Itoa(line)
}
idx := strings.LastIndexByte(file, '/')
if idx == -1 {
return file + ":" + strconv.Itoa(line)
}
idx = strings.LastIndexByte(file[:idx], '/')
return file[idx+1:] + ":" + strconv.Itoa(line)
}
}
var DefaultCaller = Caller(3)
func Timestamp(layout string) Valuer {
return func(context.Context) interface{} {
return time.Now().Format(layout)
}
}