X7ROOT File Manager
Current Path:
/opt/golang/1.22.0/src/log/slog
opt
/
golang
/
1.22.0
/
src
/
log
/
slog
/
📁
..
📄
attr.go
(2.49 KB)
📄
attr_test.go
(891 B)
📄
doc.go
(12.13 KB)
📄
example_custom_levels_test.go
(2.99 KB)
📄
example_level_handler_test.go
(2.18 KB)
📄
example_log_level_test.go
(1.85 KB)
📄
example_logvaluer_group_test.go
(825 B)
📄
example_logvaluer_secret_test.go
(906 B)
📄
example_test.go
(857 B)
📄
example_wrap_test.go
(1.34 KB)
📄
handler.go
(17.48 KB)
📄
handler_test.go
(19.61 KB)
📁
internal
📄
json_handler.go
(8.05 KB)
📄
json_handler_test.go
(6.48 KB)
📄
level.go
(5.64 KB)
📄
level_test.go
(3.75 KB)
📄
logger.go
(10.22 KB)
📄
logger_test.go
(19.48 KB)
📄
record.go
(5.98 KB)
📄
record_test.go
(4 KB)
📄
slogtest_test.go
(2.61 KB)
📄
text_handler.go
(4.71 KB)
📄
text_handler_test.go
(3.81 KB)
📄
value.go
(12.8 KB)
📄
value_access_benchmark_test.go
(4.29 KB)
📄
value_test.go
(6.08 KB)
Editing: example_level_handler_test.go
// Copyright 2022 The Go Authors. All rights reserved. // Use of this source code is governed by a BSD-style // license that can be found in the LICENSE file. package slog_test import ( "context" "log/slog" "log/slog/internal/slogtest" "os" ) // A LevelHandler wraps a Handler with an Enabled method // that returns false for levels below a minimum. type LevelHandler struct { level slog.Leveler handler slog.Handler } // NewLevelHandler returns a LevelHandler with the given level. // All methods except Enabled delegate to h. func NewLevelHandler(level slog.Leveler, h slog.Handler) *LevelHandler { // Optimization: avoid chains of LevelHandlers. if lh, ok := h.(*LevelHandler); ok { h = lh.Handler() } return &LevelHandler{level, h} } // Enabled implements Handler.Enabled by reporting whether // level is at least as large as h's level. func (h *LevelHandler) Enabled(_ context.Context, level slog.Level) bool { return level >= h.level.Level() } // Handle implements Handler.Handle. func (h *LevelHandler) Handle(ctx context.Context, r slog.Record) error { return h.handler.Handle(ctx, r) } // WithAttrs implements Handler.WithAttrs. func (h *LevelHandler) WithAttrs(attrs []slog.Attr) slog.Handler { return NewLevelHandler(h.level, h.handler.WithAttrs(attrs)) } // WithGroup implements Handler.WithGroup. func (h *LevelHandler) WithGroup(name string) slog.Handler { return NewLevelHandler(h.level, h.handler.WithGroup(name)) } // Handler returns the Handler wrapped by h. func (h *LevelHandler) Handler() slog.Handler { return h.handler } // This example shows how to Use a LevelHandler to change the level of an // existing Handler while preserving its other behavior. // // This example demonstrates increasing the log level to reduce a logger's // output. // // Another typical use would be to decrease the log level (to LevelDebug, say) // during a part of the program that was suspected of containing a bug. func ExampleHandler_levelHandler() { th := slog.NewTextHandler(os.Stdout, &slog.HandlerOptions{ReplaceAttr: slogtest.RemoveTime}) logger := slog.New(NewLevelHandler(slog.LevelWarn, th)) logger.Info("not printed") logger.Warn("printed") // Output: // level=WARN msg=printed }
Upload File
Create Folder