logger

package
v0.0.0-...-183aa75 Latest Latest
Warning

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

Go to latest
Published: Dec 22, 2020 License: MIT Imports: 15 Imported by: 0

README

特性

  • 支持格式微自定义
  • 单文件容量限制,切割文件
  • 文件保留时间设置,自动删除过期文件
  • 捕获堆栈信息

Usage

go get -u github.com/huzhongqing/gokit/logger
func TestLogger(t *testing.T) {
	lg := NewLogger(DefaultConfig())

	lg.Debug("DEBUG")
	lg.Info("INFO")
	lg.Warn("WARN")
	lg.Error("ERROR")
	lg.Error("%s_%s", "stack", lg.Stack(fmt.Errorf("这是stack")))
	lg.Fatal("FATAL")

	_ = lg.Close()
}

Documentation

Index

Constants

View Source
const (
	DebugLevel int = iota
	InfoLevel
	WarnLevel
	ErrorLevel
	FatalLevel
)

Variables

Functions

This section is empty.

Types

type Buffer

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

func (*Buffer) AppendString

func (b *Buffer) AppendString(v string)

func (*Buffer) Bytes

func (b *Buffer) Bytes() []byte

func (*Buffer) Len

func (b *Buffer) Len() int

func (*Buffer) Reset

func (b *Buffer) Reset()

func (*Buffer) String

func (b *Buffer) String() string

type BufferPool

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

func NewBufferPool

func NewBufferPool() BufferPool

func (BufferPool) Get

func (bp BufferPool) Get() *Buffer

func (BufferPool) Put

func (bp BufferPool) Put(buf *Buffer)

type Config

type Config struct {
	// 调用深度
	Calldpeth int
	// 日志等级,>= 设置的等级才会写入
	Level int
	// 格式化,配置
	Format *FormatConfig
	Write  *WriteConfig
}

Config 日志配置信息

func DefaultConfig

func DefaultConfig() Config

DefaultConfig 默认配置

type Format

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

func NewFormat

func NewFormat(cfg *FormatConfig) *Format

NewFormat

func (*Format) GenMessage

func (f *Format) GenMessage(level, message string) []byte

GenMessage 生成等待写入的内容

func (*Format) Stack

func (f *Format) Stack(msg string) string

返回当前堆栈信息

type FormatConfig

type FormatConfig struct {
	// 时间格式
	LogTimeFormat string

	// 消息前缀
	MessagePrefix string
}

FormatConfig

func DefaultFormatConfig

func DefaultFormatConfig() *FormatConfig

默认格式化配置文件

type Logger

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

保留日志天数,定时扫描 dir

func NewLogger

func NewLogger(cfg Config) *Logger

NewLogger

func (*Logger) Append

func (lg *Logger) Append(message []byte) (n int, err error)

Append 写入文件

func (*Logger) Close

func (lg *Logger) Close() error

Close

func (*Logger) Debug

func (lg *Logger) Debug(format string, args ...interface{})

Debug

func (*Logger) Error

func (lg *Logger) Error(format string, args ...interface{})

Error

func (*Logger) Fatal

func (lg *Logger) Fatal(format string, args ...interface{})

Fatal 等级 退出程序

func (*Logger) Info

func (lg *Logger) Info(format string, args ...interface{})

Info

func (*Logger) Output

func (lg *Logger) Output(calldepth int, s string) (n int, err error)

Output 系统输出

func (*Logger) SetLevel

func (lg *Logger) SetLevel(level int)

SetLevel 设置错误等级

func (*Logger) Stack

func (lg *Logger) Stack(err error) string

Stack 堆栈信息

func (*Logger) String

func (lg *Logger) String(calldpeth int, level, message string) []byte

String 生成待写入文件的数据

func (*Logger) Warn

func (lg *Logger) Warn(format string, args ...interface{})

Warn

type Write

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

Write 写入文件对象

func NewWrite

func NewWrite(cfg *WriteConfig) *Write

NewWrite new

func (*Write) Close

func (w *Write) Close() error

Close 释放

func (*Write) Sync

func (w *Write) Sync() error

func (*Write) Write

func (w *Write) Write(message []byte) (n int, err error)

type WriteConfig

type WriteConfig struct {
	// 写入文件
	Filename string
	// 单文件最大 bytes
	MaxSize int64
	// 保留文件时间
	MaxAge time.Duration
	// Gzip 压缩
	Compress bool
}

func DefaultWriteConfig

func DefaultWriteConfig() *WriteConfig

Jump to

Keyboard shortcuts

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