logger

package module
v0.3.2 Latest Latest
Warning

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

Go to latest
Published: Feb 22, 2022 License: MIT Imports: 14 Imported by: 0

README

logger

基于Zap,可选日志文件归档方式

Feature

  • 根据info/warn级别切割日志文件
  • 根据文件大小归档
  • 根据时间归档
  • 时间切割单元可选
  • 日志发送到sentry

Usage

  • install logger with go get

go get -u github.com/wzyonggege/logger

  1. 新建logger
import (
 "github.com/wzyonggege/logger"
)

...

c := logger.New()
c.SetDivision("time")	    // 设置归档方式,"time"时间归档 "size" 文件大小归档,文件大小等可以在配置文件配置
c.SetTimeUnit(logger.Minute) // 时间归档 可以设置切割单位
c.SetEncoding("json")	    // 输出格式 "json" 或者 "console"

c.SetInfoFile("./logs/server.log")		// 设置info级别日志
c.SetErrorFile("./logs/server_err.log")	// 设置warn级别日志

c.InitLogger()
  1. 从配置文件中加载(Toml,Yaml,Json)
// toml file
c := logger.NewFromToml(confPath)

// yaml file
c := logger.NewFromYaml("configs/config.yaml")


// json file
c := logger.NewFromJson("configs/config.json")

c.InitLogger()
  1. caller
c.SetCaller(true)
  1. 输出
import (
 "github.com/wzyonggege/logger"
)

...
c := logger.New()
c.InitLogger()

logger.Info("info level test")
logger.Error("error level test")
logger.Warn("warn level test")
logger.Debug("debug level test")
logger.Fatal("fatal level test")

// format
logger.Infof("info level test: %s", "111")
logger.Errorf("error level test: %s", "111")
logger.Warnf("warn level test: %s", "111")
logger.Debugf("debug level test: %s", "111")
{"level":"info","time":"2019-09-11T18:32:59.680+0800","msg":"info level test"}
{"level":"error","time":"2019-09-11T18:32:59.680+0800","msg":"error level test"}
{"level":"warn","time":"2019-09-11T18:32:59.681+0800","msg":"warn level test"}
{"level":"debug","time":"2019-09-11T18:32:59.681+0800","msg":"debug level test"}
{"level":"fatal","time":"2019-09-11T18:32:59.681+0800","msg":"fatal level test"}
  1. with args
import (
 "github.com/wzyonggege/logger"
)

...
c := logger.New()
c.InitLogger()

logger.Info("this is a log", logger.With("Trace", "12345677"))
logger.Info("this is a log", logger.WithError(errors.New("this is a new error")))
{"level":"info","time":"2019-09-11T18:38:51.022+0800","msg":"this is a log","Trace":"12345677"}
{"level":"info","time":"2019-09-11T18:38:51.026+0800","msg":"this is a log","error":"this is a new error"}

Performance

BenchmarkLogger/logde_logger_without_fields-4            3000000               563 ns/op
BenchmarkLogger/logde_logger_with_fields-4               2000000               637 ns/op
BenchmarkLogger/logde_logger_without_fields_write_into_file-4             200000             13021 ns/op
BenchmarkLogger/logde_logger_with_fields_write_into_file-4                100000             12606 ns/op
  1. sentry
c := logger.New()
c.SetDivision("time")     // 设置归档方式,"time"时间归档 "size" 文件大小归档,文件大小等可以在配置文件配置
c.SetTimeUnit(logger.Day) // 时间归档 可以设置切割单位
c.SetEncoding("json")     // 输出格式 "json" 或者 "console"
//c.Stacktrace = true

c.SetInfoFile("./logs/server.log")      // 设置info级别日志
c.SetErrorFile("./logs/server_err.log") // 设置warn级别日志

c.SentryConfig = logger.SentryLoggerConfig{
    DSN:              "sentry dsn",
    Debug:            true,
    AttachStacktrace: true,
    Environment:      "dev",
    Tags: map[string]string{
        "source": "demo",
    },
}

c.InitLogger()

Documentation

Index

Constants

View Source
const (
	TimeDivision = "time"
	SizeDivision = "size"
)
View Source
const (
	Minute = "minute"
	Hour   = "hour"
	Day    = "day"
	Month  = "month"
	Year   = "year"
)

Variables

This section is empty.

Functions

func NewSentryCore added in v0.2.4

func NewSentryCore(cfg sentryCoreConfig, sentryClient *sentry.Client) *core

NewSentryCore 生成Core对象

func With

func With(k string, v interface{}) zap.Field

func WithError

func WithError(err error) zap.Field

Types

type Log

type Log struct {
	L *zap.Logger
}

func (*Log) Debug added in v0.2.6

func (log *Log) Debug(msg string, args ...zap.Field)

func (*Log) Debugf added in v0.2.6

func (log *Log) Debugf(format string, args ...interface{})

func (*Log) Error added in v0.2.6

func (log *Log) Error(msg string, args ...zap.Field)

func (*Log) Errorf added in v0.2.6

func (log *Log) Errorf(format string, args ...interface{})

func (*Log) Fatal added in v0.2.6

func (log *Log) Fatal(msg string, args ...zap.Field)

func (*Log) Fatalf added in v0.2.6

func (log *Log) Fatalf(format string, args ...interface{})

func (*Log) Info added in v0.2.6

func (log *Log) Info(msg string, args ...zap.Field)

func (*Log) Infof added in v0.2.6

func (log *Log) Infof(format string, args ...interface{})

func (*Log) Warn added in v0.2.6

func (log *Log) Warn(msg string, args ...zap.Field)

func (*Log) Warnf added in v0.2.6

func (log *Log) Warnf(format string, args ...interface{})

type LogOptions

type LogOptions struct {
	// Encoding sets the logger's encoding. Valid values are "json" and
	// "console", as well as any third-party encodings registered via
	// RegisterEncoder.
	Encoding      string             `json:"encoding,omitempty" yaml:"encoding,omitempty" toml:"encoding,omitempty"`
	InfoFilename  string             `json:"info_filename" yaml:"info_filename" toml:"info_filename"`
	ErrorFilename string             `json:"error_filename" yaml:"error_filename" toml:"error_filename"`
	MaxSize       int                `json:"max_size" yaml:"max_size" toml:"max_size"`
	MaxBackups    int                `json:"max_backups" yaml:"max_backups" toml:"max_backups"`
	MaxAge        int                `json:"max_age" yaml:"max_age" toml:"max_age"`
	Compress      bool               `json:"compress" yaml:"compress" toml:"compress"`
	Division      string             `json:"division" yaml:"division" toml:"division"`
	LevelSeparate bool               `json:"level_separate" yaml:"level_separate" toml:"level_separate"`
	TimeUnit      TimeUnit           `json:"time_unit" yaml:"time_unit" toml:"time_unit"`
	Stacktrace    bool               `json:"stacktrace" yaml:"stacktrace" toml:"stacktrace"`
	SentryConfig  SentryLoggerConfig `json:"sentry_config" yaml:"sentry_config" toml:"sentry_config"`
	Level         int8               `json:"level" yaml:"level" toml:"level"`
	CloseDisplay  int                `json:"close_display" yaml:"close_display" toml:"close_display"`
	SampleRate    float64            `json:"sample_rate,omitempty" yaml:"sample_rate,omitempty" toml:"sample_rate,omitempty"`
	// contains filtered or unexported fields
}

func New

func New() *LogOptions

func NewFromJson

func NewFromJson(confPath string) *LogOptions

func NewFromToml

func NewFromToml(confPath string) *LogOptions

func NewFromYaml

func NewFromYaml(confPath string) *LogOptions

func (*LogOptions) CloseConsoleDisplay

func (c *LogOptions) CloseConsoleDisplay()

func (*LogOptions) InitLogger

func (c *LogOptions) InitLogger(timeKey, levelKey string, customEncodeTime, shortCaller bool) *Log

func (*LogOptions) InitSampleLogger added in v0.3.1

func (c *LogOptions) InitSampleLogger(timeKey, levelKey string, customEncodeTime, shortCaller bool) *SampleLog

func (*LogOptions) SetCaller

func (c *LogOptions) SetCaller(enable bool, skip int)

func (*LogOptions) SetDivision

func (c *LogOptions) SetDivision(division string)

func (*LogOptions) SetEncoding

func (c *LogOptions) SetEncoding(encoding string)

func (*LogOptions) SetErrorFile

func (c *LogOptions) SetErrorFile(path string)

func (*LogOptions) SetInfoFile

func (c *LogOptions) SetInfoFile(path string)

func (*LogOptions) SetTimeUnit

func (c *LogOptions) SetTimeUnit(t TimeUnit)

type SampleLog added in v0.3.1

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

func (*SampleLog) Debug added in v0.3.1

func (log *SampleLog) Debug(msg string, args ...zap.Field)

func (*SampleLog) Debugf added in v0.3.1

func (log *SampleLog) Debugf(format string, args ...interface{})

func (*SampleLog) Error added in v0.3.1

func (log *SampleLog) Error(msg string, args ...zap.Field)

func (*SampleLog) Errorf added in v0.3.1

func (log *SampleLog) Errorf(format string, args ...interface{})

func (*SampleLog) Fatal added in v0.3.1

func (log *SampleLog) Fatal(msg string, args ...zap.Field)

func (*SampleLog) Fatalf added in v0.3.1

func (log *SampleLog) Fatalf(format string, args ...interface{})

func (*SampleLog) Info added in v0.3.1

func (log *SampleLog) Info(msg string, args ...zap.Field)

func (*SampleLog) Infof added in v0.3.1

func (log *SampleLog) Infof(format string, args ...interface{})

func (*SampleLog) Warn added in v0.3.1

func (log *SampleLog) Warn(msg string, args ...zap.Field)

func (*SampleLog) Warnf added in v0.3.1

func (log *SampleLog) Warnf(format string, args ...interface{})

type SentryLoggerConfig added in v0.2.4

type SentryLoggerConfig struct {
	DSN              string `toml:"dsn" yaml:"dsn" json:"dsn"`
	Debug            bool
	AttachStacktrace bool
	Environment      string
	Tags             map[string]string
}

type TimeUnit

type TimeUnit string

func (TimeUnit) Format

func (t TimeUnit) Format() string

func (TimeUnit) RotationGap

func (t TimeUnit) RotationGap() time.Duration

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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