Skip to content
This repository was archived by the owner on Nov 21, 2024. It is now read-only.

Commit 456463d

Browse files
committed
Update NGINX log format placing the timestamp first
1 parent 598c8dc commit 456463d

File tree

1 file changed

+12
-22
lines changed

1 file changed

+12
-22
lines changed

nginx.go

+12-22
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ import (
1919

2020
// It is critical that NGINX uses the following log format:
2121
/*
22-
* log_format config '"$remote_addr" "$time_local" "$request" "$status" "$body_bytes_sent" "$request_length" "$http_user_agent"';
22+
* log_format config '"$time_local" "$remote_addr" "$request" "$status" "$body_bytes_sent" "$request_length" "$http_user_agent"';
2323
* access_log /var/log/nginx/access.log config;
2424
*/
2525

@@ -104,20 +104,10 @@ func TailNginxLogFile(logFile string, lastUpdated time.Time, channels ...chan *N
104104

105105
// parseNginxDate parses a single line of the nginx log file and returns the time.Time of the line
106106
func parseNginxDate(line string) (time.Time, error) {
107-
// "$remote_addr" "$time_local" "$request" "$status" "$body_bytes_sent" "$request_length" "$http_user_agent";
108-
quoteList := reQuotes.FindAllString(line, -1)
109-
110-
if len(quoteList) != 7 {
111-
return time.Time{}, errors.New("invalid number of quotes")
112-
}
113-
114-
// Time
115-
t := "\"02/Jan/2006:15:04:05 -0700\""
116-
tm, err := time.Parse(t, quoteList[1])
107+
tm, err := time.Parse("\"02/Jan/2006:15:04:05 -0700\"", reQuotes.FindString(line))
117108
if err != nil {
118109
return time.Time{}, err
119110
}
120-
121111
return tm, nil
122112
}
123113

@@ -126,7 +116,7 @@ func parseNginxDate(line string) (time.Time, error) {
126116
// If the log file is not in the correct format or if some other part of the parsing fails
127117
// this function will return an error
128118
func parseNginxLine(line string) (*NginxLogEntry, error) {
129-
// "$remote_addr" "$time_local" "$request" "$status" "$body_bytes_sent" "$request_length" "$http_user_agent";
119+
// "$time_local" "$remote_addr" "$request" "$status" "$body_bytes_sent" "$request_length" "$http_user_agent";
130120
quoteList := reQuotes.FindAllString(line, -1)
131121

132122
if len(quoteList) != 7 {
@@ -141,8 +131,16 @@ func parseNginxLine(line string) (*NginxLogEntry, error) {
141131
var entry NginxLogEntry
142132
var err error
143133

134+
// Time
135+
t := "02/Jan/2006:15:04:05 -0700"
136+
tm, err := time.Parse(t, quoteList[0])
137+
if err != nil {
138+
return nil, err
139+
}
140+
entry.Time = tm
141+
144142
// IPv4 or IPv6 address
145-
entry.IP = net.ParseIP(quoteList[0])
143+
entry.IP = net.ParseIP(quoteList[1])
146144
if entry.IP == nil {
147145
return nil, errors.New("failed to parse ip")
148146
}
@@ -159,14 +157,6 @@ func parseNginxLine(line string) (*NginxLogEntry, error) {
159157
entry.City = nil
160158
}
161159

162-
// Time
163-
t := "02/Jan/2006:15:04:05 -0700"
164-
tm, err := time.Parse(t, quoteList[1])
165-
if err != nil {
166-
return nil, err
167-
}
168-
entry.Time = tm
169-
170160
// Method url http version
171161
split := strings.Split(quoteList[2], " ")
172162
if len(split) != 3 {

0 commit comments

Comments
 (0)