mirror of
https://gitea.com/gitea/runner.git
synced 2026-11-14 03:43:42 +00:00
refactor(logging): use the logging package for the remaining act log calls (#1301)
Move the global logrus calls left in `act` onto `logging`, and add `SetOutput` and `Output` for tests that capture the process logger. Behavior is unchanged. Stacked on https://gitea.com/gitea/runner/pulls/1300. Assisted-by: Codet Co-authored-by: bircni <[email protected]> Reviewed-on: https://gitea.com/gitea/runner/pulls/1301 Reviewed-by: bircni <[email protected]>
This commit is contained in:
1 parent
3bc5cc03f8
commit
76110557ab
9 files changed
+87
-75
No files matched your search
@@ -9,7 +9,7 @@ import (
|
||||
"fmt"
|
||||
"runtime/debug"
|
||||
|
||||
log "github.com/sirupsen/logrus"
|
||||
"gitea.com/gitea/runner/act/common/logging"
|
||||
)
|
||||
|
||||
// Executor define contract for the steps of a workflow
|
||||
@@ -74,33 +74,33 @@ func NewParallelExecutor(parallel int, executors ...Executor) Executor {
|
||||
errs := make(chan error, len(executors))
|
||||
|
||||
if 1 > parallel {
|
||||
log.Debugf("Parallel tasks (%d) below minimum, setting to 1", parallel)
|
||||
logging.Debugf("Parallel tasks (%d) below minimum, setting to 1", parallel)
|
||||
parallel = 1
|
||||
}
|
||||
|
||||
log.Infof("NewParallelExecutor: Creating %d workers for %d executors", parallel, len(executors))
|
||||
logging.Infof("NewParallelExecutor: Creating %d workers for %d executors", parallel, len(executors))
|
||||
|
||||
for i := 0; i < parallel; i++ {
|
||||
go func(workerID int, work <-chan Executor, errs chan<- error) {
|
||||
log.Debugf("Worker %d started", workerID)
|
||||
logging.Debugf("Worker %d started", workerID)
|
||||
taskCount := 0
|
||||
for executor := range work {
|
||||
taskCount++
|
||||
log.Debugf("Worker %d executing task %d", workerID, taskCount)
|
||||
logging.Debugf("Worker %d executing task %d", workerID, taskCount)
|
||||
// Recover from panics in executors to avoid crashing the worker
|
||||
// goroutine which would leave the runner process hung.
|
||||
// https://gitea.com/gitea/runner/issues/371
|
||||
errs <- func() (err error) {
|
||||
defer func() {
|
||||
if r := recover(); r != nil {
|
||||
log.Errorf("panic in executor: %v\n%s", r, debug.Stack())
|
||||
logging.Errorf("panic in executor: %v\n%s", r, debug.Stack())
|
||||
err = fmt.Errorf("panic: %v", r)
|
||||
}
|
||||
}()
|
||||
return executor(ctx)
|
||||
}()
|
||||
}
|
||||
log.Debugf("Worker %d finished (%d tasks executed)", workerID, taskCount)
|
||||
logging.Debugf("Worker %d finished (%d tasks executed)", workerID, taskCount)
|
||||
}(i, work, errs)
|
||||
}
|
||||
|
||||
|
||||
@@ -3,7 +3,11 @@
|
||||
|
||||
package logging
|
||||
|
||||
import "github.com/sirupsen/logrus"
|
||||
import (
|
||||
"io"
|
||||
|
||||
"github.com/sirupsen/logrus"
|
||||
)
|
||||
|
||||
var std = New(NewLogrusHandler(logrus.StandardLogger()))
|
||||
|
||||
@@ -16,6 +20,12 @@ func SetLevel(level Level) { logrus.SetLevel(toLogrusLevel(level)) }
|
||||
// GetLevel returns the minimum level of the process-wide Logger.
|
||||
func GetLevel() Level { return fromLogrusLevel(logrus.GetLevel()) }
|
||||
|
||||
// SetOutput sets where the process-wide Logger writes.
|
||||
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 With(args ...any) *Logger { return std.With(args...) }
|
||||
|
||||
@@ -100,13 +100,13 @@ func TestCallerPC(t *testing.T) {
|
||||
}
|
||||
|
||||
std := logrus.StandardLogger()
|
||||
prevOut, prevCaller, prevHooks := std.Out, std.ReportCaller, std.ReplaceHooks(logrus.LevelHooks{})
|
||||
prevOut, prevCaller, prevHooks := logging.Output(), std.ReportCaller, std.ReplaceHooks(logrus.LevelHooks{})
|
||||
t.Cleanup(func() {
|
||||
std.SetOutput(prevOut)
|
||||
logging.SetOutput(prevOut)
|
||||
std.SetReportCaller(prevCaller)
|
||||
std.ReplaceHooks(prevHooks)
|
||||
})
|
||||
std.SetOutput(io.Discard)
|
||||
logging.SetOutput(io.Discard)
|
||||
std.SetReportCaller(true)
|
||||
hook := logrustest.NewGlobal()
|
||||
logging.Infof("global")
|
||||
|
||||
Reference in new issue
Block a user