Skip to content

func25/batchlog

Folders and files

NameName
Last commit message
Last commit date

Latest commit

ec2aa94 · May 14, 2022

History

17 Commits
May 14, 2022
Apr 22, 2022
May 3, 2022
May 14, 2022
May 14, 2022
Apr 22, 2022
Apr 22, 2022
Apr 22, 2022
Apr 19, 2022
Apr 19, 2022
May 14, 2022
May 14, 2022

Repository files navigation

batchlog

batchlog is used to batch the logs (built on top of zerolog), avoid spamming

Installation

go get github.com/func25/batchlog

Samples

Batching

Batch with three variables: "tokenId", "isBatch" and "message"

logger := batchlog.NewLogger()
logger.Debug().BatchStr("tokenId", "1").BatchBool("isBatch", false).BatchMsg("hello")
logger.Debug().BatchStr("tokenId", "1").BatchMsg("hello")
logger.Debug().BatchStr("tokenId", "2").BatchMsg("hello")
logger.Debug().BatchStr("tokenId", "2").BatchMsg("hello")
logger.Debug().BatchStr("tokenId", "2").BatchMsg("hello")
logger.Debug().BatchStr("tokenId", "2").BatchMsg("hellok")
logger.Debug().BatchStr("tokenId", "2").BatchMsg("hello")
logger.Error().BatchStr("tokenId", "2").BatchMsg("hello")
logger.Debug().BatchStr("tokenId", "2").BatchMsg("hello")
logger.Debug().BatchStr("tokenId", "2").BatchMsg("hello")
logger.Info().BatchStr("tokenId", "2").BatchMsg("hello")

The result will be:

{"level":"info","tokenId":"2","message":"hello","__repeat":1}
{"level":"error","tokenId":"2","message":"hello","__repeat":1}
{"level":"debug","tokenId":"2","message":"hellok","__repeat":1}
{"level":"debug","tokenId":"2","message":"hello","__repeat":6}
{"level":"debug","tokenId":"1","message":"hello","__repeat":1}
{"level":"debug","tokenId":"1","isBatch":false,"message":"hello","__repeat":1}

Grouping

This sample will batch first 20 messages in 1 batch and also create group of ids for the log

logger := batchlog.NewLogger(batchlog.OptTimeout(time.Hour))
for i := 0; i < 30; i++ {
	time.Sleep(1500 * time.Millisecond)
	logger.Debug().BatchStr("tokenID", "123456").GroupInt("id", i).BatchMsg("hello")
}
{"level":"debug","tokenID":"123456","message":"hello","__repeat":20,"id":["0","1","2","3","4","5","6","7","8","9","10","11","12","13","14","15","16","17","18","19"]}
{"level":"debug","tokenID":"123456","message":"hello","__repeat":10,"id":["20","21","22","23","24","25","26","27","28","29"]}

Status: pre-release

This lib is under developing, please notice when using it

About

No description, website, or topics provided.

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages