Skip to content

Commit

Permalink
add logger interface defination#153
Browse files Browse the repository at this point in the history
  • Loading branch information
liulei committed Jan 5, 2022
1 parent d5f7cf7 commit 886965f
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 2 deletions.
18 changes: 16 additions & 2 deletions dtmcli/logger/log.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,27 @@ import (
"go.uber.org/zap/zapcore"
)

var logger *zap.SugaredLogger = nil
//var logger *zap.SugaredLogger = nil

var logger Logger = nil

func init() {
InitLog("info")
}

// InitLog is a initialization for a logger
// Logger logger interface
type Logger interface {
Debugf(format string, args ...interface{})
Infof(format string, args ...interface{})
Warnf(format string, args ...interface{})
Errorf(format string, args ...interface{})
}

func WithLogger(log Logger) {
logger = log
}

// InitLog is an initialization for a logger
// level can be: debug info warn error
func InitLog(level string) {
config := zap.NewProductionConfig()
Expand Down
12 changes: 12 additions & 0 deletions dtmcli/logger/logger_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package logger

import (
"github.com/sirupsen/logrus"
"os"
"testing"
)
Expand All @@ -15,3 +16,14 @@ func TestInitLog(t *testing.T) {
FatalfIf(false, "nothing")
FatalIfError(nil)
}

func TestWithLogger(t *testing.T) {
logger := logrus.New()
WithLogger(logger)
Debugf("a debug msg")
Infof("a info msg")
Warnf("a warn msg")
Errorf("a error msg")
FatalfIf(false, "nothing")
FatalIfError(nil)
}

0 comments on commit 886965f

Please sign in to comment.