@@ -19,7 +19,7 @@ import (
19
19
20
20
// It is critical that NGINX uses the following log format:
21
21
/*
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"';
23
23
* access_log /var/log/nginx/access.log config;
24
24
*/
25
25
@@ -104,20 +104,10 @@ func TailNginxLogFile(logFile string, lastUpdated time.Time, channels ...chan *N
104
104
105
105
// parseNginxDate parses a single line of the nginx log file and returns the time.Time of the line
106
106
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 ))
117
108
if err != nil {
118
109
return time.Time {}, err
119
110
}
120
-
121
111
return tm , nil
122
112
}
123
113
@@ -126,7 +116,7 @@ func parseNginxDate(line string) (time.Time, error) {
126
116
// If the log file is not in the correct format or if some other part of the parsing fails
127
117
// this function will return an error
128
118
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";
130
120
quoteList := reQuotes .FindAllString (line , - 1 )
131
121
132
122
if len (quoteList ) != 7 {
@@ -141,8 +131,16 @@ func parseNginxLine(line string) (*NginxLogEntry, error) {
141
131
var entry NginxLogEntry
142
132
var err error
143
133
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
+
144
142
// IPv4 or IPv6 address
145
- entry .IP = net .ParseIP (quoteList [0 ])
143
+ entry .IP = net .ParseIP (quoteList [1 ])
146
144
if entry .IP == nil {
147
145
return nil , errors .New ("failed to parse ip" )
148
146
}
@@ -159,14 +157,6 @@ func parseNginxLine(line string) (*NginxLogEntry, error) {
159
157
entry .City = nil
160
158
}
161
159
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
-
170
160
// Method url http version
171
161
split := strings .Split (quoteList [2 ], " " )
172
162
if len (split ) != 3 {
0 commit comments