Skip to content

Commit

Permalink
feat: add clientIP for accesslog (#24)
Browse files Browse the repository at this point in the history
Signed-off-by: Dup4 <[email protected]>
  • Loading branch information
Dup4 authored Mar 20, 2023
1 parent 67a82fa commit b153fe7
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 2 deletions.
5 changes: 3 additions & 2 deletions accesslog/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,7 @@ package main

import (
"context"

"github.com/cloudwego/hertz/pkg/app"
"github.com/cloudwego/hertz/pkg/app/server"
"github.com/cloudwego/hertz/pkg/common/hlog"
Expand Down Expand Up @@ -200,6 +200,7 @@ const (
TagPort = "port"
TagIP = "ip"
TagIPs = "ips"
TagClientIP = "clientIP"
TagHost = "host"
TagMethod = "method"
TagPath = "path"
Expand Down Expand Up @@ -251,4 +252,4 @@ func main() {
h.Spin()
}

```
```
4 changes: 4 additions & 0 deletions accesslog/tag.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ const (
TagIP = "ip"
TagIPs = "ips"
TagHost = "host"
TagClientIP = "clientIP"
TagMethod = "method"
TagPath = "path"
TagURL = "url"
Expand Down Expand Up @@ -102,6 +103,9 @@ var Tags = map[string]logTagFunc{
TagHost: func(ctx context.Context, c *app.RequestContext, buf *bytebufferpool.ByteBuffer) (int, error) {
return buf.WriteString(string(c.Request.URI().Host()))
},
TagClientIP: func(ctx context.Context, c *app.RequestContext, buf *bytebufferpool.ByteBuffer) (int, error) {
return buf.WriteString(c.ClientIP())
},
TagPath: func(ctx context.Context, c *app.RequestContext, buf *bytebufferpool.ByteBuffer) (int, error) {
return buf.WriteString(string(c.Request.Path()))
},
Expand Down

0 comments on commit b153fe7

Please sign in to comment.