README ¶

zapring

😄 zapring

go.dev reference License GitHub release Docker Metrics Made by Manfred Touron

Go Release PR GolangCI codecov Go Report Card CodeFactor

Gitpod ready-to-code

Usage

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:
}
TYPES

type 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
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

Contributors ✨

All Contributors

Thanks goes to these wonderful people (emoji key):


Manfred Touron

🚧 📖 ⚠️ 💻

moul-bot

🚧

This project follows the all-contributors specification. Contributions of any kind welcome!

Stargazers over time

Stargazers over time

License

© 2021 Manfred Touron

Licensed under the Apache License, Version 2.0 (LICENSE-APACHE) or the MIT license (LICENSE-MIT), at your option. See the COPYRIGHT file for more details.

SPDX-License-Identifier: (Apache-2.0 OR MIT)

Documentation ¶

Overview ¶

message from the author:

+--------------------------------------------------------------+
| * * * ░░░░░░░░░░░░░░░░░░░░  Hello  ░░░░░░░░░░░░░░░░░░░░░░░░░░|
+--------------------------------------------------------------+
|                                                              |
|     ++              ______________________________________   |
|     ++++           /                                      \  |
|      ++++          |                                      |  |
|    ++++++++++      |   Feel free to contribute to this    |  |
|   +++       |      |       project or contact me on       |  |
|   ++         |     |    manfred.life if you like this     |  |
|   +  -==   ==|     |               project!               |  |
|  (   <*>   <*>     |                                      |  |
|   |          |    /|                  :)                  |  |
|   |         _)   / |                                      |  |
|   |      +++    /  \______________________________________/  |
|    \      =+   /                                             |
|     \      +                                                 |
|     |\++++++                                                 |
|     |  ++++      ||//                                        |
|  ___|   |___    _||/__                                     __|
| /    ---    \   \|  |||                   __ _  ___  __ __/ /|
|/  |       |  \    \ /                    /  ' \/ _ \/ // / / |
||  |       |  |    | |                   /_/_/_/\___/\_,_/_/  |
+--------------------------------------------------------------+
Example (Composite) ¶
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"}
Example (Custom) ¶
Output:

-->  {"L":"INFO","C":"zapring/example_test.go:30","M":"hello world!"}
-->  {"L":"INFO","C":"zapring/example_test.go:31","M":"lorem ipsum"}
Example (Simple) ¶
Output:

Index ¶

Examples ¶

Constants ¶

This section is empty.

Variables ¶

This section is empty.

Functions ¶

This section is empty.

Types ¶

type Core ¶

type Core struct {
	zapcore.Core
	// contains filtered or unexported fields
}

Core is an in-memory ring buffer log that implements zapcore.Core.

func New ¶

func New(size uint) *Core

New returns a ring-buffer with a capacity of 'size' bytes.

func (*Core) Check ¶

func (c *Core) Check(entry zapcore.Entry, checked *zapcore.CheckedEntry) *zapcore.CheckedEntry

Check implements zapcore.Core.

func (*Core) Close ¶

func (c *Core) Close()

Close implements zapcore.Core.

func (*Core) Enabled ¶

func (c *Core) Enabled(level zapcore.Level) bool

Enabled implements zapcore.LevelEnabler.

func (*Core) SetEncoder ¶

func (c *Core) SetEncoder(enc zapcore.Encoder) *Core

func (*Core) SetNextCore ¶

func (c *Core) SetNextCore(core zapcore.Core) *Core

func (*Core) Sync ¶

func (c *Core) Sync() error

Sync implements zapcore.Core.

func (*Core) With ¶

func (c *Core) With(fields []zapcore.Field) zapcore.Core

With implements zapcore.Core.

func (*Core) Write ¶

func (c *Core) Write(entry zapcore.Entry, fields []zapcore.Field) error

Write implements zapcore.Core.

func (*Core) WriteTo ¶

func (c *Core) WriteTo(w io.Writer) (n int64, err error)

WriteTo implements io.WriterTo.