go>> log>> 返回
项目作者: avegner

项目描述 :
Simple logger for Go with several standard outputs
高级语言: Go
项目地址: git://github.com/avegner/log.git
创建时间: 2019-05-28T18:11:21Z
项目社区:https://github.com/avegner/log

开源协议:MIT License

下载


log

This a logger supporting the following outputs:

  • stderr
  • network (TCP, UDP, Unix networks)
  • file (zlib compression)
  • custom output

Installation

  1. go get github.com/avegner/log

API

Logger has a very simple interface:

  1. type Logger interface {
  2. Printf(level Level, format string, args ...interface{}) error
  3. SetMask(mask Level)
  4. Flush() error
  5. Child(name string) Logger
  6. }
  • Printf - prints a log message with the given level
  • SetMask - sets enabled log levels
  • Flush - flushes all log outputs
  • Child - creates a child logger with the same parameters (mask, outputs, flags)

Each output supports the following interface:

  1. type Outputter interface {
  2. io.Writer
  3. io.Closer
  4. Flush() error
  5. }

There are standard outputs:

  1. func NewNetOut(network, address string) (Outputter, error)
  2. func NewFileOut(name string, perm os.FileMode, append bool, comprLevel int) (Outputter, error)
  3. func NewStderrOut() (Outputter, error)

Any custom output implementing Outputter interface may be created.

To create a logger and a child logger:

  1. // main scope
  2. o, err := out.NewStderrOut()
  3. if err != nil {
  4. // ...
  5. }
  6. mlog := log.New("main", log.ALL_LEVELS, log.STD_FLAGS, []out.Outputter{o})
  7. // module scope
  8. chlog := mlog.Child("module")