rotate

package module
v0.0.3 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Oct 22, 2023 License: MIT Imports: 8 Imported by: 1

README

rotate

CircleCI Build status

A rotating file Writer

func ExampleWriter() {
	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
}

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

type PolicyFunc func(fileState FileState) bool

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

func NewWriter

func NewWriter(dir, filename string, opts ...OptionFunc) (*Writer, error)

NewWriter creates a *rotate.Writer

func (*Writer) Close

func (w *Writer) Close() error

Close closes the file and releases resources

func (*Writer) Write

func (w *Writer) Write(p []byte) (int, error)

Write implements io.Writer

Directories

Path Synopsis
internal

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL