-
Notifications
You must be signed in to change notification settings - Fork 1
/
log_console.go
86 lines (75 loc) · 1.9 KB
/
log_console.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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
package log
import (
"context"
"fmt"
"github.com/Andrew-M-C/go.util/log/trace"
"github.com/Andrew-M-C/go.util/runtime/caller"
"github.com/fatih/color"
)
type consoleLog Level
func (l consoleLog) getLogger() func(string, ...any) string {
var fu func(string, ...any) string
switch Level(l) {
default:
fu = fmt.Sprintf
case FatalLevel:
fu = color.HiRedString
case ErrorLevel:
fu = color.RedString
case WarnLevel:
fu = color.YellowString
case DebugLevel:
fu = color.BlueString
case TraceLevel:
fu = color.CyanString
}
return fu
}
func (l consoleLog) logf(f string, a ...any) {
fu := l.getLogger()
ca := caller.GetCaller(internalGetCallerSkip())
f = fmt.Sprintf("%s - %s - %s - %s", timeDesc(), Level(l).String(), callerDesc(ca), f)
s := fu(f, a...)
fmt.Println(s)
}
func (l consoleLog) log(a ...any) {
fu := l.getLogger()
ca := caller.GetCaller(internalGetCallerSkip())
f := fmt.Sprintf("%s - %s - %s", timeDesc(), Level(l).String(), callerDesc(ca))
s := fmt.Sprint(a...)
s = fu("%s - %s", f, s)
fmt.Println(s)
}
func (l consoleLog) logCtxf(ctx context.Context, f string, a ...any) {
fu := l.getLogger()
id := trace.GetTraceID(ctx)
ca := caller.GetCaller(internalGetCallerSkip())
if id == "" {
f = fmt.Sprintf(
"%s - %s - %s - %s",
timeDesc(), Level(l).String(), callerDesc(ca), f,
)
} else {
f = fmt.Sprintf(
"%s - %s - %s - %s {\"trace_id\":\"%s\")",
timeDesc(), Level(l).String(), callerDesc(ca), f, id,
)
}
s := fu(f, a...)
fmt.Println(s)
}
func (l consoleLog) logCtx(ctx context.Context, a ...any) {
fu := l.getLogger()
id := trace.GetTraceID(ctx)
ca := caller.GetCaller(internalGetCallerSkip())
f := fmt.Sprintf("%s - %s - %s", timeDesc(), Level(l).String(), callerDesc(ca))
s := fmt.Sprint(a...)
s = fmt.Sprintf("%s - %s", f, s)
if id == "" {
s = fu("%s", s)
fmt.Println(s)
} else {
s = fu("%s {\"trace_id\":\"%s\"}", s, id)
fmt.Println(s)
}
}