https://github.com/func25/batchlog
https://github.com/func25/batchlog
Last synced: 5 months ago
JSON representation
- Host: GitHub
- URL: https://github.com/func25/batchlog
- Owner: func25
- License: mit
- Created: 2022-04-18T06:58:41.000Z (about 4 years ago)
- Default Branch: main
- Last Pushed: 2022-05-14T16:57:25.000Z (about 4 years ago)
- Last Synced: 2024-06-21T09:42:12.186Z (about 2 years ago)
- Language: Go
- Size: 35.2 KB
- Stars: 0
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# batchlog
batchlog is used to batch the logs (built on top of zerolog), avoid spamming
* [Installation](#installation)
* [Samples](#samples)
* [Batching](#batching)
* [Grouping](#grouping)
* [Status](#status-pre-release)
## Installation
`go get github.com/func25/batchlog`
## Samples
### Batching
Batch with three variables: "tokenId", "isBatch" and "message"
```go
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
```go
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")
}
```
```go
{"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