Skip to content

Commit fe4ec13

Browse files
elboulangerojbkempf
authored andcommitted
logs: Factorize some code for readability
Create the line of logs in two passes: the first part of the line is the same in all cases, it's only the second part that varies depending on the situation. Now it's clear from reading the code.
1 parent 08a8156 commit fe4ec13

File tree

1 file changed

+9
-6
lines changed

1 file changed

+9
-6
lines changed

logs/logs.go

+9-6
Original file line numberDiff line numberDiff line change
@@ -169,6 +169,8 @@ func LogDownload(typ string, method string, statuscode int, p *mirrors.Results,
169169
errstr = err.Error()
170170
}
171171

172+
line := fmt.Sprintf("%s %d %s \"%s\" ip:%s", typ, statuscode, method, path, ip)
173+
172174
if (statuscode == 302 || statuscode == 200) && p != nil && len(p.MirrorList) > 0 {
173175
var distance, countries string
174176
m := p.MirrorList[0]
@@ -182,18 +184,19 @@ func LogDownload(typ string, method string, statuscode int, p *mirrors.Results,
182184
if m.Asnum > 0 && m.Asnum == p.ClientInfo.ASNum {
183185
sameASNum = "same"
184186
}
185-
186-
dlogger.l.Printf("%s %d %s \"%s\" ip:%s mirror:%s%s %sasn:%d distance:%skm countries:%s",
187-
typ, statuscode, method, path, ip, m.Name, fallback, sameASNum, m.Asnum, distance, countries)
187+
line += fmt.Sprintf(" mirror:%s%s %sasn:%d distance:%skm countries:%s",
188+
m.Name, fallback, sameASNum, m.Asnum, distance, countries)
188189
} else if statuscode == 404 && p != nil {
189-
dlogger.l.Printf("%s %d %s \"%s\" ip:%s", typ, statuscode, method, path, ip)
190+
// nothing to add to the log line
190191
} else if statuscode == 500 && p != nil {
191192
mirrorName := "unknown"
192193
if len(p.MirrorList) > 0 {
193194
mirrorName = p.MirrorList[0].Name
194195
}
195-
dlogger.l.Printf("%s %d %s \"%s\" ip:%s mirror:%s error:%s", typ, statuscode, method, path, ip, mirrorName, errstr)
196+
line += fmt.Sprintf(" mirror:%s error:%s", mirrorName, errstr)
196197
} else {
197-
dlogger.l.Printf("%s %d %s \"%s\" ip:%s error:%s", typ, statuscode, method, path, ip, errstr)
198+
line += fmt.Sprintf(" error:%s", errstr)
198199
}
200+
201+
dlogger.l.Print(line)
199202
}

0 commit comments

Comments
 (0)