Skip to content

Commit

Permalink
Remove goroutine ID field from logger
Browse files Browse the repository at this point in the history
Adding the goroutine ID as a log field required copying the calling
goroutine stack into a buffer and parsing the ID from it. This was done
for each log action which is very inefficient and does not provide much
value as we are already using a field for the iteration ID. Therefore
our decision is to remove it completely.
  • Loading branch information
ka3de committed Jul 18, 2023
1 parent 1d75cd5 commit 44fce67
Showing 1 changed file with 2 additions and 15 deletions.
17 changes: 2 additions & 15 deletions log/logger.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import (
"io/ioutil"
"regexp"
"runtime"
"strconv"
"strings"
"sync"
"time"
Expand Down Expand Up @@ -98,9 +97,8 @@ func (l *Logger) Logf(level logrus.Level, category string, msg string, args ...a
return
}
fields := logrus.Fields{
"category": category,
"elapsed": fmt.Sprintf("%d ms", elapsed),
"goroutine": goRoutineID(),
"category": category,
"elapsed": fmt.Sprintf("%d ms", elapsed),
}
if l.iterID != "" && l.GetLevel() > logrus.InfoLevel {
fields["iteration_id"] = l.iterID
Expand Down Expand Up @@ -176,17 +174,6 @@ func (l *Logger) SetCategoryFilter(filter string) (err error) {
return nil
}

func goRoutineID() int {
var buf [64]byte
n := runtime.Stack(buf[:], false)
idField := strings.Fields(strings.TrimPrefix(string(buf[:n]), "goroutine "))[0]
id, err := strconv.Atoi(idField)
if err != nil {
panic(fmt.Sprintf("internal error while getting goroutine ID: %v", err))
}
return id
}

type consoleLogFormatter struct {
logrus.Formatter
}
Expand Down

0 comments on commit 44fce67

Please sign in to comment.