Skip to content

Commit

Permalink
utils/logger: support append user specified ID in log
Browse files Browse the repository at this point in the history
So that we could filter logs from the same juicefs instance. And use
"--log-id random" to generate random uuid as log id.

Signed-off-by: Eryu Guan <[email protected]>
  • Loading branch information
eryugey committed Sep 5, 2023
1 parent 773baf3 commit cbc7477
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 1 deletion.
4 changes: 4 additions & 0 deletions cmd/flags.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,10 @@ func globalFlags() []cli.Flag {
Usage: "set log level (trace, debug, info, warn, error, fatal, panic)",
Hidden: true,
},
&cli.StringFlag{
Name: "log-id",
Usage: "append the given log id in log, use \"random\" to use random uuid",
},
&cli.BoolFlag{
Name: "no-agent",
Usage: "disable pprof (:6060) agent",
Expand Down
12 changes: 12 additions & 0 deletions cmd/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import (
"syscall"

"github.com/erikdubbelboer/gspt"
"github.com/google/uuid"
"github.com/juicedata/juicefs/pkg/utils"
"github.com/juicedata/juicefs/pkg/version"
"github.com/pyroscope-io/client/pyroscope"
Expand Down Expand Up @@ -197,6 +198,9 @@ func reorderOptions(app *cli.App, args []string) []string {
newArgs = append(newArgs, option)
if hasValue {
i++
if i >= len(args) {
logger.Fatalf("option %s requires value", option)
}
newArgs = append(newArgs, args[i])
}
} else {
Expand Down Expand Up @@ -283,6 +287,14 @@ func setup(c *cli.Context, n int) {
undo()
}

logID := c.String("log-id")
if logID != "" {
if logID == "random" {
logID = uuid.New().String()
}
utils.SetLogID(logID + " ")
}

if !c.Bool("no-agent") {
go debugAgentOnce.Do(func() {
for port := 6060; port < 6100; port++ {
Expand Down
12 changes: 11 additions & 1 deletion pkg/utils/logger.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ type logHandle struct {
logrus.Logger

name string
logid string
lvl *logrus.Level
colorful bool
}
Expand All @@ -60,8 +61,9 @@ func (l *logHandle) Format(e *logrus.Entry) ([]byte, error) {
}
const timeFormat = "2006/01/02 15:04:05.000000"
timestamp := e.Time.Format(timeFormat)
str := fmt.Sprintf("%v %s[%d] <%v>: %v [%s:%d]",
str := fmt.Sprintf("%v %s%s[%d] <%v>: %v [%s:%d]",
timestamp,
l.logid,
l.name,
os.Getpid(),
lvlStr,
Expand Down Expand Up @@ -143,3 +145,11 @@ func SetOutput(w io.Writer) {
logger.SetOutput(w)
}
}

func SetLogID(id string) {
mu.Lock()
defer mu.Unlock()
for _, logger := range loggers {
logger.logid = id
}
}
3 changes: 3 additions & 0 deletions pkg/utils/logger_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ func TestLogger(t *testing.T) {
SetOutFile("") // invalid
SetOutFile(f.Name())
InitLoggers(true)
SetLogID("testid")

SetLogLevel(logrus.TraceLevel)
SetLogLevel(logrus.DebugLevel)
Expand All @@ -53,5 +54,7 @@ func TestLogger(t *testing.T) {
t.Fatalf("info/debug should not be logged: %s", s)
} else if !strings.Contains(s, "warn level") || !strings.Contains(s, "error level") {
t.Fatalf("warn/error should be logged: %s", s)
} else if !strings.Contains(s, "testid") {
t.Fatalf("logid \"testid\" should be logged: %s", s)
}
}

0 comments on commit cbc7477

Please sign in to comment.