Skip to content

A file rotation writer(rotw) split by time span

License

Notifications You must be signed in to change notification settings

SnowWarri0r/rotw

Repository files navigation

rotw(File Rotation Writer)

What is it?

A file rotation writer which can write file split by time span

Features

  • Rotate by time span
  • Multiple time span selection
  • Max Keep files
  • Compatible with zapcore.WriteSyncer
  • Customizable rotate rule
  • Support Windows/Linux/macOS
  • Customize file write strategy
  • ...

Quick Start

classic style:

package main

import (
	"fmt"
	"time"

	"github.com/SnowWarri0r/rotw"
)

func main() {
	// rotate writer configuration
	rwo := &rotw.RotateWriterConfig{
		// max keep files
		KeepFiles: 2,
		// log file path
		LogPath: "log/test.log",
		// rotate rule
		Rule: "1min",
		// check file opened span
		CheckSpan: time.Second,
		// write buffer size
		BufSize: 4096,
	}
	// create a rotate writer
	rw, err := rotw.NewRotateWriter(rwo)
	if err != nil {
		panic(err)
	}
	// defer close the rotate writer
	defer func() {
		errClose := rw.Close()
		fmt.Printf("close err=%v\n", errClose)
	}()
	// write some data
	for i := 0; i < 100; i++ {
		_, err = rw.Write([]byte(fmt.Sprintf("hello world %d\n", i)))
		if err != nil {
			panic(err)
		}
		time.Sleep(time.Second)
	}
}

optional style:

package main

import (
	"fmt"
	"time"

	"github.com/SnowWarri0r/rotw"
)

func main() {
	// create a rotate writer
	rw, err := rotw.NewRotateWriterWithOpt(
		"log/test.log",
		rotw.WithRule("1min"),
		rotw.WithKeepFiles(2),
		rotw.WithCheckSpan(time.Second),
		rotw.WithBufSize(4096),
	)
	if err != nil {
		panic(err)
	}
	// defer close the rotate writer
	defer func() {
		errClose := rw.Close()
		fmt.Printf("close err=%v\n", errClose)
	}()
	// write some data
	for i := 0; i < 100; i++ {
		_, err = rw.Write([]byte(fmt.Sprintf("hello world %d\n", i)))
		if err != nil {
			panic(err)
		}
		time.Sleep(time.Second)
	}
}

About

A file rotation writer(rotw) split by time span

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages