Skip to content

Commit

Permalink
Merge pull request #20 from tburschka/route-tag
Browse files Browse the repository at this point in the history
Add route tag
  • Loading branch information
faabiosr committed Apr 27, 2023
2 parents 012538e + 88b9d49 commit 74b36a9
Show file tree
Hide file tree
Showing 10 changed files with 30 additions and 10 deletions.
1 change: 1 addition & 0 deletions charm.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ type CharmLogConfig struct {
// - @host
// - @method
// - @path
// - @route
// - @protocol
// - @referer
// - @user_agent
Expand Down
5 changes: 3 additions & 2 deletions charm_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,15 +27,16 @@ func TestCharmLogWithConfig(t *testing.T) {
{"handle request", "invalid log: handle request info not found"},
{"id=123", "invalid log: request id not found"},
{`remote_ip=http://foo.bar`, "invalid log: remote ip not found"},
{`uri=http://some?name=john`, "invalid log: uri not found"},
{`uri=http://some/foo/456?name=john`, "invalid log: uri not found"},
{"host=some", "invalid log: host not found"},
{"method=POST", "invalid log: method not found"},
{"status=200", "invalid log: status not found"},
{"latency=", "invalid log: latency not found"},
{"latency_human=", "invalid log: latency_human not found"},
{"bytes_in=0", "invalid log: bytes_in not found"},
{"bytes_out=4", "invalid log: bytes_out not found"},
{"path=/", "invalid log: path not found"},
{"route=/foo/:id", "invalid log: route not found"},
{"path=/foo/456", "invalid log: path not found"},
{"protocol=HTTP/1.1", "invalid log: protocol not found"},
{`referer=http://foo.bar`, "invalid log: referer not found"},
{"user_agent=cli-agent", "invalid log: user_agent not found"},
Expand Down
2 changes: 2 additions & 0 deletions log.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ const (
logHost = "@host"
logMethod = "@method"
logPath = "@path"
logRoute = "@route"
logProtocol = "@protocol"
logReferer = "@referer"
logUserAgent = "@user_agent"
Expand Down Expand Up @@ -119,6 +120,7 @@ func mapTags(ec echo.Context, latency time.Duration) map[string]interface{} {
}

tags[logPath] = path
tags[logRoute] = ec.Path()
tags[logProtocol] = req.Proto
tags[logReferer] = req.Referer()
tags[logUserAgent] = req.UserAgent()
Expand Down
14 changes: 12 additions & 2 deletions log_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ var testFields = map[string]string{
"empty": "",
"id": logID,
"path": logPath,
"route": logRoute,
"protocol": logProtocol,
"referer": logReferer,
"user_agent": logUserAgent,
Expand Down Expand Up @@ -67,7 +68,9 @@ func postCtx(t *testing.T) echo.Context {
form := url.Values{}
form.Add("username", "doejohn")

req := httptest.NewRequest(echo.POST, "http://some?name=john", strings.NewReader(form.Encode()))
u, _ := url.Parse("http://some/foo/456?name=john")

req := httptest.NewRequest(echo.POST, u.String(), strings.NewReader(form.Encode()))

req.Header.Add(echo.HeaderContentType, echo.MIMEApplicationForm)
req.Header.Add("Referer", "http://foo.bar")
Expand All @@ -83,5 +86,12 @@ func postCtx(t *testing.T) echo.Context {
rec.Header().Add(echo.HeaderXRequestID, "123")

e := echo.New()
return e.NewContext(req, rec)

e.Add(echo.GET, "/foo/:id", testHandler)

ec := e.NewContext(req, rec)

e.Router().Find(echo.GET, u.RequestURI(), ec)

return ec
}
1 change: 1 addition & 0 deletions logrus.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ type LogrusConfig struct {
// - @host
// - @method
// - @path
// - @route
// - @protocol
// - @referer
// - @user_agent
Expand Down
5 changes: 3 additions & 2 deletions logrus_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,15 +30,16 @@ func TestLogrusWithConfig(t *testing.T) {
{"handle request", "invalid log: handle request info not found"},
{"id=123", "invalid log: request id not found"},
{`remote_ip="http://foo.bar"`, "invalid log: remote ip not found"},
{`uri="http://some?name=john"`, "invalid log: uri not found"},
{`uri="http://some/foo/456?name=john"`, "invalid log: uri not found"},
{"host=some", "invalid log: host not found"},
{"method=POST", "invalid log: method not found"},
{"status=200", "invalid log: status not found"},
{"latency=", "invalid log: latency not found"},
{"latency_human=", "invalid log: latency_human not found"},
{"bytes_in=0", "invalid log: bytes_in not found"},
{"bytes_out=4", "invalid log: bytes_out not found"},
{"path=/", "invalid log: path not found"},
{"path=/foo/456", "invalid log: path not found"},
{`route="/foo/:id"`, "invalid log: route not found"},
{"protocol=HTTP/1.1", "invalid log: protocol not found"},
{`referer="http://foo.bar"`, "invalid log: referer not found"},
{"user_agent=cli-agent", "invalid log: user_agent not found"},
Expand Down
1 change: 1 addition & 0 deletions zap.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ type ZapLogConfig struct {
// - @host
// - @method
// - @path
// - @route
// - @protocol
// - @referer
// - @user_agent
Expand Down
5 changes: 3 additions & 2 deletions zap_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,13 +32,14 @@ func TestZapLogWithConfig(t *testing.T) {
{"msg", entry.Message, "handle request"},
{"id", ectx["id"], "123"},
{"remote_ip", ectx["remote_ip"], "http://foo.bar"},
{"uri", ectx["uri"], "http://some?name=john"},
{"uri", ectx["uri"], "http://some/foo/456?name=john"},
{"host", ectx["host"], "some"},
{"method", ectx["method"], "POST"},
{"status", ectx["status"], int64(http.StatusOK)},
{"bytes_in", ectx["bytes_in"], "0"},
{"bytes_out", ectx["bytes_out"], "4"},
{"path", ectx["path"], "/"},
{"path", ectx["path"], "/foo/456"},
{"route", ectx["route"], "/foo/:id"},
{"protocol", ectx["protocol"], "HTTP/1.1"},
{"referer", ectx["referer"], "http://foo.bar"},
{"user_agent", ectx["user_agent"], "cli-agent"},
Expand Down
1 change: 1 addition & 0 deletions zerolog.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ type ZeroLogConfig struct {
// - @host
// - @method
// - @path
// - @route
// - @protocol
// - @referer
// - @user_agent
Expand Down
5 changes: 3 additions & 2 deletions zerolog_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,15 +29,16 @@ func TestZeroLogWithConfig(t *testing.T) {
{"handle request", "invalid log: handle request info not found"},
{"id=123", "invalid log: request id not found"},
{`remote_ip=http://foo.bar`, "invalid log: remote ip not found"},
{`uri=http://some?name=john`, "invalid log: uri not found"},
{`uri=http://some/foo/456?name=john`, "invalid log: uri not found"},
{"host=some", "invalid log: host not found"},
{"method=POST", "invalid log: method not found"},
{"status=200", "invalid log: status not found"},
{"latency=", "invalid log: latency not found"},
{"latency_human=", "invalid log: latency_human not found"},
{"bytes_in=0", "invalid log: bytes_in not found"},
{"bytes_out=4", "invalid log: bytes_out not found"},
{"path=/", "invalid log: path not found"},
{"path=/foo/456", "invalid log: path not found"},
{`route=/foo/:id`, "invalid log: route not found"},
{"protocol=HTTP/1.1", "invalid log: protocol not found"},
{`referer=http://foo.bar`, "invalid log: referer not found"},
{"user_agent=cli-agent", "invalid log: user_agent not found"},
Expand Down

0 comments on commit 74b36a9

Please sign in to comment.