forked from SigNoz/flog
-
Notifications
You must be signed in to change notification settings - Fork 0
/
log.go
186 lines (170 loc) · 5.85 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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
package main
import (
"fmt"
"strings"
"time"
"github.com/brianvoe/gofakeit"
"github.com/openzipkin/zipkin-go/idgenerator"
"github.com/tjarratt/babble"
)
const (
// ApacheCommonLog : {host} {user-identifier} {auth-user-id} [{datetime}] "{method} {request} {protocol}" {response-code} {bytes}
ApacheCommonLog = "%s - %s [%s] \"%s %s %s\" %d %d"
// ApacheCombinedLog : {host} {user-identifier} {auth-user-id} [{datetime}] "{method} {request} {protocol}" {response-code} {bytes} "{referrer}" "{agent}"
ApacheCombinedLog = "%s - %s [%s] \"%s %s %s\" %d %d \"%s\" \"%s\""
// ApacheErrorLog : [{timestamp}] [{module}:{severity}] [pid {pid}:tid {thread-id}] [client %{client}:{port}] %{message}
ApacheErrorLog = "[%s] [%s:%s] [pid %d:tid %d] [client %s:%d] %s"
// RFC3164Log : <priority>{timestamp} {hostname} {application}[{pid}]: {message}
RFC3164Log = "<%d>%s %s %s[%d]: %s"
// RFC5424Log : <priority>{version} {iso-timestamp} {hostname} {application} {pid} {message-id} {structured-data} {message}
RFC5424Log = "<%d>%d %s %s %s %d ID%d %s %s"
// CommonLogFormat : {host} {user-identifier} {auth-user-id} [{datetime}] "{method} {request} {protocol}" {response-code} {bytes}
CommonLogFormat = "%s - %s [%s] \"%s %s %s\" %d %d"
// JSONLogFormat : {"host": "{host}", "user-identifier": "{user-identifier}", "datetime": "{datetime}", "method": "{method}", "request": "{request}", "protocol": "{protocol}", "status", {status}, "bytes": {bytes}, "referer": "{referer}"}
JSONLogFormat = `{"host":"%s", "user-identifier":"%s", "datetime":"%s", "method": "%s", "request": "%s", "protocol":"%s", "status":%d, "bytes":%d, "referer": "%s"}`
// JSONLogFormatWithTrace = {"host": "{host}", "user-identifier": "{user-identifier}", "datetime": "{datetime}", "method": "{method}", "request": "{request}", "protocol": "{protocol}", "status", {status}, "bytes": {bytes}, "referer": "{referer}"}
JSONLogFormatWithTrace = `{"host":"%s", "user-identifier":"%s", "datetime":"%s", "method": "%s", "request": "%s", "protocol":"%s", "status":%d, "bytes":%d, "referer": "%s", "traceId": "%s", "spanId": "%s"}`
// Extra
JSONLogFormatWithTraceExtraBytes = `{"host":"%s", "user-identifier":"%s", "datetime":"%s", "method": "%s", "request": "%s", "protocol":"%s", "status":%d, "bytes":%d, "referer": "%s", "traceId": "%s", "spanId": "%s", "dummyBytes": "%s"}`
)
// NewApacheCommonLog creates a log string with apache common log format
func NewApacheCommonLog(t time.Time) string {
return fmt.Sprintf(
ApacheCommonLog,
gofakeit.IPv4Address(),
RandAuthUserID(),
t.Format(Apache),
gofakeit.HTTPMethod(),
RandResourceURI(),
RandHTTPVersion(),
gofakeit.StatusCode(),
gofakeit.Number(0, 30000),
)
}
// NewApacheCombinedLog creates a log string with apache combined log format
func NewApacheCombinedLog(t time.Time) string {
return fmt.Sprintf(
ApacheCombinedLog,
gofakeit.IPv4Address(),
RandAuthUserID(),
t.Format(Apache),
gofakeit.HTTPMethod(),
RandResourceURI(),
RandHTTPVersion(),
gofakeit.StatusCode(),
gofakeit.Number(30, 100000),
gofakeit.URL(),
gofakeit.UserAgent(),
)
}
// NewApacheErrorLog creates a log string with apache error log format
func NewApacheErrorLog(t time.Time) string {
return fmt.Sprintf(
ApacheErrorLog,
t.Format(ApacheError),
gofakeit.Word(),
gofakeit.LogLevel("apache"),
gofakeit.Number(1, 10000),
gofakeit.Number(1, 10000),
gofakeit.IPv4Address(),
gofakeit.Number(1, 65535),
gofakeit.HackerPhrase(),
)
}
// NewRFC3164Log creates a log string with syslog (RFC3164) format
func NewRFC3164Log(t time.Time) string {
return fmt.Sprintf(
RFC3164Log,
gofakeit.Number(0, 191),
t.Format(RFC3164),
strings.ToLower(gofakeit.Username()),
gofakeit.Word(),
gofakeit.Number(1, 10000),
gofakeit.HackerPhrase(),
)
}
// NewRFC5424Log creates a log string with syslog (RFC5424) format
func NewRFC5424Log(t time.Time) string {
return fmt.Sprintf(
RFC5424Log,
gofakeit.Number(0, 191),
gofakeit.Number(1, 3),
t.Format(RFC5424),
gofakeit.DomainName(),
gofakeit.Word(),
gofakeit.Number(1, 10000),
gofakeit.Number(1, 1000),
"-", // TODO: structured data
gofakeit.HackerPhrase(),
)
}
// NewCommonLogFormat creates a log string with common log format
func NewCommonLogFormat(t time.Time) string {
return fmt.Sprintf(
CommonLogFormat,
gofakeit.IPv4Address(),
RandAuthUserID(),
t.Format(CommonLog),
gofakeit.HTTPMethod(),
RandResourceURI(),
RandHTTPVersion(),
gofakeit.StatusCode(),
gofakeit.Number(0, 30000),
)
}
// NewJSONLogFormat creates a log string with json log format
func NewJSONLogFormat(t time.Time) string {
return fmt.Sprintf(
JSONLogFormat,
gofakeit.IPv4Address(),
RandAuthUserID(),
t.Format(CommonLog),
gofakeit.HTTPMethod(),
RandResourceURI(),
RandHTTPVersion(),
gofakeit.StatusCode(),
gofakeit.Number(0, 30000),
gofakeit.URL(),
)
}
var letters = []rune("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ _-")
var babbler = babble.NewBabbler()
// NewJSONLogFormatWithTrace creates a log string with json log format
func NewJSONLogFormatWithTrace(lineBytes int, t time.Time) string {
g := idgenerator.NewRandom128()
traceId := g.TraceID()
babbler.Separator = " "
babbler.Count = 100
if lineBytes > 350 {
// taking the default log to be about 350 bytes
return fmt.Sprintf(
JSONLogFormatWithTraceExtraBytes,
gofakeit.IPv4Address(),
RandAuthUserID(),
t.Format(ClickHouse),
gofakeit.HTTPMethod(),
RandResourceURI(),
RandHTTPVersion(),
gofakeit.StatusCode(),
gofakeit.Number(0, 30000),
gofakeit.URL(),
traceId.String(),
g.SpanID(traceId).String(),
babbler.Babble(),
)
}
return fmt.Sprintf(
JSONLogFormatWithTrace,
gofakeit.IPv4Address(),
RandAuthUserID(),
t.Format(ClickHouse),
gofakeit.HTTPMethod(),
RandResourceURI(),
RandHTTPVersion(),
gofakeit.StatusCode(),
gofakeit.Number(0, 30000),
gofakeit.URL(),
traceId.String(),
g.SpanID(traceId).String(),
)
}