lib/logger: add WARN level for logging expected errors such as invalid user queries

This commit is contained in:
Aliaksandr Valialkin 2020-04-15 20:50:12 +03:00
parent 30b401ebbf
commit 071fdf5518
3 changed files with 25 additions and 8 deletions

View file

@ -233,7 +233,7 @@ func RequestHandler(w http.ResponseWriter, r *http.Request) bool {
}
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")
statusCode := http.StatusUnprocessableEntity

View file

@ -146,7 +146,7 @@ func gzipHandler(rh RequestHandler) http.HandlerFunc {
handlerWrapper(w, r, rh)
if zrw, ok := w.(*gzipResponseWriter); ok {
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
func (zrw *gzipResponseWriter) Flush() {
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) {
logger.Errorf("gzipResponseWriter.Flush (gzip): %s", err)
logger.Warnf("gzipResponseWriter.Flush (gzip): %s", err)
}
if fw, ok := zrw.ResponseWriter.(http.Flusher); ok {
fw.Flush()
@ -419,7 +419,7 @@ var (
// Errorf writes formatted error message to w and to logger.
func Errorf(w http.ResponseWriter, format string, args ...interface{}) {
errStr := fmt.Sprintf(format, args...)
logger.ErrorfSkipframes(1, "%s", errStr)
logger.WarnfSkipframes(1, "%s", errStr)
// Extract statusCode from args
statusCode := http.StatusBadRequest

View file

@ -18,7 +18,7 @@ import (
)
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")
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() {
switch *loggerLevel {
case "INFO", "ERROR", "FATAL", "PANIC":
case "INFO", "WARN", "ERROR", "FATAL", "PANIC":
default:
// 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...)
}
// Warnf logs warn message.
func Warnf(format string, args ...interface{}) {
logLevel("WARN", format, args...)
}
// Errorf logs error message.
func Errorf(format string, args ...interface{}) {
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.
func ErrorfSkipframes(skipframes int, format string, args ...interface{}) {
logLevelSkipframes(skipframes, "ERROR", format, args...)
@ -185,6 +195,13 @@ var mu sync.Mutex
func shouldSkipLog(level string) bool {
switch *loggerLevel {
case "WARN":
switch level {
case "WARN", "ERROR", "FATAL", "PANIC":
return false
default:
return true
}
case "ERROR":
switch level {
case "ERROR", "FATAL", "PANIC":