Documentation
¶
Index ¶
Examples ¶
Constants ¶
View Source
const ( DefaultPermission = 0600 DefaultKeeps = 5 DefaultSize = 1024 * 1024 * 10 )
Default values
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type FileState ¶
type FileState struct {
// openedAt Unix time
OpenedAt int64
// file size (when opened) + written bytes
Size int64
}
FileState holds the state of the current write destination file
type OptionFunc ¶
type OptionFunc func(o *option)
OptionFunc let you change follow.Reader behavior.
func WithKeeps ¶
func WithKeeps(v int) OptionFunc
WithKeeps let you change keep count of the rotated files
func WithPermission ¶
func WithPermission(v os.FileMode) OptionFunc
WithPermission let you change the file permission
func WithSizeBasedPolicy ¶
func WithSizeBasedPolicy(size int64) OptionFunc
WithSizeBasedPolicy let you change the rotate policy
func WithTimeBasedPolicy ¶
func WithTimeBasedPolicy(fn func(openedAtUnix int64) bool) OptionFunc
WithTimeBasedPolicy let you change the rotate policy
type PolicyFunc ¶
PolicyFunc is a type of rotate policy function
func SizeBasedPolicy ¶
func SizeBasedPolicy(size int64) PolicyFunc
SizeBasedPolicy returns size based rotate policy
func TimeBasedPolicy ¶
func TimeBasedPolicy(fn func(openedAtUnix int64) bool) PolicyFunc
TimeBasedPolicy returns time based rotate policy
func (PolicyFunc) NeedRotate ¶
func (f PolicyFunc) NeedRotate(fileState FileState) bool
NeedRotate reports whether need rotate
type Writer ¶
type Writer struct {
// contains filtered or unexported fields
}
Writer is a rotating file writer
Example ¶
package main
import (
"fmt"
"io/ioutil"
"os"
"path/filepath"
"time"
"github.com/kei2100/rotate"
)
func main() {
dir, err := ioutil.TempDir("", "rotate-test")
if err != nil {
panic(err)
}
defer os.RemoveAll(dir)
const bytes3 int64 = 3
w, err := rotate.NewWriter(dir, "test.log", rotate.WithSizeBasedPolicy(bytes3))
if err != nil {
panic(err)
}
defer w.Close()
fmt.Fprint(w, "1")
fmt.Fprint(w, "2")
fmt.Fprint(w, "3")
time.Sleep(time.Second) // wait for rotate
fmt.Fprint(w, "4")
b0, _ := ioutil.ReadFile(filepath.Join(dir, "test.log"))
b1, _ := ioutil.ReadFile(filepath.Join(dir, "test.log.1"))
fmt.Printf("%s/%s", b0, b1)
}
Output: 4/123
Example (TimeBased) ¶
package main
import (
"fmt"
"io/ioutil"
"os"
"path/filepath"
"time"
"github.com/kei2100/rotate"
)
func main() {
dir, err := ioutil.TempDir("", "rotate-test")
if err != nil {
panic(err)
}
defer os.RemoveAll(dir)
w, err := rotate.NewWriter(dir, "test.log", rotate.WithTimeBasedPolicy(func(openedAtUnix int64) bool {
opendeAt := time.Unix(openedAtUnix, 0)
now := time.Now()
return now.Second()-opendeAt.Second() > 0 // rotate if 1 sec has passed
}))
if err != nil {
panic(err)
}
defer w.Close()
fmt.Fprint(w, "1")
time.Sleep(1 * time.Second)
fmt.Fprint(w, "2") // rotate will start after this writing
time.Sleep(1 * time.Second)
fmt.Fprint(w, "3") // rotate will start after this writing
time.Sleep(1 * time.Second)
fmt.Fprint(w, "4") // rotate will start after this writing
time.Sleep(1 * time.Second) // wait for rotate
b0, _ := ioutil.ReadFile(filepath.Join(dir, "test.log"))
b1, _ := ioutil.ReadFile(filepath.Join(dir, "test.log.1"))
b2, _ := ioutil.ReadFile(filepath.Join(dir, "test.log.2"))
b3, _ := ioutil.ReadFile(filepath.Join(dir, "test.log.3"))
fmt.Printf("%s/%s/%s/%s", b0, b1, b2, b3)
}
Output: /4/3/12
Click to show internal directories.
Click to hide internal directories.