Skip to content

Commit

Permalink
add and implement zap module for logging
Browse files Browse the repository at this point in the history
  • Loading branch information
Elegant996 committed Sep 25, 2024
1 parent 02b3b36 commit 6c2074d
Showing 1 changed file with 26 additions and 1 deletion.
27 changes: 26 additions & 1 deletion client.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,9 @@ import (
"strconv"
"strings"
"time"

"go.uber.org/zap"
"go.uber.org/zap/zapcore"
)

// StatusRegex describes the pattern for a raw HTTP Response code.
Expand All @@ -44,6 +47,7 @@ var StatusRegex = regexp.MustCompile("(?i)(?:Status:|HTTP\\/[\\d\\.]+)\\s+(\\d{3
type client struct {
rwc net.Conn
// keepAlive bool // TODO: implement
logger *zap.Logger
}

// Do made the request and returns a io.Reader that translates the data read
Expand Down Expand Up @@ -81,9 +85,26 @@ type clientCloser struct {
io.Reader

status int
logger *zap.Logger
}

func (c clientCloser) Close() error { return c.rwc.Close() }
func (s clientCloser) Close() error {
stderr := s.r.stderr.Bytes()
if len(stderr) == 0 {
return s.rwc.Close()
}

logLevel := zapcore.WarnLevel
if s.status >= 400 {
logLevel = zapcore.ErrorLevel
}

if c := s.logger.Check(logLevel, "stderr"); c != nil {
c.Write(zap.ByteString("body", stderr))
}

return s.rwc.Close()
}

// Request returns a HTTP Response with Header and Body
// from scgi responder
Expand Down Expand Up @@ -147,10 +168,14 @@ func (c *client) Request(p map[string]string, req io.Reader) (resp *http.Respons
rwc: c.rwc,
Reader: rb,
status: resp.StatusCode,
logger: noopLogger,
}
if chunked(resp.TransferEncoding) {
closer.Reader = httputil.NewChunkedReader(rb)
}
if c.stderr {
closer.logger = c.logger
}
resp.Body = closer

return
Expand Down

0 comments on commit 6c2074d

Please sign in to comment.