test(logging): add logtest.NewLogger (#1300)

Return a `Logger` and its recording `Handler` together, so tests no longer need the two-line setup.

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

Assisted-by: Codet
Co-authored-by: bircni <[email protected]>
Reviewed-on: https://gitea.com/gitea/runner/pulls/1300
Reviewed-by: bircni <[email protected]>
This commit is contained in:
Lunny Xiaoandbircni committed 2026-10-11 23:47:50 +00:00
1 parent de961b91eb
commit 3bc5cc03f8
7 files changed
+21 -32

No files matched your search

+1 -2
View File
@@ -92,8 +92,7 @@ func frameOf(t *testing.T, pc uintptr) runtime.Frame {
}
func TestCallerPC(t *testing.T) {
h := logtest.New()
l := logging.New(h)
l, h := logtest.NewLogger()
l.Infof("method")
l.Info("method")
for _, r := range h.AllRecords() {
+6
View File
@@ -29,6 +29,12 @@ func New() *Handler {
return NewWithLevel(logging.LevelInfo)
}
// NewLogger returns a Logger writing to a new Handler from New, and that Handler.
func NewLogger() (*logging.Logger, *Handler) {
h := New()
return logging.New(h), h
}
func NewWithLevel(minLevel logging.Level) *Handler {
return &Handler{store: &store{}, minLevel: minLevel}
}