From takes a context and reads out a *slog.Logger. If From does not find a value it will return a discarding logger similar to log-format "none".
(ctx context.Context)
| 214 | // From takes a context and reads out a *slog.Logger. If From does not find a value it will return a discarding logger |
| 215 | // similar to log-format "none". |
| 216 | func From(ctx context.Context) *slog.Logger { |
| 217 | // Check that we have a ctx |
| 218 | if ctx == nil { |
| 219 | return newDiscard() |
| 220 | } |
| 221 | // Grab value from key |
| 222 | log := ctx.Value(defaultCtxKey) |
| 223 | if log == nil { |
| 224 | return newDiscard() |
| 225 | } |
| 226 | |
| 227 | // Ensure our value is a *slog.Logger before we cast. |
| 228 | switch l := log.(type) { |
| 229 | case *slog.Logger: |
| 230 | return l |
| 231 | default: |
| 232 | // Not reached |
| 233 | panic(fmt.Sprintf("unexpected value type on context key: %T", log)) |
| 234 | } |
| 235 | } |
| 236 | |
| 237 | // newDiscard returns a logger without any settings that goes to io.Discard |
| 238 | func newDiscard() *slog.Logger { |