聊聊zerolog的LevelWriter

codecraft · · 356 次点击 · · 开始浏览    
这是一个创建于 的文章,其中的信息可能已经有所发展或是发生改变。

本文主要研究一下zerolog的LevelWriter

LevelWriter

github.com/rs/zerolog@v1.20.0/writer.go

// LevelWriter defines as interface a writer may implement in order
// to receive level information with payload.
type LevelWriter interface {
    io.Writer
    WriteLevel(level Level, p []byte) (n int, err error)
}

type Writer interface {
    Write(p []byte) (n int, err error)
}
LevelWriter接口内嵌了io.Writer接口,定义了WriteLevel方法

levelWriterAdapter

github.com/rs/zerolog@v1.20.0/writer.go

type levelWriterAdapter struct {
    io.Writer
}

func (lw levelWriterAdapter) WriteLevel(l Level, p []byte) (n int, err error) {
    return lw.Write(p)
}
levelWriterAdapter内嵌了io.Writer属性,实现了LevelWriter的WriteLevel方法,该方法内部通过io.Writer属性的Write方法来输出

syncWriter

github.com/rs/zerolog@v1.20.0/writer.go

type syncWriter struct {
    mu sync.Mutex
    lw LevelWriter
}

func SyncWriter(w io.Writer) io.Writer {
    if lw, ok := w.(LevelWriter); ok {
        return &syncWriter{lw: lw}
    }
    return &syncWriter{lw: levelWriterAdapter{w}}
}

// Write implements the io.Writer interface.
func (s *syncWriter) Write(p []byte) (n int, err error) {
    s.mu.Lock()
    defer s.mu.Unlock()
    return s.lw.Write(p)
}

// WriteLevel implements the LevelWriter interface.
func (s *syncWriter) WriteLevel(l Level, p []byte) (n int, err error) {
    s.mu.Lock()
    defer s.mu.Unlock()
    return s.lw.WriteLevel(l, p)
}
syncWriter实现了LevelWriter接口,其对Write及WriteLevel方法都进行了加锁;SyncWriter方法通过levelWriterAdapter来适配io.Writer

multiLevelWriter

github.com/rs/zerolog@v1.20.0/writer.go

type multiLevelWriter struct {
    writers []LevelWriter
}

func (t multiLevelWriter) Write(p []byte) (n int, err error) {
    for _, w := range t.writers {
        n, err = w.Write(p)
        if err != nil {
            return
        }
        if n != len(p) {
            err = io.ErrShortWrite
            return
        }
    }
    return len(p), nil
}

func (t multiLevelWriter) WriteLevel(l Level, p []byte) (n int, err error) {
    for _, w := range t.writers {
        n, err = w.WriteLevel(l, p)
        if err != nil {
            return
        }
        if n != len(p) {
            err = io.ErrShortWrite
            return
        }
    }
    return len(p), nil
}

// MultiLevelWriter creates a writer that duplicates its writes to all the
// provided writers, similar to the Unix tee(1) command. If some writers
// implement LevelWriter, their WriteLevel method will be used instead of Write.
func MultiLevelWriter(writers ...io.Writer) LevelWriter {
    lwriters := make([]LevelWriter, 0, len(writers))
    for _, w := range writers {
        if lw, ok := w.(LevelWriter); ok {
            lwriters = append(lwriters, lw)
        } else {
            lwriters = append(lwriters, levelWriterAdapter{w})
        }
    }
    return multiLevelWriter{lwriters}
}
multiLevelWriter定义了writers属性,它也实现了LevelWriter接口,其Write及WriteLevel方法均是遍历writers挨个执行对应操作;MultiLevelWriter方法通过levelWriterAdapter来适配io.Writer

实例

func multiLevelWriterDemo() {
    consoleWriter := zerolog.ConsoleWriter{Out: os.Stdout}
    multi := zerolog.MultiLevelWriter(consoleWriter, os.Stdout)
    logger := zerolog.New(multi).With().Timestamp().Logger()
    logger.Info().Msg("Hello World!")
}
这里使用zerolog.MultiLevelWriter来指定多个输出

输出

8:02PM INF Hello World!
{"level":"info","time":"2021-01-02T20:02:27+08:00","message":"Hello World!"}

小结

LevelWriter接口内嵌了io.Writer接口,定义了WriteLevel方法;levelWriterAdapter内嵌了io.Writer属性,实现了LevelWriter的WriteLevel方法,该方法内部通过io.Writer属性的Write方法来输出;SyncWriter方法通过levelWriterAdapter来适配io.Writer;MultiLevelWriter方法通过levelWriterAdapter来适配io.Writer。

doc


有疑问加站长微信联系(非本文作者)

本文来自:Segmentfault

感谢作者:codecraft

查看原文:聊聊zerolog的LevelWriter

入群交流(和以上内容无关):加入Go大咖交流群,或添加微信:liuxiaoyan-s 备注:入群;或加QQ群:692541889

356 次点击  
加入收藏 微博
暂无回复
添加一条新回复 (您需要 登录 后才能回复 没有账号 ?)
  • 请尽量让自己的回复能够对别人有帮助
  • 支持 Markdown 格式, **粗体**、~~删除线~~、`单行代码`
  • 支持 @ 本站用户;支持表情(输入 : 提示),见 Emoji cheat sheet
  • 图片支持拖拽、截图粘贴等方式上传