summaryrefslogtreecommitdiffstats
path: root/vendor/github.com/rs/zerolog/ctx.go
diff options
context:
space:
mode:
authormsglm <msglm@techchud.xyz>2023-10-27 07:08:25 -0500
committermsglm <msglm@techchud.xyz>2023-10-27 07:08:25 -0500
commit032a7e0c1188d3507b8d9a9571f2446a43cf775b (patch)
tree2bd38c01bc7761a6195e426082ce7191ebc765a1 /vendor/github.com/rs/zerolog/ctx.go
parent56e7bd01ca09ad52b0c4f48f146a20a4f1b78696 (diff)
downloadmatterbridge-msglm-1.26.0+0.1.0.tar.gz
matterbridge-msglm-1.26.0+0.1.0.tar.bz2
matterbridge-msglm-1.26.0+0.1.0.zip
apply https://github.com/42wim/matterbridge/pull/1864v1.26.0+0.1.0
Diffstat (limited to 'vendor/github.com/rs/zerolog/ctx.go')
-rw-r--r--vendor/github.com/rs/zerolog/ctx.go52
1 files changed, 52 insertions, 0 deletions
diff --git a/vendor/github.com/rs/zerolog/ctx.go b/vendor/github.com/rs/zerolog/ctx.go
new file mode 100644
index 00000000..60432d15
--- /dev/null
+++ b/vendor/github.com/rs/zerolog/ctx.go
@@ -0,0 +1,52 @@
+package zerolog
+
+import (
+ "context"
+)
+
+var disabledLogger *Logger
+
+func init() {
+ SetGlobalLevel(TraceLevel)
+ l := Nop()
+ disabledLogger = &l
+}
+
+type ctxKey struct{}
+
+// WithContext returns a copy of ctx with the receiver attached. The Logger
+// attached to the provided Context (if any) will not be effected. If the
+// receiver's log level is Disabled it will only be attached to the returned
+// Context if the provided Context has a previously attached Logger. If the
+// provided Context has no attached Logger, a Disabled Logger will not be
+// attached.
+//
+// Note: to modify the existing Logger attached to a Context (instead of
+// replacing it in a new Context), use UpdateContext with the following
+// notation:
+//
+// ctx := r.Context()
+// l := zerolog.Ctx(ctx)
+// l.UpdateContext(func(c Context) Context {
+// return c.Str("bar", "baz")
+// })
+//
+func (l Logger) WithContext(ctx context.Context) context.Context {
+ if _, ok := ctx.Value(ctxKey{}).(*Logger); !ok && l.level == Disabled {
+ // Do not store disabled logger.
+ return ctx
+ }
+ return context.WithValue(ctx, ctxKey{}, &l)
+}
+
+// Ctx returns the Logger associated with the ctx. If no logger
+// is associated, DefaultContextLogger is returned, unless DefaultContextLogger
+// is nil, in which case a disabled logger is returned.
+func Ctx(ctx context.Context) *Logger {
+ if l, ok := ctx.Value(ctxKey{}).(*Logger); ok {
+ return l
+ } else if l = DefaultContextLogger; l != nil {
+ return l
+ }
+ return disabledLogger
+}