build: update golangci-lint to v2.12.2 with canonical config
Add the canonical .golangci.yml (v2 schema, all linters enabled with a
small documented disable list) and pin the Dockerfile lint stage to
golangci/golangci-lint:v2.12.2 by tag and digest, replacing the old
v1.64.8 digest-only pin.
Fix all findings surfaced by the v1 to v2 jump without changing any
exported signatures or behavior:
- add package and exported-symbol doc comments (revive)
- rename unused handler parameters to underscore (revive)
- check or explicitly discard error returns (errcheck, errchkjson)
- wrap errors with %w instead of %v (err113)
- use http.NewRequestWithContext instead of http.Post (noctx)
- replace fmt.Println with fmt.Fprintln(os.Stdout, ...) (forbidigo)
- name magic numbers as constants (mnd)
- add explicit slog.LevelDebug case (exhaustive)
- interface{} to any (modernize)
- move tests to the simplelog_test package (testpackage) and add
t.Parallel() (paralleltest)
- move NewWebhookHandler above its methods (funcorder)
- whitespace, line-length, and blank-line fixes (wsl_v5, whitespace,
nlreturn, lll, embeddedstructfieldcheck)
- nolint with justification for the intentional init/global design
(gochecknoinits, gochecknoglobals) and interface-returning
constructor (ireturn)
This commit is contained in:
@@ -4,26 +4,38 @@ import (
|
||||
"context"
|
||||
"fmt"
|
||||
"log/slog"
|
||||
"os"
|
||||
"runtime"
|
||||
"time"
|
||||
|
||||
"github.com/fatih/color"
|
||||
)
|
||||
|
||||
// callerSkipFrames is the number of stack frames between runtime.Caller
|
||||
// and the slog call site that produced the record.
|
||||
const callerSkipFrames = 4
|
||||
|
||||
// ConsoleHandler writes human-readable, colored log lines to stdout.
|
||||
type ConsoleHandler struct{}
|
||||
|
||||
// NewConsoleHandler returns a new ConsoleHandler.
|
||||
func NewConsoleHandler() *ConsoleHandler {
|
||||
return &ConsoleHandler{}
|
||||
}
|
||||
|
||||
// Handle writes the record to stdout as a colored, timestamped line
|
||||
// including the caller file and line.
|
||||
func (c *ConsoleHandler) Handle(
|
||||
ctx context.Context,
|
||||
_ context.Context,
|
||||
record slog.Record,
|
||||
) error {
|
||||
timestamp := time.Now().UTC().Format("2006-01-02T15:04:05.000Z07:00")
|
||||
var colorFunc func(format string, a ...interface{}) string
|
||||
|
||||
var colorFunc func(format string, a ...any) string
|
||||
|
||||
switch record.Level {
|
||||
case slog.LevelDebug:
|
||||
colorFunc = color.New(color.FgWhite).SprintfFunc()
|
||||
case slog.LevelInfo:
|
||||
colorFunc = color.New(color.FgBlue).SprintfFunc()
|
||||
case slog.LevelWarn:
|
||||
@@ -35,12 +47,14 @@ func (c *ConsoleHandler) Handle(
|
||||
}
|
||||
|
||||
// Get the caller information
|
||||
_, file, line, ok := runtime.Caller(4)
|
||||
_, file, line, ok := runtime.Caller(callerSkipFrames)
|
||||
if !ok {
|
||||
file = "???"
|
||||
line = 0
|
||||
}
|
||||
fmt.Println(
|
||||
|
||||
_, _ = fmt.Fprintln(
|
||||
os.Stdout,
|
||||
colorFunc(
|
||||
"%s [%s] %s:%d: %s",
|
||||
timestamp,
|
||||
@@ -50,20 +64,25 @@ func (c *ConsoleHandler) Handle(
|
||||
record.Message,
|
||||
),
|
||||
)
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// Enabled reports whether the handler processes records at the given
|
||||
// level; it always returns true.
|
||||
func (c *ConsoleHandler) Enabled(
|
||||
ctx context.Context,
|
||||
level slog.Level,
|
||||
_ context.Context,
|
||||
_ slog.Level,
|
||||
) bool {
|
||||
return true
|
||||
}
|
||||
|
||||
func (c *ConsoleHandler) WithAttrs(attrs []slog.Attr) slog.Handler {
|
||||
// WithAttrs returns the handler unchanged; attributes are not rendered.
|
||||
func (c *ConsoleHandler) WithAttrs(_ []slog.Attr) slog.Handler {
|
||||
return c
|
||||
}
|
||||
|
||||
func (c *ConsoleHandler) WithGroup(name string) slog.Handler {
|
||||
// WithGroup returns the handler unchanged; groups are not rendered.
|
||||
func (c *ConsoleHandler) WithGroup(_ string) slog.Handler {
|
||||
return c
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user