logger

package module
v0.3.2 Latest Latest
Warning

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

Go to latest
Published: Sep 27, 2022 License: MIT Imports: 14 Imported by: 1

README

logger

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

Feature

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

Usage

  • install logger with go get

go get -u github.com/gohp/logger

  1. 新建logger
import (
 "github.com/gohp/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/gohp/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/gohp/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

Documentation

Index

Constants

View Source
const (
	TimeDivision = "time"
	SizeDivision = "size"
)
View Source
const (
	Minute  = "minute"
	Hour    = "hour"
	Day     = "day"
	Month   = "month"
	Year    = "year"
	RFC3339 = "2006-01-02T15:04:05Z07:00"
)

Variables

This section is empty.

Functions

func AddContext added in v0.3.1

func AddContext(ctx context.Context, fields ...zap.Field) context.Context

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 GAddContext added in v0.3.2

func GAddContext(ctx *gin.Context, fields ...zap.Field)

func Info

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

func Infof

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

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
)

func Ctx added in v0.3.2

func Ctx(ctx context.Context) *Log

Ctx new func

func WithContext added in v0.3.1

func WithContext(ctx context.Context) *Log

WithContext old used

func (*Log) Debug added in v0.3.1

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

func (*Log) Debugf added in v0.3.1

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

func (*Log) Error added in v0.3.1

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

func (*Log) Errorf added in v0.3.1

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

func (*Log) Fatal added in v0.3.1

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

func (*Log) Fatalf added in v0.3.1

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

func (*Log) Info added in v0.3.1

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

func (*Log) Infof added in v0.3.1

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

func (*Log) Warn added in v0.3.1

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

func (*Log) Warnf added in v0.3.1

func (l *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" 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"`
	EncodeTime    string   `json:"encode_time" yaml:"encode_time" toml:"encode_time"`
	// 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) SetEncodeTime added in v0.3.2

func (c *LogOptions) SetEncodeTime(format 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 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