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) ¶
package main import ( "bufio" "fmt" "io" "go.uber.org/zap" "go.uber.org/zap/zapcore" "moul.io/zapring" ) func main() { 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"}
Example (Custom) ¶
package main import ( "bufio" "fmt" "io" "io/ioutil" "go.uber.org/zap" "go.uber.org/zap/zapcore" "moul.io/zapring" ) func main() { 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"}
Example (Simple) ¶
package main import ( "go.uber.org/zap" "moul.io/zapring" ) func main() { ring := zapring.New(10 * 1024 * 1024) // 10MB ring-buffer logger := zap.New(ring, zap.Development()) logger.Info("test") }
Output:
Index ¶
- type Core
- func (c *Core) Check(entry zapcore.Entry, checked *zapcore.CheckedEntry) *zapcore.CheckedEntry
- func (c *Core) Close()
- func (c *Core) Enabled(level zapcore.Level) bool
- func (c *Core) SetEncoder(enc zapcore.Encoder) *Core
- func (c *Core) SetNextCore(core zapcore.Core) *Core
- func (c *Core) Sync() error
- func (c *Core) With(fields []zapcore.Field) zapcore.Core
- func (c *Core) Write(entry zapcore.Entry, fields []zapcore.Field) error
- func (c *Core) WriteTo(w io.Writer) (n int64, err error)
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Core ¶
Core is an in-memory ring buffer log that implements zapcore.Core.
func (*Core) Check ¶
func (c *Core) Check(entry zapcore.Entry, checked *zapcore.CheckedEntry) *zapcore.CheckedEntry
Check implements zapcore.Core.
Click to show internal directories.
Click to hide internal directories.