-
Notifications
You must be signed in to change notification settings - Fork 0
/
kklogger_gcp_logging.go
138 lines (115 loc) · 2.73 KB
/
kklogger_gcp_logging.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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
package kklogger_gcp_logging
import (
"context"
"encoding/json"
"fmt"
"sync"
"cloud.google.com/go/logging"
kklogger "github.com/kklab-com/goth-kklogger"
)
type KKLoggerGCPLoggingHook struct {
enabled bool
initOnce sync.Once
logger *logging.Logger
ProjectId string
LogName string
Environment string
CodeVersion string
Service string
ServerRoot string
Level kklogger.Level
}
func (h *KKLoggerGCPLoggingHook) LogString(args ...interface{}) string {
if args == nil {
return ""
}
args = args[0].([]interface{})
argl := len(args)
if argl == 1 {
switch tp := args[0].(type) {
case string:
return tp
}
} else if argl > 1 {
switch tp := args[0].(type) {
case string:
pargs := args[1:]
return fmt.Sprintf(tp, pargs...)
}
}
return fmt.Sprint(args...)
}
func (h *KKLoggerGCPLoggingHook) Trace(args ...interface{}) {
if h.Level < kklogger.TraceLevel {
return
}
h.Send(kklogger.TraceLevel, h.LogString(args...))
}
func (h *KKLoggerGCPLoggingHook) Debug(args ...interface{}) {
if h.Level < kklogger.DebugLevel {
return
}
h.Send(kklogger.DebugLevel, h.LogString(args...))
}
func (h *KKLoggerGCPLoggingHook) Info(args ...interface{}) {
if h.Level < kklogger.InfoLevel {
return
}
h.Send(kklogger.InfoLevel, h.LogString(args...))
}
func (h *KKLoggerGCPLoggingHook) Warn(args ...interface{}) {
if h.Level < kklogger.WarnLevel {
return
}
h.Send(kklogger.WarnLevel, h.LogString(args...))
}
func (h *KKLoggerGCPLoggingHook) Error(args ...interface{}) {
if h.Level < kklogger.ErrorLevel {
return
}
h.Send(kklogger.ErrorLevel, h.LogString(args...))
}
func (h *KKLoggerGCPLoggingHook) Send(level kklogger.Level, msg string) {
h.initOnce.Do(func() {
client, err := logging.NewClient(context.Background(), h.ProjectId)
if err != nil {
fmt.Println(err.Error())
return
}
h.logger = client.Logger(h.LogName)
h.enabled = true
})
if !h.enabled {
return
}
h.logger.Log(h.getEntry(level, msg))
}
func (h *KKLoggerGCPLoggingHook) getEntry(level kklogger.Level, msg string) logging.Entry {
obj := &map[string]interface{}{}
json.Unmarshal([]byte(msg), obj)
return logging.Entry{
Severity: func(level kklogger.Level) logging.Severity {
switch level {
case kklogger.TraceLevel:
return logging.Default
case kklogger.DebugLevel:
return logging.Debug
case kklogger.InfoLevel:
return logging.Info
case kklogger.WarnLevel:
return logging.Warning
case kklogger.ErrorLevel:
return logging.Error
}
return logging.Default
}(level),
Payload: obj,
Labels: map[string]string{
"logName": h.LogName,
"environment": h.Environment,
"codeVersion": h.CodeVersion,
"service": h.Service,
"serverRoot": h.ServerRoot,
},
}
}