Slogger returns a slog logger that is intended for use by the most recent module associated with the context.
()
| 610 | // Slogger returns a slog logger that is intended for use by |
| 611 | // the most recent module associated with the context. |
| 612 | func (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. |
nothing calls this directly
no test coverage detected