mylog

package module
v0.2.3 Latest Latest
Warning

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

Go to latest
Published: Jul 12, 2024 License: MIT Imports: 14 Imported by: 2

README

mylog

一个基于logrus的日志库,实现了各种定制化配置。

QuickStart

go get -u github.com/doraemonkeys/mylog
package main

import (
	"github.com/doraemonkeys/mylog"
	"github.com/sirupsen/logrus"
)

func init() {
	config := mylog.LogConfig{
		LogDir:             `./test_log`,
		LogLevel:            "trace",
		ErrSeparate:         true, //错误日志是否单独输出到文件
		DateSplit:           true, //是否按日期分割日志
		MaxKeepDays:         1,
	}
	config.SetKeyValue("foo", "bar")
	err := mylog.InitGlobalLogger(config)
	if err != nil {
		panic(err)
	}
}

func main() {
	logrus.Trace("trace")
	logrus.Debug("debug")
	logrus.Info("info")
	logrus.Warn("warn")
	logrus.Error("error")
}

配置预览

type LogConfig struct {
	//日志保存文件夹路径(可以为空)
	LogDir string
	//日志文件名后缀
	LogFileNameSuffix string
	//默认日志文件名(若按日期或大小分割日志,此项无效)
	DefaultLogName string
	//是否分离错误日志(Error级别以上)
	ErrSeparate bool
	//使普通日志文件不包含错误日志(分离错误日志时有效)
	ErrNotInNormal bool
	//按日期分割日志(不能和按大小分割同时使用)
	DateSplit bool
	//取消日志输出到文件
	LogFileDisable bool
	//取消日志输出到控制台
	NoConsole bool
	//取消时间戳Timestamp
	NoTimestamp bool
	// 时间戳格式,默认 2006-01-02 15:04:05.000
	TimestampFormat string
	//在控制台输出shortfile
	ShowShortFileInConsole bool
	//在控制台输出func
	ShowFuncInConsole bool
	// 关闭调用者信息
	DisableCaller bool
	// 写缓冲大小,默认4096字节
	WriterBufferSize int
	// 以json格式输出
	JSONFormat bool
	// 禁用颜色
	DisableColors bool
	// Disables the truncation of the level text to 4 characters.
	DisableLevelTruncation bool
	// PadLevelText Adds padding the level text so that all the levels
	// output at the same length PadLevelText is a superset of the DisableLevelTruncation option
	PadLevelText bool
	//按大小分割日志,单位byte。(不能和按日期分割同时使用)
	MaxLogSize int64
	// 日志最大保留天数,设置后请不要在日志文件夹中放置其他文件,否则可能被删除。
	// 开启此功能后,如果没有设置日志文件夹路径,则默认为DefaultSavePath。
	MaxKeepDays int
	//日志扩展名(默认.log)
	LogExt string
	//panic,fatal,error,warn,info,debug,trace
	LogLevel string
	//时区
	TimeLocation *time.Location
	//在每条log末尾添加key-value
	key string
	//在每条log末尾添加key-value
	value interface{}
	// 标记不被删除的日志文件名需要含有的后缀
	keepSuffix string
}

Documentation

Overview

零、设置分割错误日志

  1. 如果设置了分离错误日志,则会在目录文件生成2006_01_02的文件夹,日期为log文件的创建时间。
  2. 错误日志文件名会在基础文件名后加上_error后缀。

一、既不设置按日期分割也不设置按大小分割(默认)

  1. 空配置会在目录生成文件default.log(如果设置了日志文件名则为设置的文件名)

二、设置了按日期分割

  1. 其它为默认配置则会在目录生成文件2006_01_02.log(设置日志文件名无效)

三、设置了按大小分割

  1. 其它为默认配置则会在目录生成文件2006_01_02_150405.log(设置日志文件名无效)

四、其他

  1. 文件夹设置名称为log的创建时间而不加上最后修改时间是因为怕程序运行崩溃后,最后修改时间没有被添加。 这样在设置了最大保存天数的情况下,会不太好处理。
  2. 设置了最大保存天数后,会在程序启动时启动一个goroutine来删除过期的日志文件(24小时检查一次)。 若有不想被删除的日志文件,可以在文件名后加上keep,如:2006_01_02_150405_keep.log,或者 在文件夹名后加上keep,如:2006_01_02_keep。

Index

Constants

View Source
const (
	PanicLevel = "panic"
	FatalLevel = "fatal"
	ErrorLevel = "error"
	WarnLevel  = "warn"
	InfoLevel  = "info"
	DebugLevel = "debug"
	TraceLevel = "trace"
)

日志级别

View Source
const DefaultSavePath = "./logs"

默认日志文件夹路径。

开启日志最大保留天数后,如果没有设置日志文件夹路径,则默认为此路径。

Variables

This section is empty.

Functions

func DeleteOldLog

func DeleteOldLog(logger *logrus.Logger, n int) error

删除n天前的日志,n等于0时删除所有日志。

func DirIsExist

func DirIsExist(path string) bool

文件夹是否存在

func FlushBuf

func FlushBuf(logger *logrus.Logger) error

func InitGlobalLogger

func InitGlobalLogger(config LogConfig) error

InitGlobalLogger initializes the global logger.The global logger is the default logger of logrus.

func NewLogger

func NewLogger(config LogConfig) (*logrus.Logger, error)

NewLogger creates a new logger.

func PraseLevel

func PraseLevel(level string) logrus.Level

panic,fatal,error,warn,info,debug,trace 默认info

Types

type LogConfig

type LogConfig struct {
	//日志保存文件夹路径(可以为空)
	LogDir string
	//日志文件名后缀
	LogFileNameSuffix string
	//默认日志文件名(若按日期或大小分割日志,此项无效)
	DefaultLogName string
	//是否分离错误日志(Error级别以上)
	ErrSeparate bool
	//使普通日志文件不包含错误日志(分离错误日志时有效)
	ErrNotInNormal bool
	//按日期分割日志(不能和按大小分割同时使用)
	DateSplit bool
	//取消日志输出到文件
	LogFileDisable bool
	//取消日志输出到控制台
	NoConsole bool
	//取消时间戳Timestamp
	NoTimestamp bool
	// 时间戳格式,默认 2006-01-02 15:04:05.000
	TimestampFormat string
	//在控制台输出shortfile
	ShowShortFileInConsole bool
	//在控制台输出func
	ShowFuncInConsole bool
	// 关闭调用者信息
	DisableCaller bool
	// 禁用写缓冲
	DisableWriterBuffer bool
	// 写缓冲大小,默认4096字节
	WriterBufferSize int
	// 以json格式输出
	JSONFormat bool
	// 禁用颜色
	DisableColors bool
	// Disables the truncation of the level text to 4 characters.
	DisableLevelTruncation bool
	// PadLevelText Adds padding the level text so that all the levels
	// output at the same length PadLevelText is a superset of the DisableLevelTruncation option
	PadLevelText bool
	//按大小分割日志,单位byte。(不能和按日期分割同时使用)
	MaxLogSize int64
	// 日志最大保留天数,设置后请不要在日志文件夹中放置其他文件,否则可能被删除。
	// 开启此功能后,如果没有设置日志文件夹路径,则默认为DefaultSavePath。
	MaxKeepDays int
	//日志扩展名(默认.log)
	LogExt string
	//panic,fatal,error,warn,info,debug,trace
	LogLevel string
	//时区
	TimeLocation *time.Location
	// contains filtered or unexported fields
}

日志配置,可以为空

func (*LogConfig) SetKeyValue

func (c *LogConfig) SetKeyValue(key string, value interface{})

在每条log末尾添加key-value

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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