mirror of
https://gitea.com/gitea/runner.git
synced 2026-11-06 23:30:29 +00:00
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:
1 parent
76110557ab
commit
4e8037a93b
25 files changed
+308
-246
No files matched your search
@@ -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
|
||||
}
|
||||
Reference in new issue
Block a user