Write HTTP errors to log

This commit is contained in:
Igor Chubin 2022-11-20 17:53:51 +01:00
parent d804310586
commit d4e96dbf3a
2 changed files with 26 additions and 6 deletions

View file

@ -12,6 +12,9 @@ type Logging struct {
// AccessLog path. // AccessLog path.
AccessLog string AccessLog string
// ErrorsLog path.
ErrorsLog string
// Interval between access log flushes, in seconds. // Interval between access log flushes, in seconds.
Interval int Interval int
} }
@ -38,6 +41,7 @@ type Server struct {
var Conf = Config{ var Conf = Config{
Logging{ Logging{
AccessLog: "/wttr.in/log/access.log", AccessLog: "/wttr.in/log/access.log",
ErrorsLog: "/wttr.in/log/errors.log",
Interval: 300, Interval: 300,
}, },
Server{ Server{

View file

@ -4,6 +4,7 @@ import (
"context" "context"
"crypto/tls" "crypto/tls"
"fmt" "fmt"
"io"
"log" "log"
"net" "net"
"net/http" "net/http"
@ -16,6 +17,7 @@ const uplinkSrvAddr = "127.0.0.1:9002"
const uplinkTimeout = 30 const uplinkTimeout = 30
const prefetchInterval = 300 const prefetchInterval = 300
const lruCacheSize = 12800 const lruCacheSize = 12800
const logLineStart = "LOG_LINE_START "
// plainTextAgents contains signatures of the plain-text agents // plainTextAgents contains signatures of the plain-text agents
var plainTextAgents = []string{ var plainTextAgents = []string{
@ -72,19 +74,19 @@ func copyHeader(dst, src http.Header) {
} }
} }
func serveHTTP(mux *http.ServeMux, port int, errs chan<- error) { func serveHTTP(mux *http.ServeMux, port int, logFile io.Writer, errs chan<- error) {
srv := &http.Server{ srv := &http.Server{
Addr: fmt.Sprintf(":%d", port), Addr: fmt.Sprintf(":%d", port),
ErrorLog: log.New(logFile, logLineStart, log.LstdFlags),
ReadTimeout: 5 * time.Second, ReadTimeout: 5 * time.Second,
WriteTimeout: 10 * time.Second, WriteTimeout: 10 * time.Second,
IdleTimeout: 1 * time.Second, IdleTimeout: 1 * time.Second,
Handler: mux, Handler: mux,
} }
// srv.SetKeepAlivesEnabled(false)
errs <- srv.ListenAndServe() errs <- srv.ListenAndServe()
} }
func serveHTTPS(mux *http.ServeMux, port int, errs chan<- error) { func serveHTTPS(mux *http.ServeMux, port int, logFile io.Writer, errs chan<- error) {
tlsConfig := &tls.Config{ tlsConfig := &tls.Config{
// CipherSuites: []uint16{ // CipherSuites: []uint16{
// tls.TLS_CHACHA20_POLY1305_SHA256, // tls.TLS_CHACHA20_POLY1305_SHA256,
@ -95,13 +97,13 @@ func serveHTTPS(mux *http.ServeMux, port int, errs chan<- error) {
} }
srv := &http.Server{ srv := &http.Server{
Addr: fmt.Sprintf(":%d", port), Addr: fmt.Sprintf(":%d", port),
ErrorLog: log.New(logFile, logLineStart, log.LstdFlags),
ReadTimeout: 5 * time.Second, ReadTimeout: 5 * time.Second,
WriteTimeout: 20 * time.Second, WriteTimeout: 20 * time.Second,
IdleTimeout: 1 * time.Second, IdleTimeout: 1 * time.Second,
TLSConfig: tlsConfig, TLSConfig: tlsConfig,
Handler: mux, Handler: mux,
} }
// srv.SetKeepAlivesEnabled(false)
errs <- srv.ListenAndServeTLS(Conf.Server.TLSCertFile, Conf.Server.TLSKeyFile) errs <- srv.ListenAndServeTLS(Conf.Server.TLSCertFile, Conf.Server.TLSKeyFile)
} }
@ -120,8 +122,22 @@ func main() {
// numberOfServers started. If 0, exit. // numberOfServers started. If 0, exit.
numberOfServers int numberOfServers int
errorsLog *LogSuppressor = NewLogSuppressor(
Conf.Logging.ErrorsLog,
[]string{
"error reading preface from client",
"TLS handshake error from",
},
logLineStart,
)
) )
err := errorsLog.Open()
if err != nil {
log.Fatalln("errors log:", err)
}
mux.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) { mux.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
if err := logger.Log(r); err != nil { if err := logger.Log(r); err != nil {
log.Println(err) log.Println(err)
@ -136,11 +152,11 @@ func main() {
}) })
if Conf.Server.PortHTTP != 0 { if Conf.Server.PortHTTP != 0 {
go serveHTTP(mux, Conf.Server.PortHTTP, errs) go serveHTTP(mux, Conf.Server.PortHTTP, errorsLog, errs)
numberOfServers++ numberOfServers++
} }
if Conf.Server.PortHTTPS != 0 { if Conf.Server.PortHTTPS != 0 {
go serveHTTPS(mux, Conf.Server.PortHTTPS, errs) go serveHTTPS(mux, Conf.Server.PortHTTPS, errorsLog, errs)
numberOfServers++ numberOfServers++
} }
if numberOfServers == 0 { if numberOfServers == 0 {