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 ¶
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:
Click to show internal directories.
Click to hide internal directories.