From d8309ab6f2060c6585e465b823d4488ef0f03f7c Mon Sep 17 00:00:00 2001 From: Umputun Date: Tue, 13 Apr 2021 12:45:49 -0500 Subject: [PATCH] allow optional stdout logging --- app/main.go | 2 ++ app/proxy/proxy.go | 18 +++++++++++++++++- 2 files changed, 19 insertions(+), 1 deletion(-) diff --git a/app/main.go b/app/main.go index c3fa74e..2f6ed77 100644 --- a/app/main.go +++ b/app/main.go @@ -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, diff --git a/app/proxy/proxy.go b/app/proxy/proxy.go index e207570..848d334 100644 --- a/app/proxy/proxy.go +++ b/app/proxy/proxy.go @@ -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,