0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

【Go】zapで機密情報をマスキングするサンプルコード

Posted at

概要

  • uber-go/zapを使用してパスワードの文字列をマスキングする処理のサンプルコード

サンプルコード

  • マスキングなし
func execNoMasking() {
	logger, _ := zap.NewProduction()
	defer logger.Sync()

	password := "secret123"

	logger.Info("user login",
		zap.String("username", "alice"),
		zap.String("password", password), // これだと機密情報が丸見えに
	)
}
// 出力されるログ
{"level":"info","ts":1748063559.381116,"caller":"zap/masking.go:13","msg":"user login","username":"alice","password":"secret123"}
  • マスキングあり
func maskSecret(_ string) string {
	return "****"
}


func execMasking() {
	logger, _ := zap.NewProduction()
	defer logger.Sync()

	username := "alice"
	password := "secret123"

	logger.Info("user login",
		zap.String("username", username),
		zap.String("password", maskSecret(password)), // ここでマスク
	)
}
// 出力
{"level":"info","ts":1748063559.381233,"caller":"zap/masking.go:31","msg":"user login","username":"alice","password":"****"}
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?