Add http headers to trace log
This commit is contained in:
+5
-4
@@ -109,6 +109,7 @@ func levelFromString(l string) Level {
|
|||||||
|
|
||||||
// SetLogLevels sets the log levels for specific paths in the codebase.
|
// SetLogLevels sets the log levels for specific paths in the codebase.
|
||||||
func SetLogLevels(levels map[string]string) {
|
func SetLogLevels(levels map[string]string) {
|
||||||
|
logLevels = nil
|
||||||
for k, v := range levels {
|
for k, v := range levels {
|
||||||
logLevels = append(logLevels, levelPath{path: k, level: levelFromString(v)})
|
logLevels = append(logLevels, levelPath{path: k, level: levelFromString(v)})
|
||||||
}
|
}
|
||||||
@@ -158,7 +159,7 @@ func CurrentLevel() Level {
|
|||||||
|
|
||||||
// IsGreaterOrEqualTo returns true if the caller's current log level is equal or greater than the provided level.
|
// IsGreaterOrEqualTo returns true if the caller's current log level is equal or greater than the provided level.
|
||||||
func IsGreaterOrEqualTo(level Level) bool {
|
func IsGreaterOrEqualTo(level Level) bool {
|
||||||
return shouldLog(level)
|
return shouldLog(level, 2)
|
||||||
}
|
}
|
||||||
|
|
||||||
func Fatal(args ...interface{}) {
|
func Fatal(args ...interface{}) {
|
||||||
@@ -187,14 +188,14 @@ func Trace(args ...interface{}) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func log(level Level, args ...interface{}) {
|
func log(level Level, args ...interface{}) {
|
||||||
if !shouldLog(level) {
|
if !shouldLog(level, 3) {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
logger, msg := parseArgs(args)
|
logger, msg := parseArgs(args)
|
||||||
logger.Log(logrus.Level(level), msg)
|
logger.Log(logrus.Level(level), msg)
|
||||||
}
|
}
|
||||||
|
|
||||||
func shouldLog(requiredLevel Level) bool {
|
func shouldLog(requiredLevel Level, skip int) bool {
|
||||||
if currentLevel >= requiredLevel {
|
if currentLevel >= requiredLevel {
|
||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
@@ -202,7 +203,7 @@ func shouldLog(requiredLevel Level) bool {
|
|||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
|
|
||||||
_, file, _, ok := runtime.Caller(3)
|
_, file, _, ok := runtime.Caller(skip)
|
||||||
if !ok {
|
if !ok {
|
||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -150,6 +150,10 @@ var _ = Describe("Logger", func() {
|
|||||||
})
|
})
|
||||||
|
|
||||||
Describe("IsGreaterOrEqualTo", func() {
|
Describe("IsGreaterOrEqualTo", func() {
|
||||||
|
BeforeEach(func() {
|
||||||
|
SetLogLevels(nil)
|
||||||
|
})
|
||||||
|
|
||||||
It("returns false if log level is below provided level", func() {
|
It("returns false if log level is below provided level", func() {
|
||||||
SetLevel(LevelError)
|
SetLevel(LevelError)
|
||||||
Expect(IsGreaterOrEqualTo(LevelWarn)).To(BeFalse())
|
Expect(IsGreaterOrEqualTo(LevelWarn)).To(BeFalse())
|
||||||
|
|||||||
@@ -3,6 +3,7 @@ package server
|
|||||||
import (
|
import (
|
||||||
"cmp"
|
"cmp"
|
||||||
"context"
|
"context"
|
||||||
|
"encoding/json"
|
||||||
"errors"
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
"io/fs"
|
"io/fs"
|
||||||
@@ -42,7 +43,10 @@ func requestLogger(next http.Handler) http.Handler {
|
|||||||
"httpStatus", ww.Status(),
|
"httpStatus", ww.Status(),
|
||||||
"responseSize", ww.BytesWritten(),
|
"responseSize", ww.BytesWritten(),
|
||||||
}
|
}
|
||||||
if log.IsGreaterOrEqualTo(log.LevelDebug) {
|
if log.IsGreaterOrEqualTo(log.LevelTrace) {
|
||||||
|
headers, _ := json.Marshal(r.Header)
|
||||||
|
logArgs = append(logArgs, "header", string(headers))
|
||||||
|
} else if log.IsGreaterOrEqualTo(log.LevelDebug) {
|
||||||
logArgs = append(logArgs, "userAgent", r.UserAgent())
|
logArgs = append(logArgs, "userAgent", r.UserAgent())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user