github.com/prebid/prebid-server/v2@v2.18.0/config/util/loggers.go (about)

     1  package util
     2  
     3  import (
     4  	"math/rand"
     5  )
     6  
     7  type LogMsg func(string, ...interface{})
     8  
     9  type randomGenerator func() float32
    10  
    11  // LogRandomSample will log a randam sample of the messages it is sent, based on the chance to log
    12  // chance = 1.0 => always log,
    13  // chance = 0.0 => never log
    14  func LogRandomSample(msg string, logger LogMsg, chance float32) {
    15  	logRandomSampleImpl(msg, logger, chance, rand.Float32)
    16  }
    17  
    18  func logRandomSampleImpl(msg string, logger LogMsg, chance float32, randGenerator randomGenerator) {
    19  	if chance < 1.0 && randGenerator() > chance {
    20  		// this is the chance we don't log anything
    21  		return
    22  	}
    23  	logger(msg)
    24  }