2021-04-18 15:41:13 -04:00
|
|
|
// Copyright (c) 2015-2021 MinIO, Inc.
|
|
|
|
//
|
|
|
|
// This file is part of MinIO Object Storage stack
|
|
|
|
//
|
|
|
|
// This program is free software: you can redistribute it and/or modify
|
|
|
|
// it under the terms of the GNU Affero General Public License as published by
|
|
|
|
// the Free Software Foundation, either version 3 of the License, or
|
|
|
|
// (at your option) any later version.
|
|
|
|
//
|
|
|
|
// This program is distributed in the hope that it will be useful
|
|
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
// GNU Affero General Public License for more details.
|
|
|
|
//
|
|
|
|
// You should have received a copy of the GNU Affero General Public License
|
|
|
|
// along with this program. If not, see <http://www.gnu.org/licenses/>.
|
2018-05-09 18:59:45 -04:00
|
|
|
|
|
|
|
package logger
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2020-10-30 17:55:50 -04:00
|
|
|
"errors"
|
|
|
|
"net/http"
|
2018-05-09 18:59:45 -04:00
|
|
|
"sync"
|
|
|
|
"time"
|
|
|
|
)
|
|
|
|
|
|
|
|
// Holds a map of recently logged errors.
|
|
|
|
type logOnceType struct {
|
|
|
|
IDMap map[interface{}]error
|
|
|
|
sync.Mutex
|
|
|
|
}
|
|
|
|
|
2022-05-12 10:20:58 -04:00
|
|
|
func (l *logOnceType) logOnceConsoleIf(ctx context.Context, err error, id interface{}, errKind ...interface{}) {
|
|
|
|
if err == nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
l.Lock()
|
|
|
|
shouldLog := false
|
|
|
|
prevErr := l.IDMap[id]
|
|
|
|
if prevErr == nil {
|
|
|
|
l.IDMap[id] = err
|
|
|
|
shouldLog = true
|
|
|
|
} else if prevErr.Error() != err.Error() {
|
|
|
|
l.IDMap[id] = err
|
|
|
|
shouldLog = true
|
|
|
|
}
|
|
|
|
l.Unlock()
|
|
|
|
|
|
|
|
if shouldLog {
|
|
|
|
consoleLogIf(ctx, err, errKind...)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-05-09 18:59:45 -04:00
|
|
|
// One log message per error.
|
2019-10-11 21:50:54 -04:00
|
|
|
func (l *logOnceType) logOnceIf(ctx context.Context, err error, id interface{}, errKind ...interface{}) {
|
2018-05-09 18:59:45 -04:00
|
|
|
if err == nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
l.Lock()
|
|
|
|
shouldLog := false
|
|
|
|
prevErr := l.IDMap[id]
|
|
|
|
if prevErr == nil {
|
|
|
|
l.IDMap[id] = err
|
|
|
|
shouldLog = true
|
2021-11-16 12:28:29 -05:00
|
|
|
} else if prevErr.Error() != err.Error() {
|
|
|
|
l.IDMap[id] = err
|
|
|
|
shouldLog = true
|
2018-05-09 18:59:45 -04:00
|
|
|
}
|
|
|
|
l.Unlock()
|
|
|
|
|
|
|
|
if shouldLog {
|
2019-10-11 21:50:54 -04:00
|
|
|
LogIf(ctx, err, errKind...)
|
2018-05-09 18:59:45 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Cleanup the map every 30 minutes so that the log message is printed again for the user to notice.
|
|
|
|
func (l *logOnceType) cleanupRoutine() {
|
|
|
|
for {
|
2019-02-13 07:59:36 -05:00
|
|
|
l.Lock()
|
|
|
|
l.IDMap = make(map[interface{}]error)
|
|
|
|
l.Unlock()
|
|
|
|
|
|
|
|
time.Sleep(30 * time.Minute)
|
2018-05-09 18:59:45 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Returns logOnceType
|
|
|
|
func newLogOnceType() *logOnceType {
|
|
|
|
l := &logOnceType{IDMap: make(map[interface{}]error)}
|
|
|
|
go l.cleanupRoutine()
|
|
|
|
return l
|
|
|
|
}
|
|
|
|
|
|
|
|
var logOnce = newLogOnceType()
|
|
|
|
|
|
|
|
// LogOnceIf - Logs notification errors - once per error.
|
|
|
|
// id is a unique identifier for related log messages, refer to cmd/notification.go
|
|
|
|
// on how it is used.
|
2019-10-11 21:50:54 -04:00
|
|
|
func LogOnceIf(ctx context.Context, err error, id interface{}, errKind ...interface{}) {
|
2020-10-30 17:55:50 -04:00
|
|
|
if err == nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2020-11-02 10:43:11 -05:00
|
|
|
if errors.Is(err, context.Canceled) {
|
2020-10-30 17:55:50 -04:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2020-11-02 10:43:11 -05:00
|
|
|
if err.Error() == http.ErrServerClosed.Error() || err.Error() == "disk not found" {
|
|
|
|
return
|
2020-10-30 17:55:50 -04:00
|
|
|
}
|
|
|
|
|
2019-10-11 21:50:54 -04:00
|
|
|
logOnce.logOnceIf(ctx, err, id, errKind...)
|
2018-05-09 18:59:45 -04:00
|
|
|
}
|
2022-05-12 10:20:58 -04:00
|
|
|
|
|
|
|
// LogOnceConsoleIf - similar to LogOnceIf but exclusively only logs to console target.
|
|
|
|
func LogOnceConsoleIf(ctx context.Context, err error, id interface{}, errKind ...interface{}) {
|
|
|
|
if err == nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if errors.Is(err, context.Canceled) {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if err.Error() == http.ErrServerClosed.Error() || err.Error() == "disk not found" {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
logOnce.logOnceConsoleIf(ctx, err, id, errKind...)
|
|
|
|
}
|