refactor(logging): use the logging package in internal (#1302)

Move the log calls in `internal` onto `logging`, and add what the process-wide logger setup and its tests need: `SetTextFormat` with a line filter for secret masking (replacing `report.MaskingFormatter`), `SetReportCaller`, `SetDefault` and `logtest.SetDefault`. After this only the `logging` package imports logrus.

Stacked on https://gitea.com/gitea/runner/pulls/1301.

Assisted-by: Codet
Co-authored-by: bircni <[email protected]>
Reviewed-on: https://gitea.com/gitea/runner/pulls/1302
Reviewed-by: bircni <[email protected]>
This commit is contained in:
Lunny Xiaoandbircni committed 2026-10-12 00:19:01 +00:00
1 parent 76110557ab
commit 4e8037a93b
25 files changed
+308 -246

No files matched your search

+50
View File
@@ -0,0 +1,50 @@
// Copyright 2026 The Gitea Authors. All rights reserved.
// SPDX-License-Identifier: MIT
package logging
import (
"runtime"
"github.com/sirupsen/logrus"
)
// TextOptions describes how the process-wide Logger writes a record as text.
type TextOptions struct {
DisableColors bool
DisableTimestamp bool
FullTimestamp bool
// CallerPrettyfier renames the function and file reported with a record when ReportCaller is on.
CallerPrettyfier func(*runtime.Frame) (function, file string)
// Filter rewrites each formatted line, for example to mask secrets.
Filter func(line []byte) []byte
}
// SetTextFormat sets the text format of the process-wide Logger.
func SetTextFormat(o TextOptions) {
logrus.SetFormatter(&filteringFormatter{
inner: &logrus.TextFormatter{
DisableColors: o.DisableColors,
DisableTimestamp: o.DisableTimestamp,
FullTimestamp: o.FullTimestamp,
CallerPrettyfier: o.CallerPrettyfier,
},
filter: o.Filter,
})
}
// SetReportCaller makes the process-wide Logger record where each call came from, which costs time.
func SetReportCaller(on bool) { logrus.SetReportCaller(on) }
type filteringFormatter struct {
inner logrus.Formatter
filter func([]byte) []byte
}
func (f *filteringFormatter) Format(e *logrus.Entry) ([]byte, error) {
line, err := f.inner.Format(e)
if err != nil || f.filter == nil {
return line, err
}
return f.filter(line), nil
}
+25 -15
View File
@@ -5,14 +5,24 @@ package logging
import (
"io"
"sync/atomic"
"github.com/sirupsen/logrus"
)
var std = New(NewLogrusHandler(logrus.StandardLogger()))
var std atomic.Pointer[Logger]
func init() {
std.Store(New(NewLogrusHandler(logrus.StandardLogger())))
}
// Default returns the process-wide Logger.
func Default() *Logger { return std }
func Default() *Logger { return std.Load() }
// SetDefault replaces the process-wide Logger, which tests use to capture what the package
// functions log. SetLevel, GetLevel, SetOutput and the format functions keep configuring the
// initial logrus-backed Logger.
func SetDefault(l *Logger) { std.Store(l) }
// SetLevel sets the minimum level of the process-wide Logger.
func SetLevel(level Level) { logrus.SetLevel(toLogrusLevel(level)) }
@@ -26,17 +36,17 @@ func SetOutput(w io.Writer) { logrus.SetOutput(w) }
// Output returns where the process-wide Logger writes.
func Output() io.Writer { return logrus.StandardLogger().Out }
func Enabled(level Level) bool { return std.Enabled(level) }
func Enabled(level Level) bool { return Default().Enabled(level) }
func With(args ...any) *Logger { return std.With(args...) }
func WithError(err error) *Logger { return std.WithError(err) }
func Tracef(format string, args ...any) { std.logf(LevelTrace, format, args) }
func Debugf(format string, args ...any) { std.logf(LevelDebug, format, args) }
func Infof(format string, args ...any) { std.logf(LevelInfo, format, args) }
func Warnf(format string, args ...any) { std.logf(LevelWarn, format, args) }
func Errorf(format string, args ...any) { std.logf(LevelError, format, args) }
func Trace(args ...any) { std.log(LevelTrace, args) }
func Debug(args ...any) { std.log(LevelDebug, args) }
func Info(args ...any) { std.log(LevelInfo, args) }
func Warn(args ...any) { std.log(LevelWarn, args) }
func Error(args ...any) { std.log(LevelError, args) }
func With(args ...any) *Logger { return Default().With(args...) }
func WithError(err error) *Logger { return Default().WithError(err) }
func Tracef(format string, args ...any) { Default().logf(LevelTrace, format, args) }
func Debugf(format string, args ...any) { Default().logf(LevelDebug, format, args) }
func Infof(format string, args ...any) { Default().logf(LevelInfo, format, args) }
func Warnf(format string, args ...any) { Default().logf(LevelWarn, format, args) }
func Errorf(format string, args ...any) { Default().logf(LevelError, format, args) }
func Trace(args ...any) { Default().log(LevelTrace, args) }
func Debug(args ...any) { Default().log(LevelDebug, args) }
func Info(args ...any) { Default().log(LevelInfo, args) }
func Warn(args ...any) { Default().log(LevelWarn, args) }
func Error(args ...any) { Default().log(LevelError, args) }
+11
View File
@@ -8,6 +8,7 @@ import (
"context"
"slices"
"sync"
"testing"
"gitea.com/gitea/runner/act/common/logging"
)
@@ -76,3 +77,13 @@ func (h *Handler) Reset() {
defer h.store.mu.Unlock()
h.store.records = nil
}
// SetDefault makes the process-wide Logger record into a new Handler until the test ends.
func SetDefault(t testing.TB) *Handler {
t.Helper()
prev := logging.Default()
logger, h := NewLogger()
logging.SetDefault(logger)
t.Cleanup(func() { logging.SetDefault(prev) })
return h
}