From 31d04fb5df11ce04868b424be873cd9e75aeaab0 Mon Sep 17 00:00:00 2001 From: Aliaksandr Valialkin Date: Tue, 24 Dec 2019 12:21:39 +0200 Subject: [PATCH] Revert "lib/logger: prevent from blocking when log output isn't consumed in timely manner" This reverts commit e3c462f08a8588f544c263cfd9b26baf6a3843a2. Reason to revert: this leaves incomplete logs on app shutdown. --- lib/logger/logger.go | 23 ++++++----------------- 1 file changed, 6 insertions(+), 17 deletions(-) diff --git a/lib/logger/logger.go b/lib/logger/logger.go index 8f820b622..f886427f0 100644 --- a/lib/logger/logger.go +++ b/lib/logger/logger.go @@ -8,6 +8,7 @@ import ( "os" "runtime" "strings" + "sync" "sync/atomic" "time" @@ -29,7 +30,6 @@ func Init() { validateLoggerLevel() validateLoggerFormat() go errorsLoggedCleaner() - go logMessageWriter() logAllFlags() } @@ -137,13 +137,10 @@ func logMessage(level, msg string, skipframes int) { logMsg = fmt.Sprintf("%s\t%s\t%s:%d\t%s\n", timestamp, levelLowercase, file, line, msg) } - select { - case logMessageCh <- logMsg: - default: - // Writing to log can stuck if the log output isn't consumed in timely manner. - // Handle this case via `vm_log_messages_dropped_total` - logMessagesDropped.Inc() - } + // Serialize writes to log. + mu.Lock() + fmt.Fprint(os.Stderr, logMsg) + mu.Unlock() // Increment vm_log_messages_total location := fmt.Sprintf("%s:%d", file, line) @@ -158,15 +155,7 @@ func logMessage(level, msg string, skipframes int) { } } -var logMessagesDropped = metrics.NewCounter(`vm_log_messages_dropped_total`) - -func logMessageWriter() { - for msg := range logMessageCh { - fmt.Fprint(os.Stderr, msg) - } -} - -var logMessageCh = make(chan string, 100) +var mu sync.Mutex func shouldSkipLog(level string) bool { switch *loggerLevel {