fileroller

package module
v0.0.0-...-0461068 Latest Latest
Warning

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

Go to latest
Published: Mar 19, 2019 License: MIT Imports: 4 Imported by: 0

README

fileroller

Build Status Coverage Status Go Report Card GoDoc

Package fileroller is a package capable rolling files when they becoming > a certain size The fileroller has the following features: - Log rolling with configurable size - Configurable number of logs

Download:

go get github.com/1800alex/go-utilities-fileroller

Package fileroller is a package capable rolling files when they becoming > a certain size The fileroller has the following features:

- Log rolling with configurable size
- Configurable number of logs

Examples

FileRoller Roll Code:

{
	logfile := "out.log"
	fr, err := NewFileRoller(logfile, 1024, 3)
	if err != nil {
		fmt.Println(err)
		return
	}
	for i := 0; i < 100; i++ {
		testString := time.Now().Format(time.RFC850) + ": test log message\n"
		fr.Roll(len(testString))
		var f afero.File
		f, err = DepFS.OpenFile(logfile, os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0666)
		if err != nil {
			return
		}
		defer f.Close()
		w := bufio.NewWriter(f)
		var n int
		n, err = w.WriteString(testString)
		if err != nil {
			fmt.Println(err)
			return
		}
		if n != len(testString) {
			fmt.Println("failed to write log")
			return
		}
		w.Flush()
	}
}

Documentation

Overview

Package fileroller is a package capable rolling files when they becoming > a certain size The fileroller has the following features:

  • Log rolling with configurable size
  • Configurable number of logs

Index

Examples

Constants

This section is empty.

Variables

View Source
var DepFS = afero.NewOsFs()

DepFS is an external dependancy variable set to an instance of afero.FS.

View Source
var (
	// ErrInvalidFile is the error returned when the file specified
	// is not valid.
	ErrInvalidFile = errors.New("invalid file")
)

Functions

This section is empty.

Types

type FileRoller

type FileRoller struct {
	FileName   string
	MaxSize    int64
	LogsToKeep int
}

FileRoller defines a custom FileRoller object.

func NewFileRoller

func NewFileRoller(file string, maxSize int64, logsToKeep int) (obj *FileRoller, err error)

NewFileRoller creates a new FileRoller object.

func (*FileRoller) Roll

func (obj *FileRoller) Roll(bytesToAdd int) (rolled bool, err error)

Roll flips all log files if the current size + bytesToAdd is > the configured MaxSize.

Example
logfile := "out.log"
fr, err := NewFileRoller(logfile, 1024, 3)

if err != nil {
	fmt.Println(err)
	return
}

for i := 0; i < 100; i++ {
	testString := time.Now().Format(time.RFC850) + ": test log message\n"

	// Will roll log if it exceeds current size + new string > 1024 bytes
	fr.Roll(len(testString))

	var f afero.File

	f, err = DepFS.OpenFile(logfile, os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0666)

	if err != nil {
		return
	}

	defer f.Close()

	w := bufio.NewWriter(f)

	var n int
	n, err = w.WriteString(testString)

	if err != nil {
		fmt.Println(err)
		return
	}

	if n != len(testString) {
		fmt.Println("failed to write log")
		return
	}

	w.Flush()
}
Output:

Jump to

Keyboard shortcuts

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