Is a simple revolving file writer for go.
Revolver allows for a simple log file rotation setup:
- Writing dir
- File prefix
- File middle name
- File suffix
- Max file size
- Max files
...can be customized.
func main() {
w, err := revolver.NewQuick(
"logs",
"log_",
".txt",
revolver.DateStringMiddle,
1024*1024,
3,
)
if err != nil {
panic(err)
}
Log := log.New(w, "", log.Ldate|log.Ltime|log.Lshortfile)
Log.Printf("Ready to use...")
}
Alternatively must can be used to get a writer or panic:
var (
count = 0
w = revolver.Must(revolver.NewQuick(
"logs",
"log_",
".txt",
revolver.DateStringMiddle,
1024*1024,
3,
))
Log = log.New(w, "", log.Ldate|log.Ltime|log.Lshortfile)
)
func main() {
Log.Printf("Ready to use...")
}
A different use case would be:
func main() {
count := 0
w, err := revolver.NewQuick(
"exports",
"export_",
".json",
func() string {
count++
return strconv.Itoa(count)
},
1024*1024,
10,
)
if err != nil {
panic(err)
}
defer w.Close()
w.Write([]byte("Some json data..."))
}
Specifies the directory to write to. If the directory dose not exist, it and all parents will be created.
The prefix is mandatory and will be used to determent which files can be deleted.
In case the next generated filename already exists revolver will append a number to this filename. E. g. the file export.json already exists export.json_1 will be created. But now the file extension would be broken. To remedy that a filename suffix can be specified. All files are guaranteed to end with this suffix.
The middle name of the file can be customized in this function.
Specifies the maximum bytes size a file can have. If the data to be written is larger then the remaining file size, a new file will be created.
This is the limit of files that will be created.
Revolver is tested on Linux and Mac. On Windows the package seems to work. However the tests won't pass and since the returned errors are windows language specific there is no point in fixing them.