(chore): document and test new LogLevel APIs
Golang lint / lint (push) Successful in 47s

- add godoc for SameLevel and Equal
- update package docs and READMEs with new helpers
- refresh unreleased release notes
- add tests for Printf, HTTP method levels, and level constructors
This commit is contained in:
2026-04-27 17:04:41 +03:00
parent 812caed471
commit b7fa3f7606
6 changed files with 197 additions and 2 deletions
+100
View File
@@ -4,6 +4,7 @@ import (
"bytes"
"encoding/json"
"errors"
"net/http"
"os"
"strings"
"testing"
@@ -100,6 +101,105 @@ func TestLoggerPreservesMessageTypesWithoutReplacers(t *testing.T) {
}
}
func TestLoggerPrintfFormatsMessage(t *testing.T) {
writer := &stubLoggerWriter{}
logger := CreateLogger().
SetLevel(DEBUG).
AddWriter(writer)
logger.Printf(INFO, "status=%d %s", 200, "ok")
if writer.printCalls != 1 {
t.Fatalf("Printf() should write once, got %d calls", writer.printCalls)
}
if len(writer.messages) != 1 {
t.Fatalf("Printf() should produce one formatted message, got %d parts", len(writer.messages))
}
if got, ok := writer.messages[0].(string); !ok || got != "status=200 ok" {
t.Fatalf("Printf() should format message, got %#v", writer.messages)
}
}
func TestLogLevelForMethodReturnsExpectedLevels(t *testing.T) {
tests := []struct {
name string
method string
want LogLevel
}{
{name: "get", method: http.MethodGet, want: HTTPGetLevel},
{name: "head", method: http.MethodHead, want: HTTPHeadLevel},
{name: "post", method: http.MethodPost, want: HTTPPostLevel},
{name: "put", method: http.MethodPut, want: HTTPPutLevel},
{name: "patch", method: http.MethodPatch, want: HTTPPatchLevel},
{name: "delete", method: http.MethodDelete, want: HTTPDeleteLevel},
{name: "options", method: http.MethodOptions, want: HTTPOptionsLevel},
{name: "connect", method: http.MethodConnect, want: HTTPConnectLevel},
{name: "trace", method: http.MethodTrace, want: HTTPTraceLevel},
{name: "unknown", method: "PROPFIND", want: HTTPUnknownLevel},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got := LogLevelForMethod(tt.method)
if got.n != tt.want.n || got.t != tt.want.t || got.fg != tt.want.fg || got.bg != tt.want.bg {
t.Fatalf("LogLevelForMethod(%q) = %#v, want %#v", tt.method, got, tt.want)
}
})
}
}
func TestNewLevelConstructorsSetExpectedSeverity(t *testing.T) {
tests := []struct {
name string
got LogLevel
want LogLevel
}{
{name: "info", got: NewInfoLogLevel("custom"), want: INFO},
{name: "warn", got: NewWarnLogLevel("custom"), want: WARN},
{name: "error", got: NewErrorLogLevel("custom"), want: ERROR},
{name: "fatal", got: NewFatalLogLevel("custom"), want: FATAL},
{name: "debug", got: NewDebugLogLevel("custom"), want: DEBUG},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if tt.got.n != tt.want.n {
t.Fatalf("%s severity = %d, want %d", tt.name, tt.got.n, tt.want.n)
}
if tt.got.GetName() != "custom" {
t.Fatalf("%s constructor should preserve name, got %q", tt.name, tt.got.GetName())
}
})
}
}
func TestNewLevelConstructorsWithColorsSetExpectedFields(t *testing.T) {
info := NewInfoLogLevelWithColors("info-custom", FgBlue, BgYellow)
if info.n != INFO.n || info.GetName() != "info-custom" || info.fg != FgBlue || info.bg != BgYellow {
t.Fatalf("NewInfoLogLevelWithColors() = %#v", info)
}
warn := NewWarnLogLevelWithColors("warn-custom", FgMagenta, BgCyan)
if warn.n != WARN.n || warn.GetName() != "warn-custom" || warn.fg != FgMagenta || warn.bg != BgCyan {
t.Fatalf("NewWarnLogLevelWithColors() = %#v", warn)
}
errLevel := NewErrorLogLevelWithColors("error-custom", FgRed, BgWhite)
if errLevel.n != ERROR.n || errLevel.GetName() != "error-custom" || errLevel.fg != FgRed || errLevel.bg != BgWhite {
t.Fatalf("NewErrorLogLevelWithColors() = %#v", errLevel)
}
fatal := NewFatalLogLevelWithColors("fatal-custom", FgHiRed, BgBlack)
if fatal.n != FATAL.n || fatal.GetName() != "fatal-custom" || fatal.fg != FgHiRed || fatal.bg != BgBlack {
t.Fatalf("NewFatalLogLevelWithColors() = %#v", fatal)
}
debug := NewDebugLogLevelWithColors("debug-custom", FgGreen, BgDefault)
if debug.n != DEBUG.n || debug.GetName() != "debug-custom" || debug.fg != FgGreen || debug.bg != BgDefault {
t.Fatalf("NewDebugLogLevelWithColors() = %#v", debug)
}
}
func TestCreateTextWriterCloseOnNonCloserIsNoOp(t *testing.T) {
writer := CreateTextWriter(&bytes.Buffer{})
if err := writer.Close(); err != nil {