Skip to content

Commit

Permalink
feat(log): add function name and line number in log message
Browse files Browse the repository at this point in the history
ref: longhorn/longhorn 5509

Signed-off-by: Jack Lin <jack.lin@suse.com>
  • Loading branch information
ChanYiLin authored and David Ko committed Oct 23, 2023
1 parent a16d9e8 commit f80354e
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 0 deletions.
13 changes: 13 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
package main

import (
"fmt"
"os"
"path"
"runtime"

"github.com/sirupsen/logrus"
"github.com/urfave/cli"
Expand All @@ -25,6 +28,16 @@ func main() {
meta.GitCommit = GitCommit
meta.BuildDate = BuildDate

logrus.SetReportCaller(true)
logrus.SetFormatter(&logrus.TextFormatter{
CallerPrettyfier: func(f *runtime.Frame) (function string, file string) {
fileName := fmt.Sprintf("%s:%d", path.Base(f.File), f.Line)
funcName := path.Base(f.Function)
return funcName, fileName
},
FullTimestamp: true,
})

a.Before = func(c *cli.Context) error {
if c.GlobalBool("debug") {
logrus.SetLevel(logrus.DebugLevel)
Expand Down
10 changes: 10 additions & 0 deletions pkg/util/log.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,11 @@ import (
"bufio"
"bytes"
"errors"
"fmt"
"os"
"path"
"path/filepath"
"runtime"

"github.com/sirupsen/logrus"
)
Expand Down Expand Up @@ -58,9 +61,16 @@ func SetUpLogger(logsDir string) error {
return err
}
logrus.Infof("Storing process logs at path: %v", logsDir)
logrus.SetReportCaller(true)

logrus.SetFormatter(LonghornFormatter{
TextFormatter: &logrus.TextFormatter{
DisableColors: false,
CallerPrettyfier: func(f *runtime.Frame) (function string, file string) {
fileName := fmt.Sprintf("%s:%d", path.Base(f.File), f.Line)
funcName := path.Base(f.Function)
return funcName, fileName
},
},
LogsDir: logsDir,
})
Expand Down

0 comments on commit f80354e

Please sign in to comment.