Ultimate Access log filter
This commit is contained in:
parent
f99363674b
commit
8d468925d3
24 changed files with 1722 additions and 683 deletions
|
@ -14,56 +14,69 @@ const (
|
|||
defaultValue = "-"
|
||||
)
|
||||
|
||||
// CommonLogFormatter provides formatting in the Traefik common log format
|
||||
// CommonLogFormatter provides formatting in the Træfik common log format
|
||||
type CommonLogFormatter struct{}
|
||||
|
||||
//Format formats the log entry in the Traefik common log format
|
||||
// Format formats the log entry in the Træfik common log format
|
||||
func (f *CommonLogFormatter) Format(entry *logrus.Entry) ([]byte, error) {
|
||||
b := &bytes.Buffer{}
|
||||
|
||||
timestamp := entry.Data[StartUTC].(time.Time).Format(commonLogTimeFormat)
|
||||
elapsedMillis := entry.Data[Duration].(time.Duration).Nanoseconds() / 1000000
|
||||
var timestamp = defaultValue
|
||||
if v, ok := entry.Data[StartUTC]; ok {
|
||||
timestamp = v.(time.Time).Format(commonLogTimeFormat)
|
||||
}
|
||||
|
||||
var elapsedMillis int64
|
||||
if v, ok := entry.Data[Duration]; ok {
|
||||
elapsedMillis = v.(time.Duration).Nanoseconds() / 1000000
|
||||
}
|
||||
|
||||
_, err := fmt.Fprintf(b, "%s - %s [%s] \"%s %s %s\" %v %v %s %s %v %s %s %dms\n",
|
||||
entry.Data[ClientHost],
|
||||
entry.Data[ClientUsername],
|
||||
toLog(entry.Data, ClientHost, defaultValue, false),
|
||||
toLog(entry.Data, ClientUsername, defaultValue, false),
|
||||
timestamp,
|
||||
entry.Data[RequestMethod],
|
||||
entry.Data[RequestPath],
|
||||
entry.Data[RequestProtocol],
|
||||
toLog(entry.Data[OriginStatus], defaultValue),
|
||||
toLog(entry.Data[OriginContentSize], defaultValue),
|
||||
toLog(entry.Data["request_Referer"], `"-"`),
|
||||
toLog(entry.Data["request_User-Agent"], `"-"`),
|
||||
toLog(entry.Data[RequestCount], defaultValue),
|
||||
toLog(entry.Data[FrontendName], defaultValue),
|
||||
toLog(entry.Data[BackendURL], defaultValue),
|
||||
toLog(entry.Data, RequestMethod, defaultValue, false),
|
||||
toLog(entry.Data, RequestPath, defaultValue, false),
|
||||
toLog(entry.Data, RequestProtocol, defaultValue, false),
|
||||
toLog(entry.Data, OriginStatus, defaultValue, true),
|
||||
toLog(entry.Data, OriginContentSize, defaultValue, true),
|
||||
toLog(entry.Data, "request_Referer", `"-"`, true),
|
||||
toLog(entry.Data, "request_User-Agent", `"-"`, true),
|
||||
toLog(entry.Data, RequestCount, defaultValue, true),
|
||||
toLog(entry.Data, FrontendName, defaultValue, true),
|
||||
toLog(entry.Data, BackendURL, defaultValue, true),
|
||||
elapsedMillis)
|
||||
|
||||
return b.Bytes(), err
|
||||
}
|
||||
|
||||
func toLog(v interface{}, defaultValue string) interface{} {
|
||||
if v == nil {
|
||||
return defaultValue
|
||||
}
|
||||
|
||||
switch s := v.(type) {
|
||||
case string:
|
||||
return quoted(s, defaultValue)
|
||||
|
||||
case fmt.Stringer:
|
||||
return quoted(s.String(), defaultValue)
|
||||
|
||||
default:
|
||||
return v
|
||||
func toLog(fields logrus.Fields, key string, defaultValue string, quoted bool) interface{} {
|
||||
if v, ok := fields[key]; ok {
|
||||
if v == nil {
|
||||
return defaultValue
|
||||
}
|
||||
|
||||
switch s := v.(type) {
|
||||
case string:
|
||||
return toLogEntry(s, defaultValue, quoted)
|
||||
|
||||
case fmt.Stringer:
|
||||
return toLogEntry(s.String(), defaultValue, quoted)
|
||||
|
||||
default:
|
||||
return v
|
||||
}
|
||||
}
|
||||
return defaultValue
|
||||
|
||||
}
|
||||
|
||||
func quoted(s string, defaultValue string) string {
|
||||
func toLogEntry(s string, defaultValue string, quote bool) string {
|
||||
if len(s) == 0 {
|
||||
return defaultValue
|
||||
}
|
||||
return `"` + s + `"`
|
||||
|
||||
if quote {
|
||||
return `"` + s + `"`
|
||||
}
|
||||
return s
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue