forked from mingrammer/flog
-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathlog.go
164 lines (151 loc) · 4.99 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
package main
import (
"fmt"
"strings"
"time"
"github.com/brianvoe/gofakeit/v6"
)
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"}`
// SpringBootLogFormat : "{timestamp} {severity} {pid} --- [{thread-id}] {classname}: {message}"
SpringBootLogFormat = "%s %s %s --- [%s] %s: %s"
)
func message(length int) string {
if length < 1 {
return ""
}
msg := gofakeit.Word()
for len(msg) <= length {
msg = msg + " " + gofakeit.Word()
}
return msg[:length-1]
}
// 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.HTTPStatusCode(),
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.HTTPStatusCode(),
gofakeit.Number(30, 100000),
gofakeit.URL(),
gofakeit.UserAgent(),
)
}
// NewApacheErrorLog creates a log string with apache error log format
func NewApacheErrorLog(t time.Time, length int) string {
preMsg := 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(),
)
return preMsg + message(length-len(preMsg))
}
// NewRFC3164Log creates a log string with syslog (RFC3164) format
func NewRFC3164Log(t time.Time, length int) string {
preMsg := fmt.Sprintf(
RFC3164Log,
gofakeit.Number(0, 191),
t.Format(RFC3164),
strings.ToLower(gofakeit.Username()),
gofakeit.Word(),
gofakeit.Number(1, 10000),
gofakeit.HackerPhrase(),
)
return preMsg + message(length-len(preMsg))
}
// NewRFC5424Log creates a log string with syslog (RFC5424) format
func NewRFC5424Log(t time.Time, length int) string {
preMsg := 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(),
)
return preMsg + message(length-len(preMsg))
}
// 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.HTTPStatusCode(),
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.HTTPStatusCode(),
gofakeit.Number(0, 30000),
gofakeit.URL(),
)
}
// NewSpringBootLogFormat creates a log string with spring boot default format
func NewSpringBootLogFormat(t time.Time, length int) string {
preMsg := fmt.Sprintf(
SpringBootLogFormat,
t.Format(Java),
strings.ToUpper(gofakeit.LogLevel("general")),
gofakeit.StreetNumber(),
gofakeit.BuzzWord(),
gofakeit.DomainSuffix()+"."+gofakeit.BS()+"."+gofakeit.Word()+"."+gofakeit.FirstName(),
gofakeit.HackerPhrase(),
)
return preMsg + message(length-len(preMsg))
}