go_log/main_test.go

70 lines
1.7 KiB
Go
Raw Normal View History

2022-10-15 23:22:06 +02:00
package go_log
2022-10-11 06:06:26 +02:00
2022-10-11 07:30:46 +02:00
import (
"testing"
2022-10-15 23:06:06 +02:00
"github.com/stretchr/testify/assert"
2022-10-11 07:30:46 +02:00
)
2022-10-11 06:06:26 +02:00
func TestDefault(t *testing.T) {
2022-10-15 23:06:06 +02:00
stdout := ""
stderr := ""
2022-10-11 06:06:26 +02:00
testLog := Log{}
testLog.SetDefaultValues()
2022-10-15 23:06:06 +02:00
testLog.Stdout = func(msg string) {
stdout += msg
}
testLog.Stderr = func(msg string) {
stderr += msg
2022-10-11 06:06:26 +02:00
}
2022-10-15 23:06:06 +02:00
testLog.Warn("oh noes")
testLog.Info("einfoo")
testLog.Verbose("moar moar")
testLog.Debug("deboug")
// Only info should be processed due to default log limit being info
assert.Equal(t, "[\x1b[32mInf\x1b[0m] einfoo\n", stdout[20:])
assert.Equal(t, "[\x1b[33mWrn\x1b[0m] oh noes\n", stderr[20:])
2022-10-11 06:06:26 +02:00
}
2022-10-11 07:30:46 +02:00
2022-10-15 23:06:06 +02:00
func TestError(t *testing.T) {
stderr := ""
testLog := Log{}
testLog.SetDefaultValues()
testLog.Stderr = func(msg string) {
stderr += msg
2022-10-11 07:30:46 +02:00
}
2022-10-15 23:06:06 +02:00
testLog.Error("lureri")
assert.Equal(t, "[\x1b[31mErr\x1b[0m] lureri\n", stderr[20:])
2022-10-11 07:30:46 +02:00
}
2022-10-15 23:06:06 +02:00
// func TestMetadata(t *testing.T) {
// testLog := Log{}
// testLog.SetDefaultValues()
// testLog.Context = []Metadata{
// {
// Name: "foo",
// Value: "bar",
// },
// {
// Name: "lur",
// Value: "pelle",
// },
// }
// testLog.Info("bosse")
// testLog.ErrorM("frasse", []Metadata{{Name: "wat", Value: ":O"}})
// }
// func TestWoo(t *testing.T) {
// loc, _ := time.LoadLocation("UTC")
// log := Log{
// Context: []Metadata{}, // Will be prepended to metadata on all log entries
// MinLogLvl: Info, // Minimal log level to output
// Fmt: DefaultFmt, // Log message formatter
// Stderr: DefaultStderr, // Log message outputter for Debug, Verbose and Info
// Stdout: DefaultStdout, // Log message outputter for Warning and Error
// TimeLocation: loc, // Timestamp location/time zone setting
// }
// log.Info("wat")
// }