mirror of
https://github.com/VictoriaMetrics/VictoriaMetrics.git
synced 2024-11-21 14:44:00 +00:00
lib/logger: add WARN level for logging expected errors such as invalid user queries
This commit is contained in:
parent
30b401ebbf
commit
071fdf5518
3 changed files with 25 additions and 8 deletions
|
@ -233,7 +233,7 @@ func RequestHandler(w http.ResponseWriter, r *http.Request) bool {
|
||||||
}
|
}
|
||||||
|
|
||||||
func sendPrometheusError(w http.ResponseWriter, r *http.Request, err error) {
|
func sendPrometheusError(w http.ResponseWriter, r *http.Request, err error) {
|
||||||
logger.Errorf("error in %q: %s", r.RequestURI, err)
|
logger.Warnf("error in %q: %s", r.RequestURI, err)
|
||||||
|
|
||||||
w.Header().Set("Content-Type", "application/json")
|
w.Header().Set("Content-Type", "application/json")
|
||||||
statusCode := http.StatusUnprocessableEntity
|
statusCode := http.StatusUnprocessableEntity
|
||||||
|
|
|
@ -146,7 +146,7 @@ func gzipHandler(rh RequestHandler) http.HandlerFunc {
|
||||||
handlerWrapper(w, r, rh)
|
handlerWrapper(w, r, rh)
|
||||||
if zrw, ok := w.(*gzipResponseWriter); ok {
|
if zrw, ok := w.(*gzipResponseWriter); ok {
|
||||||
if err := zrw.Close(); err != nil && !isTrivialNetworkError(err) {
|
if err := zrw.Close(); err != nil && !isTrivialNetworkError(err) {
|
||||||
logger.Errorf("gzipResponseWriter.Close: %s", err)
|
logger.Warnf("gzipResponseWriter.Close: %s", err)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -329,10 +329,10 @@ func (zrw *gzipResponseWriter) WriteHeader(statusCode int) {
|
||||||
// Implements http.Flusher
|
// Implements http.Flusher
|
||||||
func (zrw *gzipResponseWriter) Flush() {
|
func (zrw *gzipResponseWriter) Flush() {
|
||||||
if err := zrw.bw.Flush(); err != nil && !isTrivialNetworkError(err) {
|
if err := zrw.bw.Flush(); err != nil && !isTrivialNetworkError(err) {
|
||||||
logger.Errorf("gzipResponseWriter.Flush (buffer): %s", err)
|
logger.Warnf("gzipResponseWriter.Flush (buffer): %s", err)
|
||||||
}
|
}
|
||||||
if err := zrw.zw.Flush(); err != nil && !isTrivialNetworkError(err) {
|
if err := zrw.zw.Flush(); err != nil && !isTrivialNetworkError(err) {
|
||||||
logger.Errorf("gzipResponseWriter.Flush (gzip): %s", err)
|
logger.Warnf("gzipResponseWriter.Flush (gzip): %s", err)
|
||||||
}
|
}
|
||||||
if fw, ok := zrw.ResponseWriter.(http.Flusher); ok {
|
if fw, ok := zrw.ResponseWriter.(http.Flusher); ok {
|
||||||
fw.Flush()
|
fw.Flush()
|
||||||
|
@ -419,7 +419,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.ErrorfSkipframes(1, "%s", errStr)
|
logger.WarnfSkipframes(1, "%s", errStr)
|
||||||
|
|
||||||
// Extract statusCode from args
|
// Extract statusCode from args
|
||||||
statusCode := http.StatusBadRequest
|
statusCode := http.StatusBadRequest
|
||||||
|
|
|
@ -18,7 +18,7 @@ import (
|
||||||
)
|
)
|
||||||
|
|
||||||
var (
|
var (
|
||||||
loggerLevel = flag.String("loggerLevel", "INFO", "Minimum level of errors to log. Possible values: INFO, ERROR, FATAL, PANIC")
|
loggerLevel = flag.String("loggerLevel", "INFO", "Minimum level of errors to log. Possible values: INFO, WRAN, ERROR, FATAL, PANIC")
|
||||||
loggerFormat = flag.String("loggerFormat", "default", "Format for logs. Possible values: default, json")
|
loggerFormat = flag.String("loggerFormat", "default", "Format for logs. Possible values: default, json")
|
||||||
loggerOutput = flag.String("loggerOutput", "stderr", "Output for the logs. Supported values: stderr, stdout")
|
loggerOutput = flag.String("loggerOutput", "stderr", "Output for the logs. Supported values: stderr, stdout")
|
||||||
)
|
)
|
||||||
|
@ -51,10 +51,10 @@ var output io.Writer = os.Stderr
|
||||||
|
|
||||||
func validateLoggerLevel() {
|
func validateLoggerLevel() {
|
||||||
switch *loggerLevel {
|
switch *loggerLevel {
|
||||||
case "INFO", "ERROR", "FATAL", "PANIC":
|
case "INFO", "WARN", "ERROR", "FATAL", "PANIC":
|
||||||
default:
|
default:
|
||||||
// We cannot use logger.Panicf here, since the logger isn't initialized yet.
|
// We cannot use logger.Panicf here, since the logger isn't initialized yet.
|
||||||
panic(fmt.Errorf("FATAL: unsupported `-loggerLevel` value: %q; supported values are: INFO, ERROR, FATAL, PANIC", *loggerLevel))
|
panic(fmt.Errorf("FATAL: unsupported `-loggerLevel` value: %q; supported values are: INFO, WARN, ERROR, FATAL, PANIC", *loggerLevel))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -79,11 +79,21 @@ func Infof(format string, args ...interface{}) {
|
||||||
logLevel("INFO", format, args...)
|
logLevel("INFO", format, args...)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Warnf logs warn message.
|
||||||
|
func Warnf(format string, args ...interface{}) {
|
||||||
|
logLevel("WARN", format, args...)
|
||||||
|
}
|
||||||
|
|
||||||
// Errorf logs error message.
|
// Errorf logs error message.
|
||||||
func Errorf(format string, args ...interface{}) {
|
func Errorf(format string, args ...interface{}) {
|
||||||
logLevel("ERROR", format, args...)
|
logLevel("ERROR", format, args...)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// WarnfSkipframes logs warn message and skips the given number of frames for the caller.
|
||||||
|
func WarnfSkipframes(skipframes int, format string, args ...interface{}) {
|
||||||
|
logLevelSkipframes(skipframes, "WARN", format, args...)
|
||||||
|
}
|
||||||
|
|
||||||
// ErrorfSkipframes logs error message and skips the given number of frames for the caller.
|
// ErrorfSkipframes logs error message and skips the given number of frames for the caller.
|
||||||
func ErrorfSkipframes(skipframes int, format string, args ...interface{}) {
|
func ErrorfSkipframes(skipframes int, format string, args ...interface{}) {
|
||||||
logLevelSkipframes(skipframes, "ERROR", format, args...)
|
logLevelSkipframes(skipframes, "ERROR", format, args...)
|
||||||
|
@ -185,6 +195,13 @@ var mu sync.Mutex
|
||||||
|
|
||||||
func shouldSkipLog(level string) bool {
|
func shouldSkipLog(level string) bool {
|
||||||
switch *loggerLevel {
|
switch *loggerLevel {
|
||||||
|
case "WARN":
|
||||||
|
switch level {
|
||||||
|
case "WARN", "ERROR", "FATAL", "PANIC":
|
||||||
|
return false
|
||||||
|
default:
|
||||||
|
return true
|
||||||
|
}
|
||||||
case "ERROR":
|
case "ERROR":
|
||||||
switch level {
|
switch level {
|
||||||
case "ERROR", "FATAL", "PANIC":
|
case "ERROR", "FATAL", "PANIC":
|
||||||
|
|
Loading…
Reference in a new issue