Documentation
¶
Overview ¶
零、设置分割错误日志
- 如果设置了分离错误日志,则会在目录文件生成2006_01_02的文件夹,日期为log文件的创建时间。
- 错误日志文件名会在基础文件名后加上_error后缀。
一、既不设置按日期分割也不设置按大小分割(默认)
- 空配置会在目录生成文件default.log(如果设置了日志文件名则为设置的文件名)
二、设置了按日期分割
- 其它为默认配置则会在目录生成文件2006_01_02.log(设置日志文件名无效)
三、设置了按大小分割
- 其它为默认配置则会在目录生成文件2006_01_02_150405.log(设置日志文件名无效)
四、其他
- 文件夹设置名称为log的创建时间而不加上最后修改时间是因为怕程序运行崩溃后,最后修改时间没有被添加。 这样在设置了最大保存天数的情况下,会不太好处理。
- 设置了最大保存天数后,会在程序启动时启动一个goroutine来删除过期的日志文件(24小时检查一次)。 若有不想被删除的日志文件,可以在文件名后加上keep,如:2006_01_02_150405_keep.log,或者 在文件夹名后加上keep,如:2006_01_02_keep。
Index ¶
- Constants
- func DeleteOldLog(logger *logrus.Logger, n int) error
- func DirIsExist(path string) bool
- func FlushBuf(logger *logrus.Logger) error
- func InitGlobalLogger(config LogConfig) error
- func NewLogger(config LogConfig) (*logrus.Logger, error)
- func PraseLevel(level string) logrus.Level
- type LogConfig
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 InitGlobalLogger ¶
InitGlobalLogger initializes the global logger.The global logger is the default logger of logrus.
func PraseLevel ¶
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 ¶
在每条log末尾添加key-value
Click to show internal directories.
Click to hide internal directories.