123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172 |
- package logutil
- import (
- "bytes"
- "strings"
- "testing"
- )
- func TestFormatting(t *testing.T) {
- ClearLogSinks()
- sf := TemplateFormatter("%t [%l] %s %m")
- sf.(*templateFormatter).tsFunc = func() string {
- return "0000000000000" // Timestamp for testing is always 0
- }
- rootBuf := &bytes.Buffer{}
- logger := GetLogger("")
- logger.AddLogSink(Debug, sf, rootBuf)
- logger.Info("foo")
- logger.Warning("bar")
- if rootBuf.String() != `
- 0000000000000 [Info] foo
- 0000000000000 [Warning] bar
- `[1:] {
- t.Error("Unexpected output:", rootBuf.String())
- return
- }
- ClearLogSinks()
- sf = TemplateFormatter("%c - %m")
- sf.(*templateFormatter).tsFunc = func() string {
- return "0000000000000" // Timestamp for testing is always 0
- }
- rootBuf = &bytes.Buffer{}
- logger = GetLogger("")
- logger.AddLogSink(Debug, sf, rootBuf)
- logger.Info("foo")
- logger.Warning("bar")
- if !strings.Contains(rootBuf.String(), "formatter_test.go:47") {
- t.Error("Unexpected output:", rootBuf.String())
- return
- }
- ClearLogSinks()
- rootBuf = &bytes.Buffer{}
- logger = GetLogger("")
- logger.AddLogSink(Debug, ConsoleFormatter(), rootBuf)
- logger.Info("foo")
- logger.Warning("bar")
- if rootBuf.String() != `
- Info: foo
- Warning: bar
- `[1:] {
- t.Error("Unexpected output:", rootBuf.String())
- return
- }
- }
|