Skip to content

Commit

Permalink
Merge pull request #857 from devlights/add-slog-example
Browse files Browse the repository at this point in the history
  • Loading branch information
devlights authored Oct 4, 2024
2 parents dd7a905 + 3f02d77 commit aa221f2
Show file tree
Hide file tree
Showing 3 changed files with 82 additions and 0 deletions.
11 changes: 11 additions & 0 deletions examples/slog/07.dynamic-level-change/Taskfile.yml
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
70 changes: 70 additions & 0 deletions examples/slog/07.dynamic-level-change/main.go
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
}
1 change: 1 addition & 0 deletions examples/slog/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ Go 1.21 にて導入された ```log/slog``` パッケージについてのサ
- [A Guide to Writing slog Handlers](https://github.com/golang/example/blob/master/slog-handler-guide/README.md)
- [awesome-slog](https://github.com/go-slog/awesome-slog)
- [Goのslog使い方まとめ](https://qiita.com/Imamotty/items/3fbe8ce6da4f1a653fae)
- [Go1.21で登場したlog/slogパッケージのパフォーマンスを徹底解説!!](https://zenn.dev/kkkxxx/articles/0843dc3b42ac7f)
- [Go1.21 log/slogパッケージ超入門](https://zenn.dev/88888888_kota/articles/7e97ff874083cf)
- [Go公式の構造化ロガー(として提案されている)slogを触ってみたメモ](https://zenn.dev/mizutani/articles/golang-exp-slog)
- [slog を触る(Group, Context)](https://zenn.dev/kyoshigai/articles/bc90cc776dea2c#context)
Expand Down

0 comments on commit aa221f2

Please sign in to comment.