test(pkg/logger): logger test
This commit is contained in:
parent
16705d1164
commit
02fa0cc931
@ -32,7 +32,7 @@ var daemonCmd = &cobra.Command{
|
|||||||
log.Fatalf("Can not parse clean cache interval into duration time: %v", err)
|
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)
|
measurementUnit, err := sensor.SelectTemperatureMeasurementUnit(temperatureUnit)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
@ -38,20 +38,34 @@ func (dl *defaultLogger) Fatal(f string, v ...interface{}) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (dl *defaultLogger) log(ll LogLevel, 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"
|
layout := "2006/01/02 15:04:05"
|
||||||
dw := dl.stdout
|
dw := dl.stdout
|
||||||
switch dl.logLevel {
|
prefix := ""
|
||||||
|
switch ll {
|
||||||
|
case LogLevelDebug:
|
||||||
|
prefix = "DEBUG"
|
||||||
|
case LogLevelInfo:
|
||||||
|
prefix = "INFO"
|
||||||
|
case LogLevelWarn:
|
||||||
|
prefix = "WARN"
|
||||||
case LogLevelError:
|
case LogLevelError:
|
||||||
|
prefix = "ERROR"
|
||||||
dw = dl.stderr
|
dw = dl.stderr
|
||||||
case LogLevelFatal:
|
case LogLevelFatal:
|
||||||
|
prefix = "FATAL"
|
||||||
dw = dl.stderr
|
dw = dl.stderr
|
||||||
}
|
}
|
||||||
dl.mutex.Lock()
|
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()
|
dl.mutex.Unlock()
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewDefaultLogger(logLevel LogLevel, time bool) *defaultLogger {
|
func NewDefaultLogger(logLevel LogLevel) Logger {
|
||||||
return &defaultLogger{
|
return &defaultLogger{
|
||||||
logLevel: logLevel,
|
logLevel: logLevel,
|
||||||
mutex: new(sync.Mutex),
|
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{
|
return &defaultLogger{
|
||||||
logLevel: LogLevelDebug,
|
logLevel: LogLevelDebug,
|
||||||
mutex: new(sync.Mutex),
|
mutex: new(sync.Mutex),
|
||||||
|
69
pkg/logger/default_test.go
Normal file
69
pkg/logger/default_test.go
Normal file
@ -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])
|
||||||
|
}
|
@ -1,11 +1,11 @@
|
|||||||
package logger
|
package logger
|
||||||
|
|
||||||
type LogLevel string
|
type LogLevel int
|
||||||
|
|
||||||
const (
|
const (
|
||||||
LogLevelDebug LogLevel = "DEBUG"
|
LogLevelDebug LogLevel = iota + 1
|
||||||
LogLevelInfo LogLevel = "INFO"
|
LogLevelInfo
|
||||||
LogLevelWarn LogLevel = "WARN"
|
LogLevelWarn
|
||||||
LogLevelError LogLevel = "ERROR"
|
LogLevelError
|
||||||
LogLevelFatal LogLevel = "FATAL"
|
LogLevelFatal
|
||||||
)
|
)
|
||||||
|
Loading…
Reference in New Issue
Block a user