]> Cypherpunks.ru repositories - gostls13.git/blob - src/log/slog/example_custom_levels_test.go
log/slog: initial commit
[gostls13.git] / src / log / slog / example_custom_levels_test.go
1 // Copyright 2023 The Go Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style
3 // license that can be found in the LICENSE file.
4
5 package slog_test
6
7 import (
8         "log/slog"
9         "os"
10 )
11
12 // This example demonstrates using custom log levels and custom log level names.
13 // In addition to the default log levels, it introduces Trace, Notice, and
14 // Emergency levels. The ReplaceAttr changes the way levels are printed for both
15 // the standard log levels and the custom log levels.
16 func ExampleHandlerOptions_customLevels() {
17         // Exported constants from a custom logging package.
18         const (
19                 LevelTrace     = slog.Level(-8)
20                 LevelDebug     = slog.LevelDebug
21                 LevelInfo      = slog.LevelInfo
22                 LevelNotice    = slog.Level(2)
23                 LevelWarning   = slog.LevelWarn
24                 LevelError     = slog.LevelError
25                 LevelEmergency = slog.Level(12)
26         )
27
28         th := slog.HandlerOptions{
29                 // Set a custom level to show all log output. The default value is
30                 // LevelInfo, which would drop Debug and Trace logs.
31                 Level: LevelTrace,
32
33                 ReplaceAttr: func(groups []string, a slog.Attr) slog.Attr {
34                         // Remove time from the output for predictable test output.
35                         if a.Key == slog.TimeKey {
36                                 return slog.Attr{}
37                         }
38
39                         // Customize the name of the level key and the output string, including
40                         // custom level values.
41                         if a.Key == slog.LevelKey {
42                                 // Rename the level key from "level" to "sev".
43                                 a.Key = "sev"
44
45                                 // Handle custom level values.
46                                 level := a.Value.Any().(slog.Level)
47
48                                 // This could also look up the name from a map or other structure, but
49                                 // this demonstrates using a switch statement to rename levels. For
50                                 // maximum performance, the string values should be constants, but this
51                                 // example uses the raw strings for readability.
52                                 switch {
53                                 case level < LevelDebug:
54                                         a.Value = slog.StringValue("TRACE")
55                                 case level < LevelInfo:
56                                         a.Value = slog.StringValue("DEBUG")
57                                 case level < LevelNotice:
58                                         a.Value = slog.StringValue("INFO")
59                                 case level < LevelWarning:
60                                         a.Value = slog.StringValue("NOTICE")
61                                 case level < LevelError:
62                                         a.Value = slog.StringValue("WARNING")
63                                 case level < LevelEmergency:
64                                         a.Value = slog.StringValue("ERROR")
65                                 default:
66                                         a.Value = slog.StringValue("EMERGENCY")
67                                 }
68                         }
69
70                         return a
71                 },
72         }.NewTextHandler(os.Stdout)
73
74         logger := slog.New(th)
75         logger.Log(nil, LevelEmergency, "missing pilots")
76         logger.Error("failed to start engines", "err", "missing fuel")
77         logger.Warn("falling back to default value")
78         logger.Log(nil, LevelNotice, "all systems are running")
79         logger.Info("initiating launch")
80         logger.Debug("starting background job")
81         logger.Log(nil, LevelTrace, "button clicked")
82
83         // Output:
84         // sev=EMERGENCY msg="missing pilots"
85         // sev=ERROR msg="failed to start engines" err="missing fuel"
86         // sev=WARNING msg="falling back to default value"
87         // sev=NOTICE msg="all systems are running"
88         // sev=INFO msg="initiating launch"
89         // sev=DEBUG msg="starting background job"
90         // sev=TRACE msg="button clicked"
91 }