Skip to content

Implement log deduplication to minimize repetitive messages #525

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 3 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,7 @@
# Data generated by the app
/cmd/def/data
/cmd/svc/data
/data

# Built binaries
ddns-updater
Binary file added ddns-updater
Binary file not shown.
86 changes: 82 additions & 4 deletions pkg/logging/logging.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
"path/filepath"
"runtime"
"strings"
"sync"
)

const (
Expand All @@ -29,16 +30,32 @@
errorLogger *log.Logger
}

// LogEntry tracks the last logged message and count for deduplication
type LogEntry struct {
message string
count int
}

// LogMemory maintains memory of recent log messages to prevent duplicates
type LogMemory struct {
mu sync.Mutex
entries map[string]*LogEntry // key is log level + message content
}

var (
consoleLogger *Logger
fileLogger *Logger
logFile *os.File
logMemory *LogMemory
)

func init() {
consoleLogger = createLogger(os.Stdout, os.Stderr)
openLogFile()
fileLogger = createLogger(logFile, logFile)
logMemory = &LogMemory{
entries: make(map[string]*LogEntry),
}
}

func openLogFile() {
Expand All @@ -60,17 +77,78 @@
}
}

// shouldLog checks if a message should be logged based on deduplication logic
// Returns true if the message should be logged, false if it's a duplicate
func (lm *LogMemory) shouldLog(level, message string) bool {
lm.mu.Lock()
defer lm.mu.Unlock()

key := level + ":" + message
entry, exists := lm.entries[key]

if !exists {
// First time seeing this message, log it
lm.entries[key] = &LogEntry{
message: message,
count: 1,
}
return true
}

// Message exists, increment count but don't log duplicate
entry.count++
return false
}

// ClearDuplicateMemory clears the duplicate message memory and logs counts for any repeated messages
// This can be called periodically to report on suppressed duplicate messages
func ClearDuplicateMemory() {
logMemory.mu.Lock()
defer logMemory.mu.Unlock()

for key, entry := range logMemory.entries {
if entry.count > 1 {
parts := strings.SplitN(key, ":", 2)
if len(parts) == 2 {
level := parts[0]
duplicateMsg := fmt.Sprintf("(message repeated %d times): %s", entry.count-1, entry.message)

// Log the duplicate summary without going through deduplication
if level == INFO {

Check failure on line 117 in pkg/logging/logging.go

View workflow job for this annotation

GitHub Actions / Lint Golang

QF1003: could use tagged switch on level (staticcheck)
consoleLogger.infoLogger.Printf(INFOC+trace()+"message:"+duplicateMsg)

Check failure on line 118 in pkg/logging/logging.go

View workflow job for this annotation

GitHub Actions / Lint Golang

printf: non-constant format string in call to (*log.Logger).Printf (govet)
fileLogger.infoLogger.Printf(INFO+trace()+"message:"+duplicateMsg)

Check failure on line 119 in pkg/logging/logging.go

View workflow job for this annotation

GitHub Actions / Lint Golang

printf: non-constant format string in call to (*log.Logger).Printf (govet)
} else if level == ERROR {
consoleLogger.errorLogger.Printf(ERRORC+trace()+"message:"+duplicateMsg)

Check failure on line 121 in pkg/logging/logging.go

View workflow job for this annotation

GitHub Actions / Lint Golang

printf: non-constant format string in call to (*log.Logger).Printf (govet)
fileLogger.errorLogger.Printf(ERROR+trace()+"message:"+duplicateMsg)
}
}
}
}

// Clear the memory
logMemory.entries = make(map[string]*LogEntry)
}

func Infof(msg string, args ...interface{}) {
consoleLogger.infoLogger.Printf(INFOC+trace()+"message:"+msg, args...)
fileLogger.infoLogger.Printf(INFO+trace()+"message:"+msg, args...)
formattedMsg := fmt.Sprintf(msg, args...)

if logMemory.shouldLog(INFO, formattedMsg) {
consoleLogger.infoLogger.Printf(INFOC+trace()+"message:"+formattedMsg)
fileLogger.infoLogger.Printf(INFO+trace()+"message:"+formattedMsg)
}
}

func Errorf(msg string, args ...interface{}) {
consoleLogger.errorLogger.Printf(ERRORC+trace()+"message:"+msg, args...)
fileLogger.errorLogger.Printf(ERROR+trace()+"message:"+msg, args...)
formattedMsg := fmt.Sprintf(msg, args...)

if logMemory.shouldLog(ERROR, formattedMsg) {
consoleLogger.errorLogger.Printf(ERRORC+trace()+"message:"+formattedMsg)
fileLogger.errorLogger.Printf(ERROR+trace()+"message:"+formattedMsg)
}
}

func Fatalf(msg string, args ...interface{}) {
// Fatal messages should always be logged, no deduplication
consoleLogger.errorLogger.Fatalf(FATALC+trace()+"message:"+msg, args...)
fileLogger.errorLogger.Fatalf(FATAL+trace()+"message:"+msg, args...)
}
Expand Down
Loading