https://github.com/kyslik/gaglog
gaglog is a Go package that adds regex/duration based filtering to the standard library log package.
https://github.com/kyslik/gaglog
go log logging throttling
Last synced: 8 days ago
JSON representation
gaglog is a Go package that adds regex/duration based filtering to the standard library log package.
- Host: GitHub
- URL: https://github.com/kyslik/gaglog
- Owner: Kyslik
- License: mit
- Created: 2023-11-05T23:08:36.000Z (over 2 years ago)
- Default Branch: main
- Last Pushed: 2023-11-13T14:40:35.000Z (over 2 years ago)
- Last Synced: 2025-02-24T03:44:34.732Z (over 1 year ago)
- Topics: go, log, logging, throttling
- Language: Go
- Homepage:
- Size: 4.88 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# gaglog [](https://pkg.go.dev/github.com/kyslik/gaglog/v1) [](https://github.com/kyslik/gaglog/releases/latest) 
`gaglog` is a Go package that adds regex/duration based filtering to the standard library [`log`](https://pkg.go.dev/log) package.
This package was heavily influenced by [`hashicorp/logutils`](https://github.com/hashicorp/logutils) package.
## Simple example
Presumably your application already uses the default `log` package. To start throttling logs, you'll want your code to look like the following:
```go
package main
import (
"log"
"os"
"regexp"
"time"
"github.com/kyslik/gaglog"
)
func main() {
filter := &gaglog.GagFilter{
Writer: os.Stderr,
Gags: gaglog.Gags{
regexp.MustCompile("P([a-z]+)ch"): time.Millisecond * 100,
regexp.MustCompile("L([a-z]+)ch"): time.Millisecond * 1000,
regexp.MustCompile("F([a-z]+)ch"): time.Millisecond * 10000,
},
}
log.SetOutput(filter)
for i := 0; i < 1000000; i++ {
log.Print("Pinch")
log.Print("Lynch")
log.Print("Flinch")
if i % 100000 == 0 {
log.Print("Grinch")
}
}
time.Sleep(5 * time.Second)
log.Println("Clinch")
}
```
This logs to standard error exactly like Go's standard logger. Any log messages that are not matched by a regex, won't be gaged.