forked from tommy351/zap-stackdriver
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcontext.go
116 lines (96 loc) · 2.64 KB
/
context.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
package stackdriver
import (
"go.uber.org/zap/zapcore"
)
// The schema is based on: https://cloud.google.com/error-reporting/docs/formatting-error-messages
type ServiceContext struct {
Service string `json:"service"`
Version string `json:"version"`
}
func (s *ServiceContext) Clone() *ServiceContext {
return &ServiceContext{
Service: s.Service,
Version: s.Version,
}
}
func (s *ServiceContext) MarshalLogObject(e zapcore.ObjectEncoder) error {
e.AddString("service", s.Service)
e.AddString("version", s.Version)
return nil
}
type Context struct {
User string `json:"user"`
HTTPRequest *HTTPRequest `json:"httpRequest"`
ReportLocation *ReportLocation `json:"reportLocation"`
}
func (c *Context) Clone() *Context {
output := &Context{
User: c.User,
}
if c.HTTPRequest != nil {
output.HTTPRequest = c.HTTPRequest.Clone()
}
if c.ReportLocation != nil {
output.ReportLocation = c.ReportLocation.Clone()
}
return output
}
func (c *Context) MarshalLogObject(e zapcore.ObjectEncoder) (err error) {
e.AddString("user", c.User)
if c.HTTPRequest != nil {
if err = e.AddObject("httpRequest", c.HTTPRequest); err != nil {
return
}
}
if c.ReportLocation != nil {
if err = e.AddObject("reportLocation", c.ReportLocation); err != nil {
return
}
}
return
}
type HTTPRequest struct {
Method string `json:"method"`
URL string `json:"url"`
UserAgent string `json:"userAgent"`
Referrer string `json:"referrer"`
ResponseStatusCode int `json:"responseStatusCode"`
RemoteIP string `json:"remoteIp"`
}
func (h *HTTPRequest) Clone() *HTTPRequest {
return &HTTPRequest{
Method: h.Method,
URL: h.URL,
UserAgent: h.UserAgent,
Referrer: h.Referrer,
ResponseStatusCode: h.ResponseStatusCode,
RemoteIP: h.RemoteIP,
}
}
func (h *HTTPRequest) MarshalLogObject(e zapcore.ObjectEncoder) error {
e.AddString("method", h.Method)
e.AddString("url", h.URL)
e.AddString("userAgent", h.UserAgent)
e.AddString("referrer", h.Referrer)
e.AddInt("responseStatusCode", h.ResponseStatusCode)
e.AddString("remoteIp", h.RemoteIP)
return nil
}
type ReportLocation struct {
FilePath string
LineNumber int
FunctionName string
}
func (r *ReportLocation) Clone() *ReportLocation {
return &ReportLocation{
FilePath: r.FilePath,
LineNumber: r.LineNumber,
FunctionName: r.FunctionName,
}
}
func (r *ReportLocation) MarshalLogObject(e zapcore.ObjectEncoder) error {
e.AddString("filePath", r.FilePath)
e.AddInt("lineNumber", r.LineNumber)
e.AddString("functionName", r.FunctionName)
return nil
}