Files
runner/act/common/logging/format.go
T
Lunny Xiaoandbircni 4e8037a93b 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]>
2026-10-12 00:19:01 +00:00

51 lines
1.4 KiB
Go

// 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
}