Skip to content

Commit

Permalink
Fixes of request logger
Browse files Browse the repository at this point in the history
  • Loading branch information
rekby committed Dec 2, 2020
1 parent 91061ce commit f97f3a9
Show file tree
Hide file tree
Showing 7 changed files with 11 additions and 9 deletions.
2 changes: 1 addition & 1 deletion cmd/a_main-packr.go

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion cmd/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ type logConfig struct {
EnableLogToFile bool
EnableLogToStdErr bool
LogLevel string
DisableAccessLog bool
EnableAccessLog bool
EnableRotate bool
DeveloperMode bool
File string
Expand Down
2 changes: 1 addition & 1 deletion cmd/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,7 @@ func startProgram(config *configType) {
err = tlsListener.Start(ctx, registry)
log.DebugFatal(logger, err, "StartAutoRenew tls listener")

config.Proxy.DisableAccessLog = config.Log.DisableAccessLog
config.Proxy.EnableAccessLog = config.Log.EnableAccessLog
p := proxy.NewHTTPProxy(ctx, tlsListener)
p.GetContext = func(req *http.Request) (i context.Context, e error) {
localAddr := req.Context().Value(http.LocalAddrContextKey).(net.Addr)
Expand Down
4 changes: 2 additions & 2 deletions cmd/static/default-config.toml
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,8 @@ EnableLogToStdErr = true
# verbose level of log, one of: debug, info, warning, error, fatal
LogLevel = "info"

# Disable write info about every http request (but write info about connections if need by level)
DisableAccessLog = false
# Enable write info about every http request (but write info about connections if need by level)
EnableAccessLog = true

# Enable self log rotating
EnableRotate = true
Expand Down
4 changes: 2 additions & 2 deletions internal/proxy/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ type Config struct {
KeepAliveTimeoutSeconds int
HTTPSBackend bool
HTTPSBackendIgnoreCert bool
DisableAccessLog bool
EnableAccessLog bool
}

func (c *Config) Apply(ctx context.Context, p *HTTPProxy) error {
Expand All @@ -47,7 +47,7 @@ func (c *Config) Apply(ctx context.Context, p *HTTPProxy) error {
appendDirector(c.getHeadersDirector)
appendDirector(c.getSchemaDirector)
p.HTTPTransport = Transport{c.HTTPSBackendIgnoreCert}
p.EnableAccessLog = !c.DisableAccessLog
p.EnableAccessLog = c.EnableAccessLog

if resErr != nil {
zc.L(ctx).Error("Can't parse proxy config", zap.Error(resErr))
Expand Down
2 changes: 2 additions & 0 deletions internal/proxy/http-proxy.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,12 +62,14 @@ func (p *HTTPProxy) Close() error {
// Any public fields must not change after Start called
func (p *HTTPProxy) Start() error {
if p.HTTPTransport != nil {
p.logger.Info("Set transport to reverse proxy")
p.httpReverseProxy.Transport = p.HTTPTransport
}

if p.EnableAccessLog {
p.httpReverseProxy.Transport = NewTransportLogger(p.httpReverseProxy.Transport)
}
p.logger.Info("Access log", zap.Bool("enabled", p.EnableAccessLog))

mux := &http.ServeMux{}
mux.HandleFunc("/", func(writer http.ResponseWriter, request *http.Request) {
Expand Down
4 changes: 2 additions & 2 deletions internal/proxy/transport_logger.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,11 @@ func (t TransportLogger) RoundTrip(request *http.Request) (resp *http.Response,
start := time.Now()

defer func() {
log.DebugErrorCtx(request.Context(), err, "Request",
log.InfoErrorCtx(request.Context(), err, "Request",
zap.Duration("duration_without_body", time.Since(start)),
zap.String("initiator_addr", request.RemoteAddr),
zap.String("metod", request.Method),
zap.String("host", request.Header.Get("Host")),
zap.String("host", request.Host),
zap.String("path", request.URL.Path),
zap.String("query", request.URL.RawQuery),
zap.Int("status_code", resp.StatusCode),
Expand Down

0 comments on commit f97f3a9

Please sign in to comment.