1
0
mirror of https://github.com/kataras/iris.git synced 2025-12-17 09:57:01 +00:00
This commit is contained in:
Gerasimos (Makis) Maropoulos
2022-04-13 02:25:47 +03:00
parent 3582427df6
commit ecb1c617df
5 changed files with 91 additions and 9 deletions

62
iris.go
View File

@@ -1,6 +1,7 @@
package iris
import (
"bytes"
stdContext "context"
"errors"
"fmt"
@@ -475,6 +476,40 @@ func (app *Application) ConfigureHost(configurators ...host.Configurator) *Appli
return app
}
const serverLoggerPrefix = "[HTTP Server] "
type customHostServerLogger struct { // see #1875
parent io.Writer
ignoreLogs [][]byte
}
var newLineBytes = []byte("\n")
func newCustomHostServerLogger(w io.Writer, ignoreLogs []string) *customHostServerLogger {
prefixAsByteSlice := []byte(serverLoggerPrefix)
// build the ignore lines.
ignoreLogsAsByteSlice := make([][]byte, 0, len(ignoreLogs))
for _, s := range ignoreLogs {
ignoreLogsAsByteSlice = append(ignoreLogsAsByteSlice, append(prefixAsByteSlice, []byte(s)...)) // append([]byte(s), newLineBytes...)
}
return &customHostServerLogger{
parent: w,
ignoreLogs: ignoreLogsAsByteSlice,
}
}
func (l *customHostServerLogger) Write(p []byte) (int, error) {
for _, ignoredLogBytes := range l.ignoreLogs {
if bytes.Equal(bytes.TrimSuffix(p, newLineBytes), ignoredLogBytes) {
return 0, nil
}
}
return l.parent.Write(p)
}
// NewHost accepts a standard *http.Server object,
// completes the necessary missing parts of that "srv"
// and returns a new, ready-to-use, host (supervisor).
@@ -487,9 +522,10 @@ func (app *Application) NewHost(srv *http.Server) *host.Supervisor {
srv.Handler = app.Router
}
// check if different ErrorLog provided, if not bind it with the framework's logger
// check if different ErrorLog provided, if not bind it with the framework's logger.
if srv.ErrorLog == nil {
srv.ErrorLog = log.New(app.logger.Printer.Output, "[HTTP Server] ", 0)
serverLogger := newCustomHostServerLogger(app.logger.Printer.Output, app.config.IgnoreServerErrors)
srv.ErrorLog = log.New(serverLogger, serverLoggerPrefix, 0)
}
if addr := srv.Addr; addr == "" {
@@ -913,11 +949,23 @@ func Raw(f func() error) Runner {
}
}
// ErrServerClosed is returned by the Server's Serve, ServeTLS, ListenAndServe,
// and ListenAndServeTLS methods after a call to Shutdown or Close.
//
// A shortcut for the `http#ErrServerClosed`.
var ErrServerClosed = http.ErrServerClosed
var (
// ErrServerClosed is logged by the standard net/http server when the server is terminated.
// Ignore it by passing this error to the `iris.WithoutServerError` configurator
// on `Application.Run/Listen` method.
//
// An alias of the `http#ErrServerClosed`.
ErrServerClosed = http.ErrServerClosed
// ErrURLQuerySemicolon is logged by the standard net/http server when
// the request contains a semicolon (;) wihch, after go1.17 it's not used as a key-value separator character.
//
// Ignore it by passing this error to the `iris.WithoutServerError` configurator
// on `Application.Run/Listen` method.
//
// An alias of the `http#ErrServerClosed`.
ErrURLQuerySemicolon = errors.New("http: URL query contains semicolon, which is no longer a supported separator; parts of the query may be stripped when parsed; see golang.org/issue/25192")
)
// Listen builds the application and starts the server
// on the TCP network address "host:port" which