summaryrefslogtreecommitdiffstats
path: root/pkg/common/logger.go
diff options
context:
space:
mode:
authorDaniel Baumann <daniel@debian.org>2024-10-20 23:07:42 +0200
committerDaniel Baumann <daniel@debian.org>2024-11-09 15:38:42 +0100
commit714c83b2736d7e308bc33c49057952490eb98be2 (patch)
tree1d9ba7035798368569cd49056f4d596efc908cd8 /pkg/common/logger.go
parentInitial commit. (diff)
downloadforgejo-act-upstream.tar.xz
forgejo-act-upstream.zip
Adding upstream version 1.21.4.HEADupstream/1.21.4upstreamdebian
Signed-off-by: Daniel Baumann <daniel@debian.org>
Diffstat (limited to 'pkg/common/logger.go')
-rw-r--r--pkg/common/logger.go48
1 files changed, 48 insertions, 0 deletions
diff --git a/pkg/common/logger.go b/pkg/common/logger.go
new file mode 100644
index 0000000..74fc96d
--- /dev/null
+++ b/pkg/common/logger.go
@@ -0,0 +1,48 @@
+package common
+
+import (
+ "context"
+
+ "github.com/sirupsen/logrus"
+)
+
+type loggerContextKey string
+
+const loggerContextKeyVal = loggerContextKey("logrus.FieldLogger")
+
+// Logger returns the appropriate logger for current context
+func Logger(ctx context.Context) logrus.FieldLogger {
+ val := ctx.Value(loggerContextKeyVal)
+ if val != nil {
+ if logger, ok := val.(logrus.FieldLogger); ok {
+ return logger
+ }
+ }
+ return logrus.StandardLogger()
+}
+
+// WithLogger adds a value to the context for the logger
+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)
+}