2014-03-11 03:22:08 +04:00
|
|
|
package logrus
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"encoding/json"
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
)
|
|
|
|
|
|
|
|
func LogAndAssertJSON(t *testing.T, log func(*Logger), assertions func(fields Fields)) {
|
|
|
|
var buffer bytes.Buffer
|
|
|
|
var fields Fields
|
|
|
|
|
|
|
|
logger := New()
|
|
|
|
logger.Out = &buffer
|
|
|
|
logger.Formatter = new(JSONFormatter)
|
|
|
|
|
|
|
|
log(logger)
|
|
|
|
|
|
|
|
err := json.Unmarshal(buffer.Bytes(), &fields)
|
|
|
|
assert.Nil(t, err)
|
|
|
|
|
|
|
|
assertions(fields)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestPrint(t *testing.T) {
|
|
|
|
LogAndAssertJSON(t, func(log *Logger) {
|
|
|
|
log.Print("test")
|
|
|
|
}, func(fields Fields) {
|
|
|
|
assert.Equal(t, fields["msg"], "test")
|
|
|
|
assert.Equal(t, fields["level"], "info")
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestInfo(t *testing.T) {
|
|
|
|
LogAndAssertJSON(t, func(log *Logger) {
|
|
|
|
log.Info("test")
|
|
|
|
}, func(fields Fields) {
|
|
|
|
assert.Equal(t, fields["msg"], "test")
|
|
|
|
assert.Equal(t, fields["level"], "info")
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestWarn(t *testing.T) {
|
|
|
|
LogAndAssertJSON(t, func(log *Logger) {
|
|
|
|
log.Warn("test")
|
|
|
|
}, func(fields Fields) {
|
|
|
|
assert.Equal(t, fields["msg"], "test")
|
|
|
|
assert.Equal(t, fields["level"], "warning")
|
|
|
|
})
|
|
|
|
}
|
2014-03-19 20:02:00 +04:00
|
|
|
|
2014-05-04 03:51:12 +04:00
|
|
|
func TestInfolnShouldAddSpacesBetweenStrings(t *testing.T) {
|
|
|
|
LogAndAssertJSON(t, func(log *Logger) {
|
|
|
|
log.Infoln("test", "test")
|
|
|
|
}, func(fields Fields) {
|
|
|
|
assert.Equal(t, fields["msg"], "test test")
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestInfolnShouldAddSpacesBetweenStringAndNonstring(t *testing.T) {
|
|
|
|
LogAndAssertJSON(t, func(log *Logger) {
|
|
|
|
log.Infoln("test", 10)
|
|
|
|
}, func(fields Fields) {
|
|
|
|
assert.Equal(t, fields["msg"], "test 10")
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestInfolnShouldAddSpacesBetweenTwoNonStrings(t *testing.T) {
|
|
|
|
LogAndAssertJSON(t, func(log *Logger) {
|
|
|
|
log.Infoln(10, 10)
|
|
|
|
}, func(fields Fields) {
|
|
|
|
assert.Equal(t, fields["msg"], "10 10")
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestInfoShouldAddSpacesBetweenTwoNonStrings(t *testing.T) {
|
|
|
|
LogAndAssertJSON(t, func(log *Logger) {
|
|
|
|
log.Infoln(10, 10)
|
|
|
|
}, func(fields Fields) {
|
|
|
|
assert.Equal(t, fields["msg"], "10 10")
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestInfoShouldNotAddSpacesBetweenStringAndNonstring(t *testing.T) {
|
|
|
|
LogAndAssertJSON(t, func(log *Logger) {
|
|
|
|
log.Info("test", 10)
|
|
|
|
}, func(fields Fields) {
|
|
|
|
assert.Equal(t, fields["msg"], "test10")
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestInfoShouldNotAddSpacesBetweenStrings(t *testing.T) {
|
|
|
|
LogAndAssertJSON(t, func(log *Logger) {
|
|
|
|
log.Info("test", "test")
|
|
|
|
}, func(fields Fields) {
|
|
|
|
assert.Equal(t, fields["msg"], "testtest")
|
|
|
|
})
|
|
|
|
}
|
2014-06-09 19:53:23 +04:00
|
|
|
|
|
|
|
func TestWithFieldsShouldAllowAssignments(t *testing.T) {
|
|
|
|
var buffer bytes.Buffer
|
|
|
|
var fields Fields
|
|
|
|
|
|
|
|
logger := New()
|
|
|
|
logger.Out = &buffer
|
|
|
|
logger.Formatter = new(JSONFormatter)
|
|
|
|
|
|
|
|
localLog := logger.WithFields(Fields{
|
|
|
|
"key1": "value1",
|
|
|
|
})
|
|
|
|
|
|
|
|
localLog.WithField("key2", "value2").Info("test")
|
|
|
|
err := json.Unmarshal(buffer.Bytes(), &fields)
|
|
|
|
assert.Nil(t, err)
|
|
|
|
|
|
|
|
assert.Equal(t, "value2", fields["key2"])
|
|
|
|
assert.Equal(t, "value1", fields["key1"])
|
|
|
|
|
|
|
|
buffer = bytes.Buffer{}
|
|
|
|
fields = Fields{}
|
|
|
|
localLog.Info("test")
|
|
|
|
err = json.Unmarshal(buffer.Bytes(), &fields)
|
|
|
|
assert.Nil(t, err)
|
|
|
|
|
|
|
|
_, ok := fields["key2"]
|
|
|
|
assert.Equal(t, false, ok)
|
|
|
|
assert.Equal(t, "value1", fields["key1"])
|
|
|
|
}
|