diff --git a/_examples/logging/request-logger/accesslog/main.go b/_examples/logging/request-logger/accesslog/main.go index 768b7d0d..5a102446 100644 --- a/_examples/logging/request-logger/accesslog/main.go +++ b/_examples/logging/request-logger/accesslog/main.go @@ -41,6 +41,16 @@ func main() { ac.SetFormatter(&accesslog.Template{ Text: "{{.Now.Format .TimeFormat}}|{{.Latency}}|{{.Method}}|{{.Path}}|{{.RequestValuesLine}}|{{.Code}}|{{.Request}}|{{.Response}}|\n", }) + + Set custom request fields: + ac.AddField(func(ctx iris.Context) (string, interface{}) { + v := ctx.RemoteAddr() + if v == "" { + return "", nil + } + // the log entry name and its value. + return "IP", v + }) */ defer ac.Close() diff --git a/context/context.go b/context/context.go index 1512b8d7..421be940 100644 --- a/context/context.go +++ b/context/context.go @@ -1129,6 +1129,8 @@ type ( // The structure contains struct tags for JSON, form, XML, YAML and TOML. // Look the `GetReferrer() Referrer` and `goreferrer` external package. Referrer struct { + // The raw refer(r)er URL. + Raw string `json:"raw" form:"raw" xml:"Raw" yaml:"Raw" toml:"Raw"` Type ReferrerType `json:"type" form:"referrer_type" xml:"Type" yaml:"Type" toml:"Type"` Label string `json:"label" form:"referrer_form" xml:"Label" yaml:"Label" toml:"Label"` URL string `json:"url" form:"referrer_url" xml:"URL" yaml:"URL" toml:"URL"` @@ -1147,6 +1149,11 @@ type ( ReferrerGoogleSearchType = goreferrer.GoogleSearchType ) +// String returns the raw ref url. +func (ref Referrer) String() string { + return ref.Raw +} + // Contains the available values of the goreferrer enums. const ( ReferrerInvalid ReferrerType = iota @@ -1187,6 +1194,7 @@ func (ctx *Context) GetReferrer() Referrer { if ref := goreferrer.DefaultRules.Parse(refURL); ref.Type > goreferrer.Invalid { return Referrer{ + Raw: refURL, Type: ReferrerType(ref.Type), Label: ref.Label, URL: ref.URL, diff --git a/middleware/accesslog/accesslog.go b/middleware/accesslog/accesslog.go index 4321629b..7182798d 100644 --- a/middleware/accesslog/accesslog.go +++ b/middleware/accesslog/accesslog.go @@ -257,7 +257,7 @@ func (ac *AccessLog) after(ctx *context.Context, lat time.Duration, method, path var fields []memstore.Entry if n := len(ac.Fields); n > 0 { - fields = make([]memstore.Entry, n) + fields = make([]memstore.Entry, 0, n) for _, extract := range ac.Fields { key, value := extract(ctx)