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_wrap_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" "fmt" "log/slog" "os" "path/filepath" "runtime" "time" ) // Infof is an example of a user-defined logging function that wraps slog. // The log record contains the source position of the caller of Infof. func Infof(logger *slog.Logger, format string, args ...any) { if !logger.Enabled(context.Background(), slog.LevelInfo) { return } var pcs [1]uintptr runtime.Callers(2, pcs[:]) // skip [Callers, Infof] r := slog.NewRecord(time.Now(), slog.LevelInfo, fmt.Sprintf(format, args...), pcs[0]) _ = logger.Handler().Handle(context.Background(), r) } func Example_wrapping() { replace := func(groups []string, a slog.Attr) slog.Attr { // Remove time. if a.Key == slog.TimeKey && len(groups) == 0 { return slog.Attr{} } // Remove the directory from the source's filename. if a.Key == slog.SourceKey { source := a.Value.Any().(*slog.Source) source.File = filepath.Base(source.File) } return a } logger := slog.New(slog.NewTextHandler(os.Stdout, &slog.HandlerOptions{AddSource: true, ReplaceAttr: replace})) Infof(logger, "message, %s", "formatted") // Output: // level=INFO source=example_wrap_test.go:43 msg="message, formatted" }
Upload File
Create Folder