Since rotatefile.Writer implements the io.Writer interface, you can use it with the standard Go log/slog package (Go 1.21+) to get advanced file rotation features while maintaining the standard API.
package main
import (
"log/slog"
"github.com/gookit/rotatefile"
)
func main() {
// Configure rotation via rotatefile.NewConfig
w, err := rotatefile.NewConfig("testdata/std_slog.log", func(c *rotatefile.Config) {
c.MaxSize = 50 * 1024 * 1024 // 50MB
c.RotateTime = rotatefile.EveryDay
c.BackupNum = 7
}).Create()
if err != nil {
panic(err)
}
// Plug the rotatefile writer into a standard slog handler
logger := slog.New(slog.NewJSONHandler(w, nil))
logger.Info("log message via std log/slog", "key", "value")
}
package main
import (
"log/slog"
"github.com/gookit/rotatefile"
)
func main() {
// rotate by size/time + clean + gzip, all from rotatefile config
w, err := rotatefile.NewConfig("testdata/std_slog.log", func(c *rotatefile.Config) {
c.MaxSize = 50 * 1024 * 1024 // 50MB
c.RotateTime = rotatefile.EveryDay
c.BackupNum = 7
}).Create()
if err != nil {
panic(err)
}
logger := slog.New(slog.NewJSONHandler(w, nil))
logger.Info("log message via std log/slog", "key", "value")
}