From ead1b7c4950da844d700371e7678d7ec1a71224a Mon Sep 17 00:00:00 2001 From: mahiro72 Date: Thu, 6 Jun 2024 02:57:57 +0900 Subject: [PATCH] mv: defaultLogger to log pkg --- pkg/knuu/knuu.go | 33 ++------------------------------- pkg/log/logger.go | 37 +++++++++++++++++++++++++++++++++++++ 2 files changed, 39 insertions(+), 31 deletions(-) create mode 100644 pkg/log/logger.go diff --git a/pkg/knuu/knuu.go b/pkg/knuu/knuu.go index c74d3cc7..c2fc8c9b 100644 --- a/pkg/knuu/knuu.go +++ b/pkg/knuu/knuu.go @@ -6,9 +6,6 @@ import ( "fmt" "os" "os/signal" - "path" - "runtime" - "strconv" "strings" "syscall" "time" @@ -21,6 +18,7 @@ import ( "github.com/celestiaorg/knuu/pkg/builder/kaniko" "github.com/celestiaorg/knuu/pkg/instance" "github.com/celestiaorg/knuu/pkg/k8s" + "github.com/celestiaorg/knuu/pkg/log" "github.com/celestiaorg/knuu/pkg/minio" "github.com/celestiaorg/knuu/pkg/system" "github.com/celestiaorg/knuu/pkg/traefik" @@ -103,7 +101,7 @@ func New(ctx context.Context, opts ...Option) (*Knuu, error) { // handle default values if k.Logger == nil { - k.Logger = defaultLogger() + k.Logger = log.DefaultLogger() } if k.TestScope == "" { @@ -238,30 +236,3 @@ func (k *Knuu) handleTimeout(ctx context.Context) error { return nil } - -func defaultLogger() *logrus.Logger { - logger := logrus.New() - - logger.SetFormatter(&logrus.TextFormatter{ - FullTimestamp: true, - CallerPrettyfier: func(f *runtime.Frame) (string, string) { - filename := path.Base(f.File) - directory := path.Base(path.Dir(f.File)) - return "", directory + "/" + filename + ":" + strconv.Itoa(f.Line) - }, - }) - - // Enable reporting the file and line - logger.SetReportCaller(true) - - customLevel := os.Getenv("LOG_LEVEL") - if customLevel != "" { - err := logger.Level.UnmarshalText([]byte(customLevel)) - if err != nil { - logger.Warnf("Failed to parse LOG_LEVEL: %v, defaulting to INFO", err) - } - } - logger.Info("LOG_LEVEL: ", logger.GetLevel()) - - return logger -} diff --git a/pkg/log/logger.go b/pkg/log/logger.go new file mode 100644 index 00000000..d59cb611 --- /dev/null +++ b/pkg/log/logger.go @@ -0,0 +1,37 @@ +package log + +import ( + "os" + "path" + "runtime" + "strconv" + + "github.com/sirupsen/logrus" +) + +func DefaultLogger() *logrus.Logger { + logger := logrus.New() + + logger.SetFormatter(&logrus.TextFormatter{ + FullTimestamp: true, + CallerPrettyfier: func(f *runtime.Frame) (string, string) { + filename := path.Base(f.File) + directory := path.Base(path.Dir(f.File)) + return "", directory + "/" + filename + ":" + strconv.Itoa(f.Line) + }, + }) + + // Enable reporting the file and line + logger.SetReportCaller(true) + + customLevel := os.Getenv("LOG_LEVEL") + if customLevel != "" { + err := logger.Level.UnmarshalText([]byte(customLevel)) + if err != nil { + logger.Warnf("Failed to parse LOG_LEVEL: %v, defaulting to INFO", err) + } + } + logger.Info("LOG_LEVEL: ", logger.GetLevel()) + + return logger +}