mirror of
https://gitea.com/gitea/runner.git
synced 2026-11-04 14:35:32 +00: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 <[email protected]> Reviewed-on: https://gitea.com/gitea/runner/pulls/1295 Reviewed-by: bircni <[email protected]>
60 lines
1.8 KiB
Go
60 lines
1.8 KiB
Go
// Copyright 2022 The Gitea Authors. All rights reserved.
|
|
// Copyright 2020 The nektos/act Authors. All rights reserved.
|
|
// SPDX-License-Identifier: MIT
|
|
|
|
package common
|
|
|
|
import (
|
|
"context"
|
|
|
|
"gitea.com/gitea/runner/act/common/logging"
|
|
)
|
|
|
|
type loggerContextKey string
|
|
|
|
const loggerContextKeyVal = loggerContextKey("logging.Logger")
|
|
|
|
// Logger returns the appropriate logger for current context
|
|
func Logger(ctx context.Context) *logging.Logger {
|
|
if logger, ok := ctx.Value(loggerContextKeyVal).(*logging.Logger); ok {
|
|
return logger
|
|
}
|
|
return logging.Default()
|
|
}
|
|
|
|
// WithLogger adds a value to the context for the logger
|
|
func WithLogger(ctx context.Context, logger *logging.Logger) context.Context {
|
|
return context.WithValue(ctx, loggerContextKeyVal, logger)
|
|
}
|
|
|
|
type loggerHandlerKey string
|
|
|
|
const loggerHandlerKeyVal = loggerHandlerKey("logging.Handler")
|
|
|
|
// LoggerHandler returns the extra handler the job logger sends its records to, or nil.
|
|
// It affects the job logger, not the global logger.
|
|
func LoggerHandler(ctx context.Context) logging.Handler {
|
|
if h, ok := ctx.Value(loggerHandlerKeyVal).(logging.Handler); ok {
|
|
return h
|
|
}
|
|
return nil
|
|
}
|
|
|
|
// WithLoggerHandler adds the extra handler for the job logger to the context
|
|
func WithLoggerHandler(ctx context.Context, h logging.Handler) context.Context {
|
|
return context.WithValue(ctx, loggerHandlerKeyVal, h)
|
|
}
|
|
|
|
type stepIDsContextKey struct{}
|
|
|
|
// StepIDs returns the IDs of the step and the composite steps enclosing the current one, outermost first.
|
|
func StepIDs(ctx context.Context) []string {
|
|
ids, _ := ctx.Value(stepIDsContextKey{}).([]string)
|
|
return ids
|
|
}
|
|
|
|
// WithStepIDs replaces the step IDs in the context
|
|
func WithStepIDs(ctx context.Context, ids []string) context.Context {
|
|
return context.WithValue(ctx, stepIDsContextKey{}, ids)
|
|
}
|