Advanged Go! Log Rotation library implementation.
go get -u github.com/moisespsena-go/glogrotationThe Rotator struct is the concurrent io.Writer implementation.
import library:
import "github.com/moisespsena-go/glogrotation"Basic:
rotator := glogrotation.New("app.log")With custom options:
rotator := glogrotation.New("app.log", glogrotation.Options{})The Options structure:
type Options struct {
HistoryPath string // path inside root
MaxSize int64 // -1 disables limitter
Duration RotationDuration // Default is glogrotation.MaxSize (10M)
FileMode os.FileMode
DirMode os.FileMode
HistoryDir string
HistoryCount int
}Options.Duration field values:
glogrotation.Monthlyor char'M'glogrotation.Weeklyor char'W'glogrotation.Dailyor char'D'glogrotation.Hourlyor char'h'glogrotation.Minutelyor char'm'glogrotation.Yearlyor char'Y'
Load Options From config file:
var opt glogrotation.Options
if f, err := os.Open("options.yml"); err == nil {
var cfg glogrotation.Config
if err = yaml.NewDecoder(f).Decode(&cfg); err != nil {
panic(err)
}
if opt, err = cfg.Options(); err != nil {
panic(err)
}
} else {
panic(err)
}
rotator := glogrotation.New("app.log", opt)Direct write:
n, err := rotator.Write([]byte(`message\n`)) using io.Copy:
var r io.Reader = os.Stdin // your reader
err := io.Copy(rotator, r) See to cli source code for example.