z

package module
v0.0.6 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Sep 30, 2021 License: MIT Imports: 11 Imported by: 6

README

z

sync-to-gitee test codecov Go Report Card Go Reference

  • 快速设置 zap,支持读取与序列化 zap 配置
  • 支持 github.com/youthlin/logs 日志接口 Adaptor 到 zap

import

go get -u github.com/youthlin/z
# 国内镜像
go mod edit -replace github.com/youthlin/z@latest=gitee.com/youthlin/logz@latest&&go mod tidy

gitee 镜像:gitee.com/youthlin/logz (logz = logs + zap)

鸣谢 仓库同步工具 https://github.com/Yikun/hub-mirror-action

Usage 使用方式

// to init zap config
import _ "github.com/youthlin/z"

// ------- use default config -------
zap.L().Info("info message")
// 2021-09-07T12:58:14.413+0800	INFO	z/example_test.go:79	info message

zap.S().Infow("sugar info", "key", "value")
// 2021-09-07T12:59:58.649+0800	INFO	z/example_test.go:80	sugar info	{"key": "value"}

// ------- use custom config -------
log := z.NewLogger(z.DefaultConfig().Zap)
log.Debug("Debug message")
// 2021-09-07T12:59:58.649+0800	DEBUG	z/example_test.go:85	Debug message

log.With(zap.String("key", "value")).Info("info message")
// 2021-09-07T12:59:58.649+0800	INFO	z/example_test.go:86	info message	{"key": "value"}

sugar := log.Sugar()
sugar.Debugf("Hello, %s, debug message", "tom")
// 2021-09-07T12:59:58.649+0800	DEBUG	z/example_test.go:88	Hello, tom, debug message

sugar.Infow("sugar info with fields", "key", "value", "int", 42)
// 2021-09-07T12:59:58.649+0800	INFO	z/example_test.go:89	sugar info with fields	{"key": "value", "int": 42}

// ------- ------- ------- -------
// ------- as logs Adaptor -------
// ------- ------- ------- -------
// import "github.com/youthlin/logs"
logs.Ctx(ctx).With("key", 42).Debug("Hello %s", "Tom")

// use custom config
func init() {
	c := DefaultConfig()// or from .yaml / .json
  z.SetConfig(c)
}
logs.Info("Hello %s", "world")

Configs 也支持 从 yaml/json 读取。 see testdata/config.yaml and testdata/config.json

logs:
  level: # logs config
    root: error # default only print error log
    loggers:
      "github.com": warn # if package name is github.com print warn+ log
      "github.com/youthlin": debug # this package print debug+ log
  zap: # zap config
    - name: console # 控制台输出所有日志
      enable: true
      level: info
      output:
        type: console
        destination:
          filename: stdout
      encoder:
        json: false # 不需要格式化为 json
        levelEncoder: capitalColor # 带颜色大写的日志级别 capital/capitalColor/color/lowcase
        timeEncoder: rfc3339nano # e.g.: 2006-01-02T15:04:05.999999999Z07:00 rfc3339nano/rfc3339/iso8601/millis/nanos/epoch
        durationEncoder: string # 时间段格式化为带单位的: 968.6µs string/nanos/ms/seconds or 带 layout 子字段
        callerEncoder: full # full/short

Documentation

Index

Constants

View Source
const (
	Stdout = "stdout" // 标准输出
	Stderr = "stderr" // 标准错误
)

Variables

This section is empty.

Functions

func NewLogger

func NewLogger(configs []*ZapConfig) *zap.Logger

NewLogger new 一个 Logger

func NewZapAdaptor added in v0.0.5

func NewZapAdaptor(zLog *zap.Logger) logs.Adaptor

func SetConfig added in v0.0.5

func SetConfig(c *LogsConfig)

Types

type Encoder added in v0.0.5

type Encoder struct {
	zapcore.EncoderConfig
	EncodeTime TimeEncoder `json:"timeEncoder" yaml:"timeEncoder"`
	AsJSON     bool        `json:"json" yaml:"json"` // 整条日志使用 JSON 格式输出
	// contains filtered or unexported fields
}

Encoder 包装 zapcore 的 EncoderConfig

func (*Encoder) AsMap added in v0.0.5

func (e *Encoder) AsMap() map[string]interface{}

AsMap 转为 map 用于序列化为 yaml/json. zap 配置本身只支持反序列化,不支持序列化,所以需要先转 map

func (Encoder) MarshalJSON added in v0.0.5

func (e Encoder) MarshalJSON() ([]byte, error)

func (Encoder) MarshalYAML added in v0.0.5

func (e Encoder) MarshalYAML() (interface{}, error)

func (*Encoder) UnmarshalYAML added in v0.0.5

func (e *Encoder) UnmarshalYAML(unmarshal func(interface{}) error) error

UnmarshalYAML 先反序列化到 map,然后通过 json 反序列化 因为 yaml 不支持反序列化嵌入结构(需要 `yaml:"",inline"` 不能有同名字段)

func (*Encoder) Zap added in v0.0.5

func (e *Encoder) Zap() zapcore.Encoder

Zap 转为 zap 的 Encoder

type LogsConfig added in v0.0.5

type LogsConfig struct {
	Level *logs.LoggerLevel `json:"level" yaml:"level"` // 各 Logger 的日志级别,配合 logs 库使用
	Zap   []*ZapConfig      `json:"zap" yaml:"zap"`     // zap 的输出配置
}

LogsConfig 日志配置

func DefaultConfig

func DefaultConfig() *LogsConfig

DefaultConfig 返回默认的日志配置,debug 级别、输出到控制台

type Output

type Output struct {
	Type OutputType        `json:"type" yaml:"type"` // 控制台或文件
	File lumberjack.Logger `json:"file" yaml:"file"` // 如果是控制台,则只需要填写 Filename(stdout/stderr),如果是文件,则根据需要填写字段
	// contains filtered or unexported fields
}

Output 日志输出配置

func (*Output) Write added in v0.0.6

func (o *Output) Write(p []byte) (n int, err error)

func (*Output) WriteSyncer added in v0.0.5

func (o *Output) WriteSyncer() zapcore.WriteSyncer

WriteSyncer 转为 zap 的 WriteSyncer

type OutputType

type OutputType string

OutputType 日志输出类型

const (
	Console OutputType = "console" // 控制台
	File    OutputType = "file"    // 文件
)

type TimeEncoder added in v0.0.5

type TimeEncoder struct {
	Name   string
	Layout string
}

TimeEncoder zapcore 的 TimeEncoder 可能有两种格式 1. ISO8601 这种指定的预置格式 { "timeEncoder": "ISO8601" } 2. 用户使用 layout 字段指定时间格式 { "timeEncoder: { "layout": "2006-01-02 15:04:05.000"} }

func (*TimeEncoder) ToZapTimeEncoder added in v0.0.5

func (e *TimeEncoder) ToZapTimeEncoder() zapcore.TimeEncoder

func (*TimeEncoder) UnmarshalJSON added in v0.0.5

func (e *TimeEncoder) UnmarshalJSON(data []byte) error

func (*TimeEncoder) UnmarshalText added in v0.0.5

func (e *TimeEncoder) UnmarshalText(text []byte) error

func (*TimeEncoder) UnmarshalYAML added in v0.0.5

func (e *TimeEncoder) UnmarshalYAML(unmarshal func(interface{}) error) error

UnmarshalYAML 反序列化 yaml,先尝试 layout 模式,再尝试预置格式

type ZapAdaptor added in v0.0.5

type ZapAdaptor struct {
	*zap.Logger
}

ZapAdaptor is an adaptor for github.com/youthlin/logs

func (*ZapAdaptor) Log added in v0.0.5

func (s *ZapAdaptor) Log(msg logs.Message)

Log log message to zap

type ZapConfig added in v0.0.5

type ZapConfig struct {
	Name    string        `json:"name" yaml:"name"`       // 配置名称
	Enable  bool          `json:"enable" yaml:"enable"`   // 是否启用
	Level   zapcore.Level `json:"level" yaml:"level"`     // 大于等于该级别的日志才会输出
	Encoder *Encoder      `json:"encoder" yaml:"encoder"` // 输出的各个字段格式
	Output  *Output       `json:"output" yaml:"output"`   // 日志输出去向
}

ZapConfig 日志输出配置

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL