Documentation ¶
Overview ¶
Package rotatefile provides a rolling logger.
rotatefile is intended to be one part of a logging infrastructure. It is not an all-in-one solution, but instead is a pluggable component at the bottom of the logging stack that simply controls the files to which logs are written.
rotatefile plays well with any logging package that can write to an io.Writer, including the standard library's log package.
rotatefile assumes that only one process is writing to the output files. Using the same rotatefile configuration from multiple processes on the same machine will result in improper behavior.
Example ¶
To use rotatefile with the standard library's log package, just pass it into the SetOutput function when your application starts.
log.SetOutput(New())
Output:
Index ¶
- Constants
- Variables
- func Debugf(format string, a ...interface{})
- func Env(envName, defaultValue string) string
- func EnvBool(envName string, defaultValue bool) bool
- func EnvInt(envName string, defaultValue int) int
- func EnvSignals(envName string, defaultValue []os.Signal) []os.Signal
- func EnvSize(envName string, defaultValue uint64) uint64
- func FindLogDir(appName, logDir string) string
- func GenerateFilename(appName, prefix, filename string, tryLock bool) (string, *flock.Flock)
- func GetFilename() string
- func HomeDir() (string, error)
- func IsDirWritable(dir string) bool
- func MkdirAll(dir string, mod os.FileMode) error
- func ParseBytes(s string) (uint64, error)
- type Config
- type ConfigFn
- func WithAppName(v string) ConfigFn
- func WithCompress(v bool) ConfigFn
- func WithConfig(v Config) ConfigFn
- func WithFilename(v string) ConfigFn
- func WithMaxBackups(v int) ConfigFn
- func WithMaxDays(v int) ConfigFn
- func WithMaxSize(v uint64) ConfigFn
- func WithMinDiskFree(v uint64) ConfigFn
- func WithPrefix(v string) ConfigFn
- func WithPrintTerm(v bool) ConfigFn
- func WithRotateSignals(v ...os.Signal) ConfigFn
- func WithTotalSizeCap(v uint64) ConfigFn
- func WithUtcTime(v bool) ConfigFn
- type RotateFile
Examples ¶
Constants ¶
const ( // Byte 1个字节 Byte = 1 << (iota * 10) KiByte MiByte GiByte TiByte PiByte EiByte IByte = 1 KByte = IByte * 1000 MByte = KByte * 1000 GByte = MByte * 1000 TByte = GByte * 1000 PByte = TByte * 1000 EByte = PByte * 1000 )
const ( // MB is mega MB = 1024 * 1024 // GB is giga GB = 1024 * MB )
const DAY = 24 * time.Hour
Variables ¶
var ErrMismatched = errors.New("mismatched extension/prefix/backup time format")
ErrMismatched defines the error type for mismatched time format.
var IsTerminal = term.IsTerminal(1)
IsTerminal tell is if it is on a terminal.
Functions ¶
func EnvSignals ¶
EnvSignals 解析环境变量设置的信号
func FindLogDir ¶
FindLogDir 寻找日志合理的写入目录 0. 配置指定目录 /var/log/xxx/ 1. $HOME/log/{appName}/{appName}_{appWorkDirBase}.log 2. $PWD/log/{appName}_{appWorkDirBase}.log 3. /var/log/apps/{appName}/{appName}_{appWorkDirBase}.log 4. $TMPDIR/{appName}/{appName}_{appWorkDirBase}.log
func GenerateFilename ¶
GenerateFilename 根据 filename 生成完整的 1. filename 为 /some/path/xxx.log, 则继续保持 2. filename 为 /some/path/, 则补齐日志文件名为: {appName}{currentDirBase}.log 3. filename 为 空, 则根据 FindLogDir 生成指定的日志目录,日志文件名见上
func ParseBytes ¶
ParseBytes parses a string representation of bytes into the number of bytes it represents.
See Also: Bytes, IBytes.
ParseBytes("42 MB") -> 42000000, nil ParseBytes("42 mib") -> 44040192, nil
Types ¶
type Config ¶
type Config struct { // AppName 定义日志文件的基础文件名,默认 filepath.Base(os.Args[0]) AppName string `json:"appName" yaml:"appName"` // Filename is the file to write logs to. Backup log files will be retained // in the same directory. It uses <processname>.log in os.TempDir() if empty. Filename string `json:"filename" yaml:"filename"` // Prefix 是日志基本文件名前缀,在 Filename 不指定的情况下,可以使用本字段给自动生成的日志文件名添加此前缀 Prefix string `json:"prefix" yaml:"prefix"` // RotateSignals 设置滚动日志的信号 RotateSignals []os.Signal `json:"-" yaml:"-"` // MaxSize is the maximum size of the log file before it gets // rotated. It defaults to 100 megabytes. MaxSize uint64 `json:"maxSize" yaml:"maxSize"` // MaxDays is the maximum number of days to retain old log files based on the // timestamp encoded in their filename. Note that a day is defined as 24 // hours and may not exactly correspond to calendar days due to daylight // savings, leap seconds, etc. The default is not to remove old log files // based on age. MaxDays int `json:"maxDays" yaml:"maxDays"` // MaxBackups is the maximum number of old log files to retain. The default // is to retain all old log files (though MaxDays may still cause them to get // deleted.) MaxBackups int `json:"maxBackups" yaml:"maxBackups"` // TotalSizeCap 控制所有文件累积总大小 // 如果超过该大小,则从最早的文件开始删除,直到删除到当前文件为止 // 当前日志文件大小可以超过 TotalSizeCap // 0 不控制 TotalSizeCap uint64 `json:"totalSizeCap" yaml:"totalSizeCap"` // MinDiskFree 日志文件所在磁盘分区最少空余 MinDiskFree uint64 `json:"minDiskFree" yaml:"minDiskFree"` // UtcTime determines if the time used for formatting the timestamps in // backup files is the computer's local time. The default is to use UTC // time. UtcTime bool `json:"utcTime" yaml:"utcTime"` // Compress determines if the rotated log files should be compressed // using gzip. The default is not to perform compression. Compress bool `json:"compress" yaml:"compress"` // PrintTerm 是否同时在终端上输出,只有在终端可用时输出 PrintTerm bool `json:"printTerm" yaml:"printTerm"` }
Config 包括一些滚动文件的配置参数,所有参数,均有默认值,方便无脑集成
type ConfigFn ¶
type ConfigFn func(*Config)
ConfigFn 选项模式函数
func WithPrefix ¶
WithPrefix 指定日志基本文件名前缀,在 Filename 不指定的情况下,可以使用本字段给自动生成的日志文件名添加此前缀
type RotateFile ¶
type RotateFile interface { io.WriteCloser Rotate() error Flush() error // GetFilename 取得日志文件的距离路径 GetFilename() string }
RotateFile 滚动文件大小