-
Notifications
You must be signed in to change notification settings - Fork 0
/
log.go
163 lines (138 loc) · 4.54 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
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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
package resource
import (
"context"
"encoding/json"
"fmt"
"go.opentelemetry.io/otel/trace"
"net/http"
"os"
"time"
)
type RLogger interface {
InfoF(ctx context.Context, format string, a ...interface{})
Info(ctx context.Context, message string)
WarningF(ctx context.Context, format string, a ...interface{})
Warning(ctx context.Context, message string)
Error(ctx context.Context, r *http.Request, message string) string
ErrorErr(ctx context.Context, r *http.Request, message string, err error) string
ErrorF(ctx context.Context, r *http.Request, format string, a ...interface{}) string
Flush()
}
type rPayLoad struct {
EventTime string `json:"eventTime,omitempty"`
ServiceContext rServiceContext `json:"serviceContext,omitempty"`
Message string `json:"message,omitempty"`
Context rContext `json:"context,omitempty"`
}
type rServiceContext struct {
Service string `json:"service,omitempty"`
Version string `json:"version,omitempty"`
}
type rContext struct {
HttpRequest rHttpRequest `json:"httpRequest,omitempty"`
User string `json:"user,omitempty"`
ReportLocation rReportLocation `json:"reportLocation,omitempty"`
}
type rHttpRequest struct {
Method string `json:"method,omitempty"`
Url string `json:"url,omitempty"`
UserAgent string `json:"userAgent,omitempty"`
Referrer string `json:"referrer,omitempty"`
ResponseStatusCode int `json:"responseStatusCode,omitempty"`
RemoteIp string `json:"remoteIp,omitempty"`
}
type rReportLocation struct {
FilePath string `json:"filePath,omitempty"`
LineNumber int `json:"lineNumber,omitempty"`
FunctionName string `json:"functionName,omitempty"`
}
// https://cloud.google.com/logging/docs/agent/configuration#special-fields
type SLabel struct {
LoggerName string `json:"loggerName,omitempty"`
}
type STimestamp struct {
Seconds int64 `json:"seconds"`
Nanos int `json:"nanos"`
}
type SServiceContext struct {
Service string `json:"service"`
Version string `json:"version"`
}
type SEntry struct {
Severity string `json:"severity,omitempty"`
Message string `json:"message,omitempty"`
Timestamp *STimestamp `json:"timestamp,omitempty"`
Labels *SLabel `json:"logging.googleapis.com/labels,omitempty"`
ServiceContext *SServiceContext `json:"serviceContext,omitempty"`
SpanId string `json:"logging.googleapis.com/spanId,omitempty"`
Trace string `json:"logging.googleapis.com/trace,omitempty"`
TraceSampled bool `json:"logging.googleapis.com/trace_sampled,omitempty"`
}
type SLogger struct {
projectId string
}
func NewSLogger(projectId string) *SLogger {
return &SLogger{
projectId: projectId,
}
}
func (l *SLogger) InfoF(ctx context.Context, format string, a ...interface{}) {
l.log(ctx, "INFO", fmt.Sprintf(format, a...))
}
func (l *SLogger) Info(ctx context.Context, message string) {
l.log(ctx, "INFO", message)
}
func (l *SLogger) WarningF(ctx context.Context, format string, a ...interface{}) {
l.log(ctx, "WARNING", fmt.Sprintf(format, a...))
}
func (l *SLogger) Warning(ctx context.Context, message string) {
l.log(ctx, "WARNING", message)
}
func (l *SLogger) Error(ctx context.Context, r *http.Request, message string) string {
l.log(ctx, "ERROR", message)
return ""
}
func (l *SLogger) ErrorErr(ctx context.Context, r *http.Request, message string, err error) string {
l.log(ctx, "ERROR", fmt.Sprintf("%s: %s", message, err.Error()))
return ""
}
func (l *SLogger) ErrorF(ctx context.Context, r *http.Request, format string, a ...interface{}) string {
l.log(ctx, "ERROR", fmt.Sprintf(format, a...))
return ""
}
func (l *SLogger) Flush() {
panic("implement me")
}
func (l *SLogger) log(ctx context.Context, severity string, message string) {
now := time.Now()
entry := &SEntry{
Message: message,
Severity: severity,
Timestamp: &STimestamp{
Seconds: now.Unix(),
Nanos: now.Nanosecond(),
},
Labels: &SLabel{
LoggerName: "treactor",
},
ServiceContext: &SServiceContext{
Service: AppName,
Version: AppVersion,
},
}
entry = l.addSpan(ctx, entry)
b, err := json.Marshal(entry)
if err != nil {
fmt.Println("error:", err)
}
os.Stdout.Write(b)
os.Stdout.WriteString("\n")
}
func (l *SLogger) addSpan(ctx context.Context, entry *SEntry) *SEntry {
span := trace.SpanFromContext(ctx)
if span != nil {
entry.Trace = fmt.Sprintf("projects/%s/traces/%s", l.projectId, span.SpanContext().TraceID.String())
entry.SpanId = span.SpanContext().SpanID.String()
}
return entry
}