mirror of
https://gitea.com/gitea/act_runner.git
synced 2026-10-29 11:26:31 +08:00
Add `act/common/logging`: a printf-style `Logger` on a slog-shaped `Handler`, with a logrus-backed handler as the only backend, plus a `logtest` handler for tests. Move the job logger onto it: `common.Logger` returns `*logging.Logger`, the job log formatters and the `Reporter` hook become handlers, and composite step IDs travel in the context. Logging is tightly coupled to logrus types (hooks, formatters, `Entry.Data`). Going through our own interface first lets the remaining call sites move over with unchanged behavior, and lets the backend be swapped for `log/slog` in one place afterwards. The global process logger and its masking formatter stay on logrus for now. Assisted-by: Codet Co-authored-by: bircni <bircni@icloud.com> Reviewed-on: https://gitea.com/gitea/runner/pulls/1295 Reviewed-by: bircni <bircni@icloud.com>
54 lines
1.1 KiB
Go
54 lines
1.1 KiB
Go
// Copyright 2026 The Gitea Authors. All rights reserved.
|
|
// SPDX-License-Identifier: MIT
|
|
|
|
package logging
|
|
|
|
import (
|
|
"fmt"
|
|
"strings"
|
|
)
|
|
|
|
// Level is a log severity. The values match log/slog, with Trace added below Debug.
|
|
type Level int
|
|
|
|
const (
|
|
LevelTrace Level = -8
|
|
LevelDebug Level = -4
|
|
LevelInfo Level = 0
|
|
LevelWarn Level = 4
|
|
LevelError Level = 8
|
|
)
|
|
|
|
func (l Level) String() string {
|
|
switch {
|
|
case l <= LevelTrace:
|
|
return "trace"
|
|
case l <= LevelDebug:
|
|
return "debug"
|
|
case l <= LevelInfo:
|
|
return "info"
|
|
case l <= LevelWarn:
|
|
return "warn"
|
|
default:
|
|
return "error"
|
|
}
|
|
}
|
|
|
|
// ParseLevel accepts every name logrus does. "fatal" and "panic" map to LevelError because
|
|
// the Logger has no level above it.
|
|
func ParseLevel(s string) (Level, error) {
|
|
switch strings.ToLower(s) {
|
|
case "trace":
|
|
return LevelTrace, nil
|
|
case "debug":
|
|
return LevelDebug, nil
|
|
case "info":
|
|
return LevelInfo, nil
|
|
case "warn", "warning":
|
|
return LevelWarn, nil
|
|
case "error", "fatal", "panic":
|
|
return LevelError, nil
|
|
}
|
|
return LevelInfo, fmt.Errorf("not a valid log level: %q", s)
|
|
}
|