logo

package module
v0.0.0-...-926c858 Latest Latest
Warning

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

Go to latest
Published: Jul 5, 2020 License: MIT Imports: 11 Imported by: 0

README

需求分析

  1. 支持往不同的地方输出日志

  2. 日志分级别

    • Debug
    • Trace
    • Info
    • Warning
    • Error
    • Fatal
  3. 日志要支持开关控制

  4. 完整的日志记录要包含时间、行号、文件名、日志级别、日志信息

  5. 打印日志可以定义输出格式,至少有text和json两种格式

  6. 日志文件要切割

    • 按文件大小切割

      1. 关闭当前文件
      2. 备份一个 rename
      3. 打开一个新的日志文件
      4. 将打开的文件赋值给 fl.FileObj
      
    • 按日期切割

      1. 在日志结构体中设置一个字段记录上一次切割的小时数
      2. 再写日志之前检查一下当前时间的小时数和保存的是否一致,不一致就要切割
      
    • 设置日志最大保留时长

      1. 在日志结构体中设置一个字段记录最大文件保留时长
      2. 定时扫描所有日志文件,若日志文件时间早于保留最早时间,则删除
      
    • 设置日志文件最大保留个数

      1. 在日志结构体中设置一个字段记录最大保留文件个数
      2. 定时扫描所有日志文件。 若日志文件数量超出指定个数时,保留指定个数最新文件,其余文件删除
      
  7. 性能优化:异步打印日志

更新日志

  • 2020.06.15 增加异步打印日志功能和日志同时输出console和file功能
  • 2020.06.16 增加自定义日志文件最大保留时长功能
  • 2020.06.17 增加自定义日志文件最大保留个数功能,生成文件Logger时可以传不定长参数
  • 2020.06.18 增加日志输出Json格式,优化日志打印效率
  • 2020.06.24 修改日志输入文件参数解析框架,提高参数传值和解析效率
  • 2020.07.05 实现自定义级别打印文件行信息功能

Documentation

Index

Constants

View Source
const (
	TextEncoder encoder = iota
	JsonEncoder
)

Variables

This section is empty.

Functions

This section is empty.

Types

type ConsoleFileLogger

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

func NewConsoleFileLogger

func NewConsoleFileLogger(stdout LogLevel, file LogLevel, fp, fn string, options ...LogoHandleFunc) *ConsoleFileLogger

func (*ConsoleFileLogger) Debug

func (cf *ConsoleFileLogger) Debug(msg string)

func (*ConsoleFileLogger) Debugf

func (cf *ConsoleFileLogger) Debugf(format string, a ...interface{})

func (*ConsoleFileLogger) Error

func (cf *ConsoleFileLogger) Error(msg string)

func (*ConsoleFileLogger) Errorf

func (cf *ConsoleFileLogger) Errorf(format string, a ...interface{})

func (*ConsoleFileLogger) Fatal

func (cf *ConsoleFileLogger) Fatal(msg string)

func (*ConsoleFileLogger) Fatalf

func (cf *ConsoleFileLogger) Fatalf(format string, a ...interface{})

func (*ConsoleFileLogger) Info

func (cf *ConsoleFileLogger) Info(msg string)

func (*ConsoleFileLogger) Infof

func (cf *ConsoleFileLogger) Infof(format string, a ...interface{})

func (*ConsoleFileLogger) SetEncoder

func (cf *ConsoleFileLogger) SetEncoder(encode encoder)

func (*ConsoleFileLogger) SetFileInfo

func (cf *ConsoleFileLogger) SetFileInfo(level LogLevel)

func (*ConsoleFileLogger) SetPrefix

func (cf *ConsoleFileLogger) SetPrefix(prefix string)

func (*ConsoleFileLogger) SetTimeFormat

func (cf *ConsoleFileLogger) SetTimeFormat(format string)

func (*ConsoleFileLogger) Trace

func (cf *ConsoleFileLogger) Trace(msg string)

func (*ConsoleFileLogger) Tracef

func (cf *ConsoleFileLogger) Tracef(format string, a ...interface{})

func (*ConsoleFileLogger) Warning

func (cf *ConsoleFileLogger) Warning(msg string)

func (*ConsoleFileLogger) Warningf

func (cf *ConsoleFileLogger) Warningf(format string, a ...interface{})

type ConsoleLogger

type ConsoleLogger struct {
	Level         LogLevel `json:"level"`
	Prefix        string   `json:"prefix"`
	FileInfoLevel LogLevel `json:"file_info_level"`
	Encoder       encoder  `json:"encoder"`
}

func New

func New() *ConsoleLogger

func NewConsoleLogger

func NewConsoleLogger(level LogLevel, args ...interface{}) *ConsoleLogger

ConsoleLogger 构造函数

func (*ConsoleLogger) Debug

func (cl *ConsoleLogger) Debug(msg string)

func (*ConsoleLogger) Debugf

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

func (*ConsoleLogger) Error

func (cl *ConsoleLogger) Error(msg string)

func (*ConsoleLogger) Errorf

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

func (*ConsoleLogger) Fatal

func (cl *ConsoleLogger) Fatal(msg string)

func (*ConsoleLogger) Fatalf

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

func (*ConsoleLogger) Info

func (cl *ConsoleLogger) Info(msg string)

func (*ConsoleLogger) Infof

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

func (*ConsoleLogger) JsonEncode

func (cl *ConsoleLogger) JsonEncode(formatTime, fileInfo, level, msg string) []byte

func (*ConsoleLogger) Log

func (cl *ConsoleLogger) Log(level LogLevel, format string, args ...interface{})

func (*ConsoleLogger) SetEncoder

func (cl *ConsoleLogger) SetEncoder(encode encoder)

func (*ConsoleLogger) SetFileInfo

func (cl *ConsoleLogger) SetFileInfo(level LogLevel)

func (*ConsoleLogger) SetPrefix

func (cl *ConsoleLogger) SetPrefix(prefix string)

func (*ConsoleLogger) SetTimeFormat

func (cl *ConsoleLogger) SetTimeFormat(format string)

func (*ConsoleLogger) TextEncode

func (cl *ConsoleLogger) TextEncode(formatTime, fileInfo, level, msg string) []byte

func (*ConsoleLogger) Trace

func (cl *ConsoleLogger) Trace(msg string)

func (*ConsoleLogger) Tracef

func (cl *ConsoleLogger) Tracef(format string, args ...interface{})

func (*ConsoleLogger) Warning

func (cl *ConsoleLogger) Warning(msg string)

func (*ConsoleLogger) Warningf

func (cl *ConsoleLogger) Warningf(format string, args ...interface{})

type FileLogger

type FileLogger struct {
	// 日志级别 比该级别高的打印的时候会显示
	Level LogLevel `json:"level"`
	// 前缀 打印日志信息的前缀
	Prefix string `json:"prefix"`
	// 日志文件夹路径
	FilePath string `json:"file_path"`
	// 日志文件名
	FileName string `json:"file_name"`
	// 错误日志文件名
	ErrFileName string `json:"err_file_name"`
	// 最大文件大小 分割日志文件的时候会用到
	MaxFileSize int64 `json:"max_file_size"`
	// 最大文件保留个数
	MaxFileCount    int `json:"max_file_count"`
	MaxErrFileCount int `json:"max_file_count"`
	// 分割文件最大时间间隔 按时间分割文件的时候会用到
	SplitFileAge int `json:"split_file_age"`
	// 最大保存保存时间长度
	MaxFileAge int `json:"max_file_age"`
	// 是否打印文件行号信息
	FileInfoLevel LogLevel `json:"file_info_level"`
	// 输出类型
	Encoder encoder `json:"encoder"`
	// 记录上次切割文件的时间
	LastSplitTime time.Time
	// 日志文件对象
	FileObj *os.File
	// Error日志文件对象
	ErrFileObj *os.File
	// 异步写日志 管道
	LogChan   chan *LogMsg
	CheckTime time.Duration
}

func NewFileLogger

func NewFileLogger(level LogLevel, fp, fn string, options ...LogoHandleFunc) *FileLogger

FileLogger 构造函数

func (*FileLogger) CheckFileCount

func (fl *FileLogger) CheckFileCount()

func (*FileLogger) CheckFileMaxAge

func (fl *FileLogger) CheckFileMaxAge()

func (*FileLogger) CheckFileSize

func (fl *FileLogger) CheckFileSize(file *os.File) bool

func (*FileLogger) CheckSplitFileAge

func (fl *FileLogger) CheckSplitFileAge(now time.Time) bool

func (*FileLogger) Debug

func (fl *FileLogger) Debug(msg string)

func (*FileLogger) Debugf

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

func (*FileLogger) Error

func (fl *FileLogger) Error(msg string)

func (*FileLogger) Errorf

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

func (*FileLogger) Fatal

func (fl *FileLogger) Fatal(msg string)

func (*FileLogger) Fatalf

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

func (*FileLogger) Info

func (fl *FileLogger) Info(msg string)

func (*FileLogger) Infof

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

func (*FileLogger) JsonEncode

func (fl *FileLogger) JsonEncode(logMsg *LogMsg) []byte

func (*FileLogger) Log

func (fl *FileLogger) Log(level LogLevel, format string, args ...interface{})

func (*FileLogger) RemoveMoreMaxAgeFile

func (fl *FileLogger) RemoveMoreMaxAgeFile(loc *time.Location)

func (*FileLogger) RemoveMoreMaxCountFile

func (fl *FileLogger) RemoveMoreMaxCountFile(loc *time.Location)

func (*FileLogger) SetEncoder

func (fl *FileLogger) SetEncoder(encode encoder)

func (*FileLogger) SetFileInfo

func (fl *FileLogger) SetFileInfo(level LogLevel)

func (*FileLogger) SetPrefix

func (fl *FileLogger) SetPrefix(prefix string)

func (*FileLogger) SetTimeFormat

func (fl *FileLogger) SetTimeFormat(format string)

func (*FileLogger) TextEncode

func (fl *FileLogger) TextEncode(logMsg *LogMsg) []byte

func (*FileLogger) Trace

func (fl *FileLogger) Trace(msg string)

func (*FileLogger) Tracef

func (fl *FileLogger) Tracef(format string, args ...interface{})

func (*FileLogger) Warning

func (fl *FileLogger) Warning(msg string)

func (*FileLogger) Warningf

func (fl *FileLogger) Warningf(format string, args ...interface{})

type LogLevel

type LogLevel uint8
const (
	UnKnow LogLevel = iota
	DebugLevel
	TraceLevel
	InfoLevel
	WarningLevel
	ErrorLevel
	FatalLevel
)

func ParseLogLevel

func ParseLogLevel(s string) (LogLevel, error)

func (LogLevel) String

func (ll LogLevel) String() string

type LogMsg

type LogMsg struct {
	Level        LogLevel  `json:"level"`
	Msg          string    `json:"msg"`
	LogTime      time.Time `json:"log_time"`
	FileLineInfo string    `json:"file_line_info"`
}

type Logger

type Logger interface {
	Debug(msg string)
	Trace(msg string)
	Info(msg string)
	Warning(msg string)
	Error(msg string)
	Fatal(msg string)
	Debugf(format string, args ...interface{})
	Tracef(format string, args ...interface{})
	Infof(format string, args ...interface{})
	Warningf(format string, args ...interface{})
	Errorf(format string, args ...interface{})
	Fatalf(format string, args ...interface{})
	SetPrefix(prefix string)
	SetTimeFormat(format string)
	SetFileInfo(level LogLevel)
	SetEncoder(encode encoder)
}

type LogoHandleFunc

type LogoHandleFunc interface {
	Configure(*FileLogger) error
}

func WithCheckTime

func WithCheckTime(t time.Duration) LogoHandleFunc

func WithEncoder

func WithEncoder(encode encoder) LogoHandleFunc

func WithMaxAge

func WithMaxAge(maxAge int) LogoHandleFunc

func WithMaxCount

func WithMaxCount(count int, errCount int) LogoHandleFunc

func WithMaxFileSize

func WithMaxFileSize(size int64) LogoHandleFunc

func WithSplitAge

func WithSplitAge(splitFileAge int) LogoHandleFunc

type MaxFileCount

type MaxFileCount struct {
	FileCount    int
	ErrFileCount int
}

type OptionFn

type OptionFn func(*FileLogger) error

func (OptionFn) Configure

func (o OptionFn) Configure(fl *FileLogger) error

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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