-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #12 from lupinthe14th/develop
improve: change logger package from logrus to zerolog
- Loading branch information
Showing
7 changed files
with
141 additions
and
53 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
package main | ||
|
||
import ( | ||
"io" | ||
"io/ioutil" | ||
"net/http" | ||
"time" | ||
|
||
"github.com/rs/zerolog" | ||
) | ||
|
||
// CustomLogger implements the estransport.Logger interface. | ||
type CustomLogger struct { | ||
zerolog.Logger | ||
} | ||
|
||
// LogRoundTrip prints the information about request and response. | ||
func (l *CustomLogger) LogRoundTrip( | ||
req *http.Request, | ||
res *http.Response, | ||
err error, | ||
start time.Time, | ||
dur time.Duration, | ||
) error { | ||
var ( | ||
e *zerolog.Event | ||
nReq int64 | ||
nRes int64 | ||
) | ||
|
||
// Set error level. | ||
// | ||
switch { | ||
case err != nil: | ||
e = l.Error() | ||
case res != nil && res.StatusCode > 0 && res.StatusCode < 300: | ||
e = l.Debug() | ||
case res != nil && res.StatusCode > 299 && res.StatusCode < 500: | ||
e = l.Warn() | ||
case res != nil && res.StatusCode > 499: | ||
e = l.Error() | ||
default: | ||
e = l.Error() | ||
} | ||
|
||
// Count number of bytes in request and response. | ||
// | ||
if req != nil && req.Body != nil && req.Body != http.NoBody { | ||
nReq, _ = io.Copy(ioutil.Discard, req.Body) | ||
} | ||
if res != nil && res.Body != nil && res.Body != http.NoBody { | ||
nRes, _ = io.Copy(ioutil.Discard, res.Body) | ||
} | ||
|
||
// Log event. | ||
// | ||
e.Str("method", req.Method). | ||
Int("status_code", res.StatusCode). | ||
Dur("duration", dur). | ||
Int64("req_bytes", nReq). | ||
Int64("res_bytes", nRes). | ||
Msg(req.URL.String()) | ||
|
||
return nil | ||
} | ||
|
||
// RequestBodyEnabled makes the client pass request body to logger | ||
func (l *CustomLogger) RequestBodyEnabled() bool { return true } | ||
|
||
// ResponseBodyEnabled makes the client pass response body to logger | ||
func (l *CustomLogger) ResponseBodyEnabled() bool { return true } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.