allow optional stdout logging

This commit is contained in:
Umputun 2021-04-13 12:45:49 -05:00
parent 5479063f4e
commit d8309ab6f2
2 changed files with 19 additions and 1 deletions

View File

@ -45,6 +45,7 @@ var opts struct {
} `group:"assets" namespace:"assets" env-namespace:"ASSETS"`
Logger struct {
StdOut bool `long:"stdout" env:"STDOUT" description:"enable stdout logging"`
Enabled bool `long:"enabled" env:"ENABLED" description:"enable access and error rotated logs"`
FileName string `long:"file" env:"FILE" default:"access.log" description:"location of access log"`
MaxSize int `long:"max-size" env:"MAX_SIZE" default:"100" description:"maximum size in megabytes before it gets rotated"`
@ -148,6 +149,7 @@ func main() {
SSLConfig: sslConfig,
ProxyHeaders: opts.ProxyHeaders,
AccessLog: accessLog,
StdOutEnabled: opts.Logger.StdOut,
DisableSignature: opts.NoSignature,
Timeouts: proxy.Timeouts{
ReadHeader: opts.Timeouts.ReadHeader,

View File

@ -14,6 +14,7 @@ import (
log "github.com/go-pkgz/lgr"
R "github.com/go-pkgz/rest"
"github.com/go-pkgz/rest/logger"
"github.com/gorilla/handlers"
"github.com/pkg/errors"
@ -21,7 +22,7 @@ import (
)
// Http is a proxy server for both http and https
type Http struct { //nolint golint
type Http struct { // nolint golint
Matcher
Address string
AssetsLocation string
@ -32,6 +33,7 @@ type Http struct { //nolint golint
SSLConfig SSLConfig
Version string
AccessLog io.Writer
StdOutEnabled bool
DisableSignature bool
Timeouts Timeouts
}
@ -88,6 +90,7 @@ func (h *Http) Run(ctx context.Context) error {
h.pingHandler,
h.healthMiddleware,
h.accessLogHandler(h.AccessLog),
h.stdoutLogHandler(h.StdOutEnabled),
R.SizeLimit(h.MaxBodySize),
R.Headers(h.ProxyHeaders...),
h.gzipHandler(),
@ -240,6 +243,19 @@ func (h *Http) accessLogHandler(wr io.Writer) func(next http.Handler) http.Handl
}
}
func (h *Http) stdoutLogHandler(enable bool) func(next http.Handler) http.Handler {
if enable {
return logger.New(logger.Log(log.Default()), logger.Prefix("[INFO]")).Handler
}
return func(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
next.ServeHTTP(w, r)
})
}
}
func (h *Http) makeHTTPServer(addr string, router http.Handler) *http.Server {
return &http.Server{
Addr: addr,