diff --git a/Makefile b/Makefile index d231c2c3..127a7ba7 100644 --- a/Makefile +++ b/Makefile @@ -24,7 +24,7 @@ test-race: # a gate. Analysed for the host and for linux: a file behind a build tag is # invisible to a run on another platform, which is how a finding in # watcher_linux.go stayed hidden from every local run and failed in CI. -STATICCHECK ?= honnef.co/go/tools/cmd/staticcheck@v0.7.0 +STATICCHECK ?= honnef.co/go/tools/cmd/staticcheck@v0.8.1 # bench runs every benchmark once, briefly. It is a smoke test that the # benchmarks still compile and run - not a measurement. For numbers worth diff --git a/go.mod b/go.mod index 9345ace4..b173190e 100644 --- a/go.mod +++ b/go.mod @@ -1,6 +1,6 @@ module github.com/go-admin-team/go-admin-core/v2 -go 1.25.13 +go 1.27.1 require ( dario.cat/mergo v1.0.2 diff --git a/logger/default.go b/logger/default.go index 30ac98fd..3ca3ab2b 100644 --- a/logger/default.go +++ b/logger/default.go @@ -92,22 +92,37 @@ func logCallerfilePath(loggingFilePath string) string { // Log 记录日志条目 // Log logs a log entry func (l *defaultLogger) Log(level Level, v ...interface{}) { - l.logf(level, "", v...) + if !l.opts.Level.Enabled(level) { + return + } + l.emit(level, fmt.Sprint(v...)) } // Logf 记录格式化的日志条目 // Logf logs a formatted log entry func (l *defaultLogger) Logf(level Level, format string, v ...interface{}) { - l.logf(level, format, v...) -} - -// logf 记录日志条目 -// logf logs a log entry -func (l *defaultLogger) logf(level Level, format string, v ...interface{}) { if !l.opts.Level.Enabled(level) { return } + l.emit(level, fmt.Sprintf(format, v...)) +} +// emit writes one already-rendered message. +// +// The two callers above render the message themselves rather than handing a +// format string and its arguments down here, for two reasons. go vet reads any +// function that forwards both to fmt.Sprintf as a printf wrapper, and then +// reports Log's empty format string as a call "with arguments but no +// formatting directives" - which is precisely what Log is, deliberately, being +// the unformatted entry point. And rendering at the caller keeps that work +// behind the caller's Enabled check, which is where it was before. +// +// emit must stay exactly one frame below Log and Logf. Options.CallerSkipCount +// is counted outwards from the runtime.Caller call below, so adding or removing +// a frame here silently moves every file:line this logger computes. +// +// Callers are responsible for the Enabled check; emit does not repeat it. +func (l *defaultLogger) emit(level Level, message string) { l.RLock() fields := copyFields(l.opts.Fields) l.RUnlock() @@ -119,11 +134,7 @@ func (l *defaultLogger) logf(level Level, format string, v ...interface{}) { rec := dlog.Record{ Timestamp: time.Now(), Metadata: make(map[string]string, len(fields)), - } - if format == "" { - rec.Message = fmt.Sprint(v...) - } else { - rec.Message = fmt.Sprintf(format, v...) + Message: message, } for k, v := range fields { diff --git a/server/listener/server.go b/server/listener/server.go index f4fc1f29..31b6b189 100644 --- a/server/listener/server.go +++ b/server/listener/server.go @@ -120,7 +120,7 @@ func (e *Server) Start(ctx context.Context) error { <-ctx.Done() err = e.Shutdown(ctx) if err != nil { - log.Errorf("%S Server shutdown error: %s", e.name, err.Error()) + log.Errorf("%s Server shutdown error: %s", e.name, err.Error()) } }() if e.opts.startedHook != nil {