mirror of
https://github.com/VictoriaMetrics/VictoriaMetrics.git
synced 2025-03-11 15:34:56 +00:00
lib/httpserver: log the caller of httpserver.Errorf
Previously log message contained `httpserver.Errorf`, not it contains the caller of `httpserver.Errorf`, which is more useful.
This commit is contained in:
parent
4d70a81e18
commit
9f595cb2b1
2 changed files with 19 additions and 13 deletions
|
@ -365,7 +365,7 @@ var (
|
||||||
// Errorf writes formatted error message to w and to logger.
|
// Errorf writes formatted error message to w and to logger.
|
||||||
func Errorf(w http.ResponseWriter, format string, args ...interface{}) {
|
func Errorf(w http.ResponseWriter, format string, args ...interface{}) {
|
||||||
errStr := fmt.Sprintf(format, args...)
|
errStr := fmt.Sprintf(format, args...)
|
||||||
logger.Errorf("%s", errStr)
|
logger.ErrorfSkipframes(1, "%s", errStr)
|
||||||
|
|
||||||
// Extract statusCode from args
|
// Extract statusCode from args
|
||||||
statusCode := http.StatusBadRequest
|
statusCode := http.StatusBadRequest
|
||||||
|
|
|
@ -68,6 +68,11 @@ func Errorf(format string, args ...interface{}) {
|
||||||
logLevel("ERROR", format, args...)
|
logLevel("ERROR", format, args...)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// ErrorfSkipframes logs error message and skips the given number of frames for the caller.
|
||||||
|
func ErrorfSkipframes(skipframes int, format string, args ...interface{}) {
|
||||||
|
logLevelSkipframes(skipframes, "ERROR", format, args...)
|
||||||
|
}
|
||||||
|
|
||||||
// Fatalf logs fatal message and terminates the app.
|
// Fatalf logs fatal message and terminates the app.
|
||||||
func Fatalf(format string, args ...interface{}) {
|
func Fatalf(format string, args ...interface{}) {
|
||||||
logLevel("FATAL", format, args...)
|
logLevel("FATAL", format, args...)
|
||||||
|
@ -79,19 +84,15 @@ func Panicf(format string, args ...interface{}) {
|
||||||
}
|
}
|
||||||
|
|
||||||
func logLevel(level, format string, args ...interface{}) {
|
func logLevel(level, format string, args ...interface{}) {
|
||||||
|
logLevelSkipframes(0, level, format, args...)
|
||||||
|
}
|
||||||
|
|
||||||
|
func logLevelSkipframes(skipframes int, level, format string, args ...interface{}) {
|
||||||
if shouldSkipLog(level) {
|
if shouldSkipLog(level) {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
// rate limit ERROR log messages
|
|
||||||
if level == "ERROR" {
|
|
||||||
if n := atomic.AddUint64(&errorsLogged, 1); n > 10 {
|
|
||||||
return
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
msg := fmt.Sprintf(format, args...)
|
msg := fmt.Sprintf(format, args...)
|
||||||
logMessage(level, msg, 3)
|
logMessage(level, msg, 3+skipframes)
|
||||||
}
|
}
|
||||||
|
|
||||||
func errorsLoggedCleaner() {
|
func errorsLoggedCleaner() {
|
||||||
|
@ -107,13 +108,18 @@ type logWriter struct {
|
||||||
}
|
}
|
||||||
|
|
||||||
func (lw *logWriter) Write(p []byte) (int, error) {
|
func (lw *logWriter) Write(p []byte) (int, error) {
|
||||||
if !shouldSkipLog("ERROR") {
|
logLevelSkipframes(2, "ERROR", "%s", p)
|
||||||
logMessage("ERROR", string(p), 4)
|
|
||||||
}
|
|
||||||
return len(p), nil
|
return len(p), nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func logMessage(level, msg string, skipframes int) {
|
func logMessage(level, msg string, skipframes int) {
|
||||||
|
// rate limit ERROR log messages
|
||||||
|
if level == "ERROR" {
|
||||||
|
if n := atomic.AddUint64(&errorsLogged, 1); n > 10 {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
timestamp := time.Now().UTC().Format("2006-01-02T15:04:05.000Z")
|
timestamp := time.Now().UTC().Format("2006-01-02T15:04:05.000Z")
|
||||||
levelLowercase := strings.ToLower(level)
|
levelLowercase := strings.ToLower(level)
|
||||||
_, file, line, ok := runtime.Caller(skipframes)
|
_, file, line, ok := runtime.Caller(skipframes)
|
||||||
|
|
Loading…
Reference in a new issue