github.com/shogo82148/std@v1.22.1-0.20240327122250-4e474527810c/log/slog/example_custom_levels_test.go (about) 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 "github.com/shogo82148/std/context" 9 "github.com/shogo82148/std/log/slog" 10 "github.com/shogo82148/std/os" 11 ) 12 13 // This example demonstrates using custom log levels and custom log level names. 14 // In addition to the default log levels, it introduces Trace, Notice, and 15 // Emergency levels. The ReplaceAttr changes the way levels are printed for both 16 // the standard log levels and the custom log levels. 17 func ExampleHandlerOptions_customLevels() { 18 // カスタムログパッケージからエクスポートされた定数。 19 const ( 20 LevelTrace = slog.Level(-8) 21 LevelDebug = slog.LevelDebug 22 LevelInfo = slog.LevelInfo 23 LevelNotice = slog.Level(2) 24 LevelWarning = slog.LevelWarn 25 LevelError = slog.LevelError 26 LevelEmergency = slog.Level(12) 27 ) 28 29 th := slog.NewTextHandler(os.Stdout, &slog.HandlerOptions{ 30 // すべてのログ出力を表示するために、カスタムレベルを設定します。 31 // デフォルト値はLevelInfoであり、DebugとTraceログをドロップします。 32 Level: LevelTrace, 33 34 ReplaceAttr: func(groups []string, a slog.Attr) slog.Attr { 35 // 予測可能なテスト出力のために、出力から時間を削除します。 36 if a.Key == slog.TimeKey { 37 return slog.Attr{} 38 } 39 40 // レベルキーの名前と出力文字列をカスタマイズします。 41 // カスタムレベル値を含みます。 42 if a.Key == slog.LevelKey { 43 // レベルキーを "level" から "sev" に変更します。 44 a.Key = "sev" 45 46 // カスタムレベル値を処理します。 47 level := a.Value.Any().(slog.Level) 48 49 // これはマップや他の構造から名前を検索することもできますが、 50 // この例ではswitch文を使用してレベルをリネームする方法を示しています。 51 // 文字列値は定数であるべきですが、可読性のためにこの例では生の文字列を使用しています。 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 }) 73 74 logger := slog.New(th) 75 ctx := context.Background() 76 logger.Log(ctx, LevelEmergency, "missing pilots") 77 logger.Error("failed to start engines", "err", "missing fuel") 78 logger.Warn("falling back to default value") 79 logger.Log(ctx, LevelNotice, "all systems are running") 80 logger.Info("initiating launch") 81 logger.Debug("starting background job") 82 logger.Log(ctx, LevelTrace, "button clicked") 83 84 // Output: 85 // sev=EMERGENCY msg="missing pilots" 86 // sev=ERROR msg="failed to start engines" err="missing fuel" 87 // sev=WARNING msg="falling back to default value" 88 // sev=NOTICE msg="all systems are running" 89 // sev=INFO msg="initiating launch" 90 // sev=DEBUG msg="starting background job" 91 // sev=TRACE msg="button clicked" 92 }