1// Copyright 2022 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 5package slog_test 6 7import ( 8 "log/slog" 9 "log/slog/internal/slogtest" 10 "os" 11) 12 13// A token is a secret value that grants permissions. 14type Token string 15 16// LogValue implements slog.LogValuer. 17// It avoids revealing the token. 18func (Token) LogValue() slog.Value { 19 return slog.StringValue("REDACTED_TOKEN") 20} 21 22// This example demonstrates a Value that replaces itself 23// with an alternative representation to avoid revealing secrets. 24func ExampleLogValuer_secret() { 25 t := Token("shhhh!") 26 logger := slog.New(slog.NewTextHandler(os.Stdout, &slog.HandlerOptions{ReplaceAttr: slogtest.RemoveTime})) 27 logger.Info("permission granted", "user", "Perry", "token", t) 28 29 // Output: 30 // level=INFO msg="permission granted" user=Perry token=REDACTED_TOKEN 31} 32