Closed YamiOdymel closed 4 months ago
nvm I think I got the solution:
net/http: do not log error in http.Server for TCP probes
func main() {
h := gin.Default()
server:= &http.Server{
Addr: ":8080",
Handler: h,
ReadTimeout: 30 * time.Second,
WriteTimeout: 30 * time.Second,
MaxHeaderBytes: 1 << 20,
ErrorLog: httpLogger(),
}
server.ListenAndServeTLS("cert.pem", "key.pem")
}
func httpLogger() *log.Logger {
pr, pw := io.Pipe()
logger := log.New(pw, "http: ", log.LstdFlags|log.Lmsgprefix|log.Lmicroseconds)
scanner := bufio.NewScanner(pr)
go func() {
for scanner.Scan() {
go func(line string) {
if !strings.HasSuffix(line, ": EOF") {
fmt.Fprintln(os.Stdout, line)
}
}(scanner.Text())
}
fmt.Fprintln(os.Stdout, "log scanner stopped:", scanner.Err())
}()
return logger
}
Logger interface guarantees single write per log message so you don't need go routine and a scanner, see https://github.com/golang/go/issues/26918#issuecomment-974257205
The Gin server outputs lots of
TLS handshake error
I know they are spambots poking my server without a valid TLS handshake, so I'd like to hide them all.