diff --git a/cmd/daemon/daemon.go b/cmd/daemon/daemon.go index 841c5ab..4c9cc1b 100644 --- a/cmd/daemon/daemon.go +++ b/cmd/daemon/daemon.go @@ -32,7 +32,7 @@ var daemonCmd = &cobra.Command{ log.Fatalf("Can not parse clean cache interval into duration time: %v", err) } - logger := logger.NewDefaultLogger(logger.LogLevelDebug, false) + logger := logger.NewDefaultLogger(logger.LogLevelDebug) measurementUnit, err := sensor.SelectTemperatureMeasurementUnit(temperatureUnit) if err != nil { diff --git a/pkg/logger/default.go b/pkg/logger/default.go index baa48ef..45cf72e 100644 --- a/pkg/logger/default.go +++ b/pkg/logger/default.go @@ -38,20 +38,34 @@ func (dl *defaultLogger) Fatal(f string, v ...interface{}) { } func (dl *defaultLogger) log(ll LogLevel, f string, v ...interface{}) { + + if dl.logLevel > ll { + return + } + layout := "2006/01/02 15:04:05" dw := dl.stdout - switch dl.logLevel { + prefix := "" + switch ll { + case LogLevelDebug: + prefix = "DEBUG" + case LogLevelInfo: + prefix = "INFO" + case LogLevelWarn: + prefix = "WARN" case LogLevelError: + prefix = "ERROR" dw = dl.stderr case LogLevelFatal: + prefix = "FATAL" dw = dl.stderr } dl.mutex.Lock() - fmt.Fprintf(dw, "%v %v: %v \n", time.Now().Format(layout), ll, fmt.Sprintf(f, v...)) + fmt.Fprintf(dw, "%v %v: %v\n", time.Now().Format(layout), prefix, fmt.Sprintf(f, v...)) dl.mutex.Unlock() } -func NewDefaultLogger(logLevel LogLevel, time bool) *defaultLogger { +func NewDefaultLogger(logLevel LogLevel) Logger { return &defaultLogger{ logLevel: logLevel, mutex: new(sync.Mutex), @@ -60,7 +74,16 @@ func NewDefaultLogger(logLevel LogLevel, time bool) *defaultLogger { } } -func NewSilentLogger() *defaultLogger { +func NewCustomLogger(loglevel LogLevel, out io.Writer, err io.Writer) Logger { + return &defaultLogger{ + logLevel: loglevel, + mutex: new(sync.Mutex), + stdout: out, + stderr: err, + } +} + +func NewSilentLogger() Logger { return &defaultLogger{ logLevel: LogLevelDebug, mutex: new(sync.Mutex), diff --git a/pkg/logger/default_test.go b/pkg/logger/default_test.go new file mode 100644 index 0000000..dd28f54 --- /dev/null +++ b/pkg/logger/default_test.go @@ -0,0 +1,69 @@ +package logger_test + +import ( + "bytes" + "strings" + "testing" + + "github.com/go-flucky/flucky/pkg/logger" + "github.com/stretchr/testify/require" +) + +func TestLogger(t *testing.T) { + require := require.New(t) + stdoutBuffer := new(bytes.Buffer) + stderrBuffer := new(bytes.Buffer) + + // LogLevelDebug + l := logger.NewCustomLogger(logger.LogLevelDebug, stdoutBuffer, stderrBuffer) + l.Debug("DEBUG") + require.NotEmpty(stdoutBuffer.Bytes()) + require.Empty(stderrBuffer.Bytes()) + + stdoutBuffer.Reset() + stderrBuffer.Reset() + + // LogLevelInfo + l = logger.NewCustomLogger(logger.LogLevelInfo, stdoutBuffer, stderrBuffer) + + l.Debug("DEBUG") + require.Empty(stdoutBuffer.Bytes()) + require.Empty(stderrBuffer.Bytes()) + + stdoutBuffer.Reset() + stderrBuffer.Reset() + + l.Info("INFO") + require.NotEmpty(stdoutBuffer.Bytes()) + require.Empty(stderrBuffer.Bytes()) + + stringArray := strings.Split(stdoutBuffer.String(), ": ") + require.Equal(2, len(stringArray)) + require.Equal("INFO\n", stringArray[1]) + + stdoutBuffer.Reset() + stderrBuffer.Reset() + + // LogLevelWarn + l = logger.NewCustomLogger(logger.LogLevelWarn, stdoutBuffer, stderrBuffer) + l.Warn("WARN") + require.NotEmpty(stdoutBuffer.Bytes()) + require.Empty(stderrBuffer.Bytes()) + + stringArray = strings.Split(stdoutBuffer.String(), ": ") + require.Equal(2, len(stringArray)) + require.Equal("WARN\n", stringArray[1]) + + stdoutBuffer.Reset() + stderrBuffer.Reset() + + // LogLevelError + l = logger.NewCustomLogger(logger.LogLevelError, stdoutBuffer, stderrBuffer) + l.Error("ERROR") + require.Empty(stdoutBuffer.Bytes()) + require.NotEmpty(stderrBuffer.Bytes()) + + stringArray = strings.Split(stderrBuffer.String(), ": ") + require.Equal(2, len(stringArray)) + require.Equal("ERROR\n", stringArray[1]) +} diff --git a/pkg/logger/loglevel.go b/pkg/logger/loglevel.go index 9a6ed21..71ecfbf 100644 --- a/pkg/logger/loglevel.go +++ b/pkg/logger/loglevel.go @@ -1,11 +1,11 @@ package logger -type LogLevel string +type LogLevel int const ( - LogLevelDebug LogLevel = "DEBUG" - LogLevelInfo LogLevel = "INFO" - LogLevelWarn LogLevel = "WARN" - LogLevelError LogLevel = "ERROR" - LogLevelFatal LogLevel = "FATAL" + LogLevelDebug LogLevel = iota + 1 + LogLevelInfo + LogLevelWarn + LogLevelError + LogLevelFatal )