1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121
|
package logflags
import (
"bytes"
"io"
"log/slog"
"testing"
)
type dummyLogger struct {
Logger
}
func TestMakeLogger_usingLoggerFactory(t *testing.T) {
if loggerFactory != nil {
t.Fatalf("expected loggerFactory to be nil; but was <%v>", loggerFactory)
}
defer func() {
loggerFactory = nil
}()
if logOut != nil {
t.Fatalf("expected logOut to be nil; but was <%v>", logOut)
}
logOut = &bufferWriter{}
defer func() {
logOut = nil
}()
expectedLogger := &dummyLogger{}
SetLoggerFactory(func(flag bool, fields Fields, out io.Writer) Logger {
if flag != true {
t.Fatalf("expected flag to be <%v>; but was <%v>", true, flag)
}
if len(fields) != 1 || fields["foo"] != "bar" {
t.Fatalf("expected fields to be {'foo':'bar'}; but was <%v>", fields)
}
if out != logOut {
t.Fatalf("expected out to be <%v>; but was <%v>", logOut, out)
}
return expectedLogger
})
actual := makeLogger(true, "foo", "bar")
if actual != expectedLogger {
t.Fatalf("expected actual to <%v>; but was <%v>", expectedLogger, actual)
}
}
func TestMakeLogger_usingDefaultBehavior(t *testing.T) {
if loggerFactory != nil {
t.Fatalf("expected loggerFactory to be nil; but was <%v>", loggerFactory)
}
if logOut != nil {
t.Fatalf("expected logOut to be nil; but was <%v>", logOut)
}
logOut = &bufferWriter{}
defer func() {
logOut = nil
}()
actual := makeLogger(false, "foo", "bar")
actualEntry, expectedType := actual.(slogLogger)
if !expectedType {
t.Fatalf("expected actual to be of type slogLogger; but was %T", actual)
}
h, ok := actualEntry.s.Handler().(*textHandler)
if !ok {
t.Fatalf("expected handler to be *textHandler; but was %T", actualEntry.s.Handler())
}
if lvl := h.opts.Level.Level(); lvl != slog.LevelError {
t.Fatalf("expected level to be <%v>; but was <%v>", slog.LevelError, lvl)
}
if h.out != logOut {
t.Fatalf("expected output to be <%v>; but was <%v>", logOut, h.out)
}
if len(h.attrs) != 1 || h.attrs[0].Key != "foo" || h.attrs[0].Value.String() != "bar" {
t.Fatalf("expected attributes to be {'foo':'bar'}; but was <%v>", h.attrs)
}
}
func TestMakeLogger_usingDefaultBehaviorAndFlagged(t *testing.T) {
if loggerFactory != nil {
t.Fatalf("expected loggerFactory to be nil; but was <%v>", loggerFactory)
}
if logOut != nil {
t.Fatalf("expected logOut to be nil; but was <%v>", logOut)
}
logOut = &bufferWriter{}
defer func() {
logOut = nil
}()
actual := makeLogger(true, "foo", "bar")
actualEntry, expectedType := actual.(slogLogger)
if !expectedType {
t.Fatalf("expected actual to be of type slogLogger; but was %T", actual)
}
h, ok := actualEntry.s.Handler().(*textHandler)
if !ok {
t.Fatalf("expected actualEntry.Entry.Logger.Formatter to be *textHandler; but was %T", actualEntry.s.Handler())
}
if lvl := h.opts.Level.Level(); lvl != slog.LevelDebug {
t.Fatalf("expected actualEntry.Entry.Logger.Level to be <%v>; but was <%v>", slog.LevelError, lvl)
}
if h.out != logOut {
t.Fatalf("expected actualEntry.Entry.Logger.Out to be <%v>; but was <%v>", logOut, h.out)
}
if len(h.attrs) != 1 || h.attrs[0].Key != "foo" || h.attrs[0].Value.String() != "bar" {
t.Fatalf("expected actualEntry.Entry.Data to be {'foo':'bar'}; but was <%v>", h.attrs)
}
}
type bufferWriter struct {
bytes.Buffer
}
func (bw bufferWriter) Close() error {
return nil
}
|