mylog

package module
v0.2.6 Latest Latest
Warning

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

Go to latest
Published: Aug 9, 2024 License: MIT Imports: 15 Imported by: 2

README

mylog

A log library based on Logrus that implements various custom configurations.

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, // Whether the error log is output to a separate file.
		DateSplit:           true, // Whether to split the log by date.
		MaxKeepDays:         1,   // The maximum number of days to keep the log.
	}
	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")
}

Configuration Options

type LogConfig struct {
	// Path for log storage.
	LogDir string
	// Log file name suffix
	LogFileNameSuffix string
	// Default log file name (ignored if split by date or size)
	DefaultLogName string
	// Separate error logs (for Error level and above)
	ErrSeparate bool
	// Exclude error logs from normal log file (when errors are separated)
	ErrNotInNormal bool
	// Split logs by date (cannot be used with size split)
	DateSplit bool
	// Disable file output for logs
	LogFileDisable bool
	// Disable console output for logs
	NoConsole bool
	// Disable timestamp in logs
	NoTimestamp bool
	// Timestamp format, default is 2006-01-02 15:04:05.000
	TimestampFormat string
	// Show short file path in console output
	ShowShortFileInConsole bool
	// Show function name in console output
	ShowFuncInConsole bool
	// Disable caller information
	DisableCaller bool
	// Disable write buffer
	DisableWriterBuffer bool
	// Write buffer size, default is 4096 bytes
	WriterBufferSize int
	// Output in JSON format
	JSONFormat bool
	// Disable color output
	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
	// Split logs by size in bytes (cannot be used with date split)
	MaxLogSize int64
	// Maximum retention days for logs. After enabling this, if the log folder path is not set, it defaults to DefaultSavePath.
	// Please do not place other files in the log folder, otherwise they may be deleted.
	MaxKeepDays int
	// Log file extension (default is .log)
	LogExt string
	// Log level (panic, fatal, error, warn, info, debug, trace)
	LogLevel string
	// Time zone
	TimeLocation *time.Location
	// Key for appending to each log entry
	key string
	// Value for appending to each log entry
	value interface{}
	// Suffix for log files that should not be deleted
	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"
)

logrus level

View Source
const DefaultSavePath = "./logs"

Default log folder path.

After enabling the maximum retention days for logs, if the log folder path is not set, it defaults to this path.

Variables

This section is empty.

Functions

func DeleteOldLog

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

Delete n days old logs, n equals 0 to delete all logs.

func FlushBuf deprecated

func FlushBuf(logger *logrus.Logger) error

Deprecated: You don't need to call this function now.

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 {
	// Path for log storage.
	LogDir string
	// Log file name suffix
	LogFileNameSuffix string
	// Default log file name (ignored if split by date or size)
	DefaultLogName string
	// Separate error logs (for Error level and above)
	ErrSeparate bool
	// Exclude error logs from normal log file (when errors are separated)
	ErrNotInNormal bool
	// Split logs by date (cannot be used with size split)
	DateSplit bool
	// Disable file output for logs
	LogFileDisable bool
	// Disable console output for logs
	NoConsole bool
	// Disable timestamp in logs
	NoTimestamp bool
	// Timestamp format, default is 2006-01-02 15:04:05.000
	TimestampFormat string
	// Show short file path in console output
	ShowShortFileInConsole bool
	// Show function name in console output
	ShowFuncInConsole bool
	// Disable caller information
	DisableCaller bool
	// Disable write buffer
	DisableWriterBuffer bool
	// Write buffer size, default is 4096 bytes
	WriterBufferSize int
	// Output in JSON format
	JSONFormat bool
	// Disable color output
	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
	// Split logs by size in bytes (cannot be used with date split)
	MaxLogSize int64
	// Maximum retention days for logs. After enabling this, if the log folder path is not set, it defaults to DefaultSavePath.
	// Please do not place other files in the log folder, otherwise they may be deleted.
	MaxKeepDays int
	// Log file extension (default is .log)
	LogExt string
	// Log level (panic, fatal, error, warn, info, debug, trace)
	LogLevel string
	// Time zone
	TimeLocation *time.Location
	// contains filtered or unexported fields
}

func (*LogConfig) SetKeyValue

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

SetKeyValue sets the key and value for appending to each log entry.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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