How do I mask sensitive data using zerolog in Go?

Masking sensitive data is crucial for maintaining data privacy and security, especially when logging information in applications. In Go, you can utilize the zerolog library to efficiently log events while masking sensitive data such as passwords, credit card numbers, or personal identification details.

Below is an example demonstrating how to mask sensitive data using zerolog in Go:

package main import ( "os" "github.com/rs/zerolog" "github.com/rs/zerolog/log" ) func main() { // Initialize zerolog zerolog.TimeFieldFormat = zerolog.TimeFormatUnix logger := zerolog.New(os.Stdout).With().Timestamp().Logger() username := "user123" password := "superSecretPass!" creditCard := "1234-5678-9012-3456" // Masking sensitive data logger.Info(). Str("username", username). Str("password", maskString(password)). Str("credit_card", maskString(creditCard)). Msg("User logged in") } // function to mask a string func maskString(s string) string { return "****" // Masked representation }

Go zerolog data masking sensitive data logging secure logging