MCPcopy
hub / github.com/caddyserver/caddy / Slogger

Method Slogger

context.go:612–651  ·  view source on GitHub ↗

Slogger returns a slog logger that is intended for use by the most recent module associated with the context.

()

Source from the content-addressed store, hash-verified

610// Slogger returns a slog logger that is intended for use by
611// the most recent module associated with the context.
612func (ctx Context) Slogger() *slog.Logger {
613 var (
614 handler slog.Handler
615 core zapcore.Core
616 moduleID string
617 )
618
619 // the default enables traces at ERROR level, this disables
620 // them by setting it to a level higher than any other level
621 tracesOpt := zapslog.AddStacktraceAt(slog.Level(127))
622
623 if ctx.cfg == nil {
624 // often the case in tests; just use a dev logger
625 l, err := zap.NewDevelopment()
626 if err != nil {
627 panic("config missing, unable to create dev logger: " + err.Error())
628 }
629
630 core = l.Core()
631 handler = zapslog.NewHandler(core, tracesOpt)
632 } else {
633 mod := ctx.Module()
634 if mod == nil {
635 core = Log().Core()
636 handler = zapslog.NewHandler(core, tracesOpt)
637 } else {
638 moduleID = string(mod.CaddyModule().ID)
639 core = ctx.cfg.Logging.Logger(mod).Core()
640 handler = zapslog.NewHandler(core, zapslog.WithName(moduleID), tracesOpt)
641 }
642 }
643
644 slogHandlerFactoriesMu.RLock()
645 for _, f := range slogHandlerFactories {
646 handler = f(handler, core, moduleID)
647 }
648 slogHandlerFactoriesMu.RUnlock()
649
650 return slog.New(handler)
651}
652
653// Modules returns the lineage of modules that this context provisioned,
654// with the most recent/current module being last in the list.

Callers

nothing calls this directly

Calls 5

ModuleMethod · 0.95
LogFunction · 0.85
CaddyModuleMethod · 0.65
ErrorMethod · 0.45
LoggerMethod · 0.45

Tested by

no test coverage detected