-
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.
Merge pull request #857 from devlights/add-slog-example
- Loading branch information
Showing
3 changed files
with
82 additions
and
0 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,11 @@ | ||
# https://taskfile.dev | ||
|
||
version: '3' | ||
|
||
tasks: | ||
default: | ||
cmds: | ||
- task: run | ||
run: | ||
cmds: | ||
- go run main.go |
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,70 @@ | ||
package main | ||
|
||
import ( | ||
"context" | ||
"log/slog" | ||
"os" | ||
) | ||
|
||
func main() { | ||
var ( | ||
rootCtx = context.Background() | ||
ctx, cxl = context.WithCancel(rootCtx) | ||
) | ||
defer cxl() | ||
|
||
if err := run(ctx); err != nil { | ||
slog.Error("A fatal error occurred", "err", err) | ||
os.Exit(1) | ||
} | ||
} | ||
|
||
func run(_ context.Context) error { | ||
// | ||
// slog.LevelVarを使用することで、実行時にログレベルを動的に変更できる. | ||
// | ||
// 以下、https://pkg.go.dev/log/slog@go1.23.2#hdr-Levels にある記載を抜粋 | ||
// | ||
// > Setting it to a LevelVar allows the level to be varied dynamically. | ||
// > A LevelVar holds a Level and is safe to read or write from multiple goroutines. | ||
// | ||
// >> LevelVar を設定することで、レベルを動的に変化させることができます。 | ||
// >> LevelVar は Level を保持し、複数のゴルーチンから安全に読み書きできます。 | ||
// | ||
|
||
var ( | ||
level = &slog.LevelVar{} | ||
opt = &slog.HandlerOptions{ | ||
Level: level, | ||
ReplaceAttr: noTimeKey, | ||
} | ||
handler = slog.NewTextHandler(os.Stdout, opt) | ||
rootLogger = slog.New(handler) | ||
logger *slog.Logger | ||
) | ||
|
||
// デフォルトのレベルはINFO。なのでDEBUGは出力されない | ||
logger = rootLogger.With("idx", "1") | ||
logger.Debug("this is DEBUG level message") | ||
logger.Info("this is INFO level message") | ||
|
||
// レベルを変更 | ||
// 現実的な使い方だと、コマンドライン引数で -debug を受け取った場合に | ||
// DEBUGレベルを有効にするなどの使い方が出来る。 | ||
level.Set(slog.LevelDebug) | ||
|
||
// デフォルトのレベルがDEBUGに変更された | ||
logger = rootLogger.With("idx", "2") | ||
logger.Debug("this is DEBUG level message") | ||
logger.Info("this is INFO level message") | ||
|
||
return nil | ||
} | ||
|
||
func noTimeKey(groups []string, a slog.Attr) slog.Attr { | ||
if a.Key == slog.TimeKey { | ||
return slog.Attr{} | ||
} | ||
|
||
return a | ||
} |
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