Help us shape the future of Go! Take the 2026 Go Developer Survey

logger

package module
v0.2.4 Latest Latest
Warning

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

Go to latest
Published: Apr 7, 2020 License: MIT Imports: 13 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 Debug

func Debug(msg string, args ...zap.Field)

func Debugf

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

func Error

func Error(msg string, args ...zap.Field)

func Errorf

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

func Fatal

func Fatal(msg string, args ...zap.Field)

func Fatalf

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

func Info

func Info(msg string, args ...zap.Field)

func Infof

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

func NewSentryCore added in v0.2.4

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

NewSentryCore 生成Core对象

func Warn

func Warn(msg string, args ...zap.Field)

func Warnf

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

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
}
var (
	Logger *Log
)

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" yaml:"encoding" toml:"encoding"`
	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"`
	// 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() *Log

func (*LogOptions) SetCaller

func (c *LogOptions) SetCaller(b bool)

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 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