Documentation ¶
Index ¶
- Constants
- Variables
- type CConfig
- type ClockFn
- type Clocker
- type Config
- type ConfigFn
- type FilesClear
- type RotateTime
- type RotateWriter
- type Writer
- func (d *Writer) Clean() (err error)
- func (d *Writer) Close() error
- func (d *Writer) Config() Config
- func (d *Writer) Flush() error
- func (d *Writer) ReopenFile() error
- func (d *Writer) Rotate() (err error)
- func (d *Writer) Sync() error
- func (d *Writer) Write(p []byte) (n int, err error)
- func (d *Writer) WriteString(s string) (n int, err error)
Examples ¶
Constants ¶
const ( // OneMByte size OneMByte uint64 = 1024 * 1024 // DefaultMaxSize of a log file. default is 20M. DefaultMaxSize = 20 * OneMByte // DefaultBackNum default backup numbers for old files. DefaultBackNum uint = 20 // DefaultBackTime default backup time for old files. default keep a week. DefaultBackTime uint = 24 * 7 )
Variables ¶
var ( // DefaultFilePerm perm and flags for create log file DefaultFilePerm os.FileMode = 0664 // DefaultFileFlags for open log file DefaultFileFlags = os.O_CREATE | os.O_WRONLY | os.O_APPEND // DefaultFilenameFn default new filename func DefaultFilenameFn = func(filepath string, rotateNum uint) string { suffix := time.Now().Format("010215") return filepath + fmt.Sprintf(".%s_%04d", suffix, rotateNum) } // DefaultTimeClockFn for create time DefaultTimeClockFn = ClockFn(func() time.Time { return time.Now() }) )
Functions ¶
This section is empty.
Types ¶
type CConfig ¶ added in v0.3.2
type CConfig struct { // BackupNum max number for keep old files. // 0 is not limit, default is 20. BackupNum uint `json:"backup_num" yaml:"backup_num"` // BackupTime max time for keep old files, unit is hours. // // 0 is not limit, default is a week. BackupTime uint `json:"backup_time" yaml:"backup_time"` // 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"` // FileDirs list FileDirs []string `json:"file_dirs" yaml:"file_dirs"` // TimeClock for clean files TimeClock Clocker }
CConfig struct for clean files
func (*CConfig) AddFileDir ¶ added in v0.3.3
AddFileDir for clean
type Config ¶
type Config struct { // Filepath the log file path, will be rotating Filepath string `json:"filepath" yaml:"filepath"` // MaxSize file contents max size, unit is bytes. // If is equals zero, disable rotate file by size // // default see DefaultMaxSize MaxSize uint64 `json:"max_size" yaml:"max_size"` // RotateTime the file rotate interval time, unit is seconds. // If is equals zero, disable rotate file by time // // default see EveryHour RotateTime RotateTime `json:"rotate_time" yaml:"rotate_time"` // CloseLock use sync lock on write contents, rotating file. // // default: false CloseLock bool `json:"close_lock" yaml:"close_lock"` // BackupNum max number for keep old files. // // 0 is not limit, default is DefaultBackNum BackupNum uint `json:"backup_num" yaml:"backup_num"` // BackupTime max time for keep old files, unit is hours. // // 0 is not limit, default is DefaultBackTime BackupTime uint `json:"backup_time" yaml:"backup_time"` // 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"` // RenameFunc you can custom-build filename for rotate file by size. // // default see DefaultFilenameFn RenameFunc func(filePath string, rotateNum uint) string // TimeClock for rotate TimeClock Clocker }
Config struct for rotate dispatcher
func EmptyConfigWith ¶
EmptyConfigWith new empty config with custom func
type FilesClear ¶ added in v0.3.2
type FilesClear struct {
// contains filtered or unexported fields
}
FilesClear multi files by time. TODO use for rotate and clear other program produce log files
func NewFilesClear ¶ added in v0.3.3
func NewFilesClear(cfg *CConfig) *FilesClear
NewFilesClear instance
func (*FilesClear) Clean ¶ added in v0.3.2
func (r *FilesClear) Clean() (err error)
Clean old files by config
func (*FilesClear) DaemonClean ¶ added in v0.3.2
func (r *FilesClear) DaemonClean()
DaemonClean daemon clean old files by config
func (*FilesClear) WithConfigFn ¶ added in v0.3.3
func (r *FilesClear) WithConfigFn(fn func(c *CConfig)) *FilesClear
WithConfigFn for custom settings
type RotateTime ¶
type RotateTime int
RotateTime for rotate file. unit is seconds.
EveryDay:
- "error.log.20201223"
EveryHour, Every30Min, EveryMinute:
- "error.log.20201223_1500"
- "error.log.20201223_1530"
- "error.log.20201223_1523"
const ( EveryDay RotateTime = timex.OneDaySec EveryHour RotateTime = timex.OneHourSec Every30Min RotateTime = 30 * timex.OneMinSec Every15Min RotateTime = 15 * timex.OneMinSec EveryMinute RotateTime = timex.OneMinSec EverySecond RotateTime = 1 // only use for tests )
built in rotate time consts
func (RotateTime) FirstCheckTime ¶
func (rt RotateTime) FirstCheckTime(now time.Time) int64
FirstCheckTime for rotate file. will automatically align the time from the start of each hour.
func (RotateTime) TimeFormat ¶
func (rt RotateTime) TimeFormat() (suffixFormat string)
TimeFormat get log file suffix format
EveryDay:
- "error.log.20201223"
EveryHour, Every30Min, EveryMinute:
- "error.log.20201223_1500"
- "error.log.20201223_1530"
- "error.log.20201223_1523"
type RotateWriter ¶
type RotateWriter interface { io.WriteCloser Clean() error Flush() error Rotate() error Sync() error }
RotateWriter interface
type Writer ¶
type Writer struct {
// contains filtered or unexported fields
}
Writer a flush, close, writer and support rotate file.
refer https://github.com/flike/golog/blob/master/filehandler.go
func NewWriter ¶
NewWriter create rotate dispatcher with config.
Example (On_other_logger) ¶
package main import ( "log" "github.com/gookit/slog/rotatefile" ) func main() { logFile := "testdata/another_logger.log" writer, err := rotatefile.NewConfig(logFile).Create() if err != nil { panic(err) } log.SetOutput(writer) log.Println("log message") }
Output:
func NewWriterWith ¶
NewWriterWith create rotate writer with some settings.