[log] add a sampledLogger to utils

Signed-off-by: Leo Chen <leo@harmony.one>
pull/3179/head
Leo Chen 5 years ago
parent c372ab9fa1
commit 5602a6c7c7
  1. 21
      internal/utils/singleton.go

@ -31,6 +31,9 @@ var (
// ZeroLog // ZeroLog
zeroLogger *zerolog.Logger zeroLogger *zerolog.Logger
zeroLoggerLevel = zerolog.Disabled zeroLoggerLevel = zerolog.Disabled
onceForSampleLogger sync.Once
sampledLogger *zerolog.Logger
) )
// SetLogContext used to print out loggings of node with port and ip. // SetLogContext used to print out loggings of node with port and ip.
@ -174,6 +177,24 @@ func Logger() *zerolog.Logger {
return zeroLogger return zeroLogger
} }
// SampledLogger returns a sampled zerolog singleton to be used in criticial path like p2p message handling
func SampledLogger() *zerolog.Logger {
// Will let 3 log messages per period of 1 second.
// Over 3 log messages, 1 every 100 messages are logged.
onceForSampleLogger.Do(func() {
sLog := zeroLogger.Sample(
&zerolog.BurstSampler{
Burst: 3,
Period: 1 * time.Second,
NextSampler: &zerolog.BasicSampler{N: 100},
},
)
sampledLogger = &sLog
})
return sampledLogger
}
func updateZeroLogLevel(level int) { func updateZeroLogLevel(level int) {
switch level { switch level {
case 0: case 0:

Loading…
Cancel
Save