https://github.com/moul/zapring
💍 In-memory RING buffer backend for the Zap logger
https://github.com/moul/zapring
golang in-memory logger logging ring-buffer zap
Last synced: 15 days ago
JSON representation
💍 In-memory RING buffer backend for the Zap logger
- Host: GitHub
- URL: https://github.com/moul/zapring
- Owner: moul
- License: other
- Created: 2021-03-17T17:58:37.000Z (about 4 years ago)
- Default Branch: master
- Last Pushed: 2025-03-24T18:37:36.000Z (about 1 month ago)
- Last Synced: 2025-04-14T19:11:59.982Z (15 days ago)
- Topics: golang, in-memory, logger, logging, ring-buffer, zap
- Language: Go
- Homepage: https://manfred.life/golang
- Size: 70.3 KB
- Stars: 6
- Watchers: 2
- Forks: 2
- Open Issues: 15
-
Metadata Files:
- Readme: README.md
- Contributing: .github/CONTRIBUTING.md
- Funding: .github/FUNDING.yml
- License: LICENSE-APACHE
- Code of conduct: .github/CODE_OF_CONDUCT.md
- Codeowners: .github/CODEOWNERS
- Security: SECURITY.md
- Support: .github/SUPPORT.md
- Authors: AUTHORS
Awesome Lists containing this project
README
# zapring
:smile: zapring
[](https://pkg.go.dev/moul.io/zapring)
[](https://github.com/moul/zapring/blob/master/COPYRIGHT)
[](https://github.com/moul/zapring/releases)
[](https://microbadger.com/images/moul/zapring)
[](https://manfred.life/)[](https://github.com/moul/zapring/actions?query=workflow%3AGo)
[](https://github.com/moul/zapring/actions?query=workflow%3ARelease)
[](https://github.com/moul/zapring/actions?query=workflow%3APR)
[](https://golangci.com/r/github.com/moul/zapring)
[](https://codecov.io/gh/moul/zapring)
[](https://goreportcard.com/report/moul.io/zapring)
[](https://www.codefactor.io/repository/github/moul/zapring)[](https://gitpod.io/#https://github.com/moul/zapring)
## Usage
[embedmd]:# (example_test.go /import\ / $)
```go
import (
"bufio"
"fmt"
"io"
"io/ioutil""go.uber.org/zap"
"go.uber.org/zap/zapcore"
"moul.io/zapring"
)func Example_custom() {
encoderConfig := zap.NewDevelopmentEncoderConfig()
encoderConfig.TimeKey = "" // used to make this test consistent (not depending on current timestamp)
encoder := zapcore.NewJSONEncoder(encoderConfig)
level := zap.LevelEnablerFunc(func(_ zapcore.Level) bool { return true })
ring := zapring.New(uint(10 * 1024 * 1024)) // 10Mb ring
defer ring.Close()
core := ring.
SetNextCore(zapcore.NewCore(encoder, zapcore.AddSync(ioutil.Discard), level)).
SetEncoder(encoder)
logger := zap.New(
core,
zap.Development(),
zap.AddCaller(),
)
defer logger.Sync()
logger.Info("hello world!")
logger.Info("lorem ipsum")r, w := io.Pipe()
go func() {
_, err := ring.WriteTo(w)
if err != nil && err != io.EOF {
panic(err)
}
w.Close()
}()
scanner := bufio.NewScanner(r)
lines := 0
for scanner.Scan() {
fmt.Println("--> ", scanner.Text())
lines++
if lines == 2 {
break
}
}// Output:
// --> {"L":"INFO","C":"zapring/example_test.go:30","M":"hello world!"}
// --> {"L":"INFO","C":"zapring/example_test.go:31","M":"lorem ipsum"}
}func Example_composite() {
cli := zap.NewExample()
cli.Info("hello cli!")
ring := zapring.New(10 * 1024 * 1024) // 10MB ring-buffer
encoderConfig := zap.NewDevelopmentEncoderConfig()
encoderConfig.TimeKey = "" // used to make this test consistent (not depending on current timestamp)
ring.SetEncoder(zapcore.NewJSONEncoder(encoderConfig))
// FIXME: ring.Info("hello ring!")
composite := zap.New(
zapcore.NewTee(cli.Core(), ring),
zap.Development(),
)
composite.Info("hello composite!")r, w := io.Pipe()
go func() {
_, err := ring.WriteTo(w)
if err != nil && err != io.EOF {
panic(err)
}
w.Close()
}()
composite.Info("hello composite 2!")
cli.Info("hello cli 2!")
composite.With(zap.String("foo", "bar")).Warn("warn composite!")
scanner := bufio.NewScanner(r)
lines := 0
for scanner.Scan() {
fmt.Println("-> ", scanner.Text())
lines++
if lines == 3 {
break
}
}// Output:
// {"level":"info","msg":"hello cli!"}
// {"level":"info","msg":"hello composite!"}
// {"level":"info","msg":"hello composite 2!"}
// {"level":"info","msg":"hello cli 2!"}
// {"level":"warn","msg":"warn composite!","foo":"bar"}
// -> {"L":"INFO","M":"hello composite!"}
// -> {"L":"INFO","M":"hello composite 2!"}
// -> {"L":"WARN","M":"warn composite!","foo":"bar"}
}func Example_simple() {
ring := zapring.New(10 * 1024 * 1024) // 10MB ring-buffer
logger := zap.New(ring, zap.Development())
logger.Info("test")
// Output:
}
```[embedmd]:# (.tmp/usage.txt txt /TYPES/ $)
```txt
TYPEStype Core struct {
zapcore.Core// Has unexported fields.
}
Core is an in-memory ring buffer log that implements zapcore.Core.func New(size uint) *Core
New returns a ring-buffer with a capacity of 'size' bytes.func (c *Core) Check(entry zapcore.Entry, checked *zapcore.CheckedEntry) *zapcore.CheckedEntry
Check implements zapcore.Core.func (c *Core) Close()
Close implements zapcore.Core.func (c *Core) Enabled(level zapcore.Level) bool
Enabled implements zapcore.LevelEnabler.func (c *Core) SetEncoder(enc zapcore.Encoder) *Core
func (c *Core) SetNextCore(core zapcore.Core) *Core
func (c *Core) Sync() error
Sync implements zapcore.Core.func (c *Core) With(fields []zapcore.Field) zapcore.Core
With implements zapcore.Core.func (c *Core) Write(entry zapcore.Entry, fields []zapcore.Field) error
Write implements zapcore.Core.func (c *Core) WriteTo(w io.Writer) (n int64, err error)
WriteTo implements io.WriterTo.```
## Install
### Using go
```sh
go get moul.io/zapring
```### Releases
See https://github.com/moul/zapring/releases
## Contribute

I really welcome contributions.
Your input is the most precious material.
I'm well aware of that and I thank you in advance.
Everyone is encouraged to look at what they can do on their own scale;
no effort is too small.Everything on contribution is sum up here: [CONTRIBUTING.md](./CONTRIBUTING.md)
### Contributors ✨
[](#contributors)
Thanks goes to these wonderful people ([emoji key](https://allcontributors.org/docs/en/emoji-key)):
Manfred Touron
🚧 📖 ⚠️ 💻
moul-bot
🚧
This project follows the [all-contributors](https://github.com/all-contributors/all-contributors)
specification. Contributions of any kind welcome!### Stargazers over time
[](https://starchart.cc/moul/zapring)
## License
© 2021 [Manfred Touron](https://manfred.life)
Licensed under the [Apache License, Version 2.0](https://www.apache.org/licenses/LICENSE-2.0)
([`LICENSE-APACHE`](LICENSE-APACHE)) or the [MIT license](https://opensource.org/licenses/MIT)
([`LICENSE-MIT`](LICENSE-MIT)), at your option.
See the [`COPYRIGHT`](COPYRIGHT) file for more details.`SPDX-License-Identifier: (Apache-2.0 OR MIT)`