-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
log/slog: add LogLoggerLevel to enable setting level on the default l…
…ogger Fixes golang#62418 Change-Id: I889a53d00c8a463b4d7ddb41893c000d7cd0e7b8 Reviewed-on: https://go-review.googlesource.com/c/go/+/525096 LUCI-TryBot-Result: Go LUCI <[email protected]> Reviewed-by: Heschi Kreinick <[email protected]> Reviewed-by: Jonathan Amsterdam <[email protected]>
- Loading branch information
Showing
5 changed files
with
163 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
pkg log/slog, func SetLogLoggerLevel(Level) Level #62418 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
// Copyright 2023 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 ( | ||
"log" | ||
"log/slog" | ||
"log/slog/internal/slogtest" | ||
"os" | ||
) | ||
|
||
// This example shows how to use slog.SetLogLoggerLevel to change the minimal level | ||
// of the internal default handler for slog package before calling slog.SetDefault. | ||
func ExampleSetLogLoggerLevel_log() { | ||
defer log.SetFlags(log.Flags()) // revert changes after the example | ||
log.SetFlags(0) | ||
defer log.SetOutput(log.Writer()) // revert changes after the example | ||
log.SetOutput(os.Stdout) | ||
|
||
// Default logging level is slog.LevelInfo. | ||
log.Print("log debug") // log debug | ||
slog.Debug("debug") // no output | ||
slog.Info("info") // INFO info | ||
|
||
// Set the default logging level to slog.LevelDebug. | ||
currentLogLevel := slog.SetLogLoggerLevel(slog.LevelDebug) | ||
defer slog.SetLogLoggerLevel(currentLogLevel) // revert changes after the example | ||
|
||
log.Print("log debug") // log debug | ||
slog.Debug("debug") // DEBUG debug | ||
slog.Info("info") // INFO info | ||
|
||
// Output: | ||
// log debug | ||
// INFO info | ||
// log debug | ||
// DEBUG debug | ||
// INFO info | ||
} | ||
|
||
// This example shows how to use slog.SetLogLoggerLevel to change the minimal level | ||
// of the internal writer that uses the custom handler for log package after | ||
// calling slog.SetDefault. | ||
func ExampleSetLogLoggerLevel_slog() { | ||
// Set the default logging level to slog.LevelError. | ||
currentLogLevel := slog.SetLogLoggerLevel(slog.LevelError) | ||
defer slog.SetLogLoggerLevel(currentLogLevel) // revert changes after the example | ||
|
||
defer slog.SetDefault(slog.Default()) // revert changes after the example | ||
slog.SetDefault(slog.New(slog.NewTextHandler(os.Stdout, &slog.HandlerOptions{ReplaceAttr: slogtest.RemoveTime}))) | ||
|
||
log.Print("error") // level=ERROR msg=error | ||
|
||
// Output: | ||
// level=ERROR msg=error | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters