zapsetup

package module
v0.1.4 Latest Latest
Warning

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

Go to latest
Published: Mar 4, 2024 License: MIT Imports: 2 Imported by: 0

README

简介

本仓库旨在提供适用于一般场合的 zaplog 配置,做好"最后一公里".

繁介

zaplog 有着强大又灵活的配置项,但多数场合并不需要太多功能. 其自带的 Production 也仅适用在超高吞吐量的场合. 因此, 当你想关闭采样输出, 或者想在控制台查看日志, 又或者想要动态修改 log.Level, 还或者 想实现一些sink把日志投递到日志存储&分析系统里, 那就看看这个仓库里的代码吧.

特性

  • 开箱即用
  • 支持阿里云日志(aliyun-sls)
  • 支持腾讯云日志(tencent-cls)

Usage

import (
	"flag"
	"fmt"

	zapsetup "github.com/gamedev-embers/zap-setup"
	"github.com/gamedev-embers/zap-setup/sink/aliyunsls"
	"go.uber.org/zap"
	"go.uber.org/zap/zapcore"
)

var (
	log  = zapsetup.NewLogger()
)

func init() {
	log = log.WithSink(aliyunSLS(), zapcore.WarnLevel)
	// log = log.WithSink(tencentCLS(), zapcore.WarnLevel)
}

func main() {
	log.Debug("here is the default root logger")
	log.Info("here is the default root logger")

	// log with aliyun sls
	log.Warn("here is a logger with aliyun sls",
		zap.String("str", "value"),
		zap.Int("int", 1),
		zap.Error(fmt.Errorf("fake error")))

	log.Error("here is a logger with aliyun sls",
		zap.String("str", "value"),
		zap.Int("int", 1),
		zap.Error(fmt.Errorf("fake error")))
}

func aliyunSLS() zapsetup.Sink {
	sink, err := aliyunsls.New("aliyun+sls://user:passwd@endpoint/projectA/logstoreA")
	if err != nil {
		panic(err)
	}
	sink.Open()
	return sink
}

func tencentCLS() zapsetup.Sink {
	sink, err := aliyunsls.New("tencent+cls://user:passwd@endpoint/projectA/logstoreA")
	if err != nil {
		panic(err)
	}
	sink.Open()
	return sink
}


TODO

  • aliyun-sls-sink
  • tencent-cls-sink
  • aws-watchlog-sink

LICENCE

MIT License

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type CoreX

type CoreX struct {
	// contains filtered or unexported fields
}

func NewCoreX

func NewCoreX(l *zap.Logger, sink Sink) *CoreX

func (*CoreX) Check

func (*CoreX) Enabled

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

func (*CoreX) SetLogLevelForSink

func (c *CoreX) SetLogLevelForSink(level zapcore.Level)

SetLogLevelForSink doesn't change the log level of the core, but it changes the log level of the sink Note: it's not goroutine safe

func (*CoreX) Sync

func (c *CoreX) Sync() error

func (*CoreX) With

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

func (*CoreX) Write

func (c *CoreX) Write(ent zapcore.Entry, fields []zapcore.Field) error

type LoggerX

type LoggerX struct {
	*zap.Logger
	// contains filtered or unexported fields
}

func NewLogger

func NewLogger(opts ...Option) *LoggerX

func RootLogger

func RootLogger() *LoggerX

func (*LoggerX) SetLevel

func (l *LoggerX) SetLevel(level zapcore.Level) *LoggerX

SetLevel atomically

func (*LoggerX) WithSink

func (l *LoggerX) WithSink(s Sink, logLevel ...zapcore.Level) *LoggerX

type Option

type Option func(*zap.Config)

func WithLogLevel

func WithLogLevel(level zapcore.Level) Option

func WithSampling

func WithSampling(c *zap.SamplingConfig) Option

type Sink

type Sink interface {
	Open()
	Close()
	Write(ent zapcore.Entry, fields []zapcore.Field)
}

Directories

Path Synopsis
_examples
sink

Jump to

Keyboard shortcuts

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