Advanged Go! Log Rotation library implementation.
go get -u github.com/moisespsena-go/glogrotation
The 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.Monthly
or char'M'
glogrotation.Weekly
or char'W'
glogrotation.Daily
or char'D'
glogrotation.Hourly
or char'h'
glogrotation.Minutely
or char'm'
glogrotation.Yearly
or 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.