75 lines
1.5 KiB
Go
75 lines
1.5 KiB
Go
package logging
|
|||
|
|
|
||
|
|
import (
|
||
|
|
"testing"
|
||
|
|
|
||
|
|
"github.com/stretchr/testify/assert"
|
||
|
|
)
|
||
|
|
|
||
|
|
func TestInitLogger(t *testing.T) {
|
||
|
|
err := InitLogger()
|
||
|
|
assert.NoError(t, err)
|
||
|
|
}
|
||
|
|
|
||
|
|
func TestGetLogger(t *testing.T) {
|
||
|
|
lg := GetLogger()
|
||
|
|
assert.NotNil(t, lg)
|
||
|
|
}
|
||
|
|
|
||
|
|
func TestStringField(t *testing.T) {
|
||
|
|
field := String("key", "value")
|
||
|
|
assert.NotNil(t, field)
|
||
|
|
assert.Equal(t, "key", field.Key)
|
||
|
|
}
|
||
|
|
|
||
|
|
func TestIntField(t *testing.T) {
|
||
|
|
field := Int("counter", 42)
|
||
|
|
assert.NotNil(t, field)
|
||
|
|
assert.Equal(t, "counter", field.Key)
|
||
|
|
}
|
||
|
|
|
||
|
|
func TestInt64Field(t *testing.T) {
|
||
|
|
field := Int64("bignum", 9223372036854775807)
|
||
|
|
assert.NotNil(t, field)
|
||
|
|
assert.Equal(t, "bignum", field.Key)
|
||
|
|
}
|
||
|
|
|
||
|
|
func TestErrorField(t *testing.T) {
|
||
|
|
err := assert.AnError
|
||
|
|
field := Err(err)
|
||
|
|
assert.NotNil(t, field)
|
||
|
|
assert.Equal(t, "error", field.Key)
|
||
|
|
}
|
||
|
|
|
||
|
|
// Note: TestSync is omitted because zap.Sync() may fail on stderr in test environment
|
||
|
|
// This is expected behavior and doesn't affect production use
|
||
|
|
|
||
|
|
func TestWith(t *testing.T) {
|
||
|
|
InitLogger()
|
||
|
|
lg := With(String("test", "value"))
|
||
|
|
assert.NotNil(t, lg)
|
||
|
|
}
|
||
|
|
|
||
|
|
// TestLoggingFunctions tests that logging functions don't panic
|
||
|
|
func TestLoggingFunctions(t *testing.T) {
|
||
|
|
InitLogger()
|
||
|
|
defer Sync()
|
||
|
|
|
||
|
|
// These should not panic
|
||
|
|
assert.NotPanics(t, func() {
|
||
|
|
Info("test info", String("field", "value"))
|
||
|
|
})
|
||
|
|
|
||
|
|
assert.NotPanics(t, func() {
|
||
|
|
Warn("test warn", String("field", "value"))
|
||
|
|
})
|
||
|
|
|
||
|
|
assert.NotPanics(t, func() {
|
||
|
|
Debug("test debug", String("field", "value"))
|
||
|
|
})
|
||
|
|
|
||
|
|
assert.NotPanics(t, func() {
|
||
|
|
Error("test error", String("field", "value"))
|
||
|
|
})
|
||
|
|
}
|