-
Notifications
You must be signed in to change notification settings - Fork 0
/
log.go
52 lines (43 loc) · 1.41 KB
/
log.go
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
package log
import (
"context"
"unicode/utf8"
)
const (
AppName = "app_name"
AppVersion = "app_version"
)
// Logger is an interface that wraps the most common logging methods.
// Correlation related information like the Correlation-Id and Workflow-Id
// will be extracted from the passed Context.
type Logger interface {
Debug(ctx context.Context, msg string, keysAndValues ...interface{})
Info(ctx context.Context, msg string, keysAndValues ...interface{})
Warn(ctx context.Context, msg string, keysAndValues ...interface{})
Error(ctx context.Context, msg string, keysAndValues ...interface{})
Panic(ctx context.Context, msg string, keysAndValues ...interface{})
IsDebug(ctx context.Context) bool
// Dump should be used only during development and should not stay in production code.
Dump(msg string, v ...interface{})
}
// ContextMapper is an interface that returns Values which should be included in the log fields.
type ContextMapper interface {
Values(ctx context.Context) map[string]string
}
func Truncate(str string, length int, concat string) string {
if len(str) <= length {
return str
}
finalLength := length - len(concat)
headLength := finalLength/2 + finalLength%2
tailLength := finalLength / 2
for !utf8.RuneStart(str[headLength]) {
headLength--
}
for !utf8.RuneStart(str[tailLength]) {
tailLength++
}
head := str[0:headLength]
tail := str[len(str)-tailLength:]
return head + concat + tail
}