diff options
author | fuxiaohei <fuxiaohei@vip.qq.com> | 2022-07-29 09:43:42 +0200 |
---|---|---|
committer | Jason Song <i@wolfogre.com> | 2022-11-22 07:00:13 +0100 |
commit | 0fa1fe03109cb5e41aa7a074fe0d837089d65c1e (patch) | |
tree | 53504932e425273ed5430c32287754dab2431301 /pkg/common | |
parent | build(deps): bump github.com/spf13/cobra from 1.6.0 to 1.6.1 (#1420) (diff) | |
download | forgejo-act-0fa1fe03109cb5e41aa7a074fe0d837089d65c1e.tar.xz forgejo-act-0fa1fe03109cb5e41aa7a074fe0d837089d65c1e.zip |
feat: add logger hook for standalone job logger
Diffstat (limited to 'pkg/common')
-rw-r--r-- | pkg/common/logger.go | 21 |
1 files changed, 21 insertions, 0 deletions
diff --git a/pkg/common/logger.go b/pkg/common/logger.go index a9501ce..74fc96d 100644 --- a/pkg/common/logger.go +++ b/pkg/common/logger.go @@ -25,3 +25,24 @@ func Logger(ctx context.Context) logrus.FieldLogger { func WithLogger(ctx context.Context, logger logrus.FieldLogger) context.Context { return context.WithValue(ctx, loggerContextKeyVal, logger) } + +type loggerHookKey string + +const loggerHookKeyVal = loggerHookKey("logrus.Hook") + +// LoggerHook returns the appropriate logger hook for current context +// the hook affects job logger, not global logger +func LoggerHook(ctx context.Context) logrus.Hook { + val := ctx.Value(loggerHookKeyVal) + if val != nil { + if hook, ok := val.(logrus.Hook); ok { + return hook + } + } + return nil +} + +// WithLoggerHook adds a value to the context for the logger hook +func WithLoggerHook(ctx context.Context, hook logrus.Hook) context.Context { + return context.WithValue(ctx, loggerHookKeyVal, hook) +} |