Skip to content

yoannduc/dailyrotate

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

28 Commits
 
 
 
 
 
 
 
 

Repository files navigation

Dailyrotate

Dailyrotate is a daily rotating file writer for go (golang). It is based on top of os.OpenFile and implements io.Writer interface. Dailyrotate also cleans the directory to always match the number of rotated files you want.

Why

Some applications need rotating file, daily rotating files instead of weight based rotation, such as some web app logs.

Usage

Let's start with a trivial example:

package main

import "github.com/yoannduc/dailyrotate"

func main() {
    // Instanciate a new RotateWriter
    rw, err := dailyrotate.New("/tmp/myfile", -1)
    if err != nil {
        // Handle error your own way
    }

    // Check if RotateWriter should rotate
    if rw.ShouldRotate() {
        // Perform the rotation
        err = rw.Rotate()
        if err != nil {
            // Handle error your own way
        }
    }

    // Write to the RotateWriter
    rw.Write([]byte("Some data"))
}

Or with short convenience methods:

package main

import "github.com/yoannduc/dailyrotate"

func main() {
    // Instanciate a new RotateWriter with default params
    rw, err := dailyrotate.NewWithDefaults()
    if err != nil {
        // Handle error your own way
    }

    // Write to the RotateWriter
    // Rotate if needed before writing to the file
    err = rw.RotateWrite([]byte("Some data"))
    if err != nil {
        // Handle error your own way
    }
}

Params

Instanciation is done with two params:

  • FilePath a string indicationg path to the rotating file. Must be absolute.

  • MaxAge an int indicating the number of rotated files to keep after rotation. To keep all files without limitation, must be -1. Note that MaxAge works with time, not with number of files, so passing 3 will keep last 3 days, not last 3 file.

Default params

Dailyrotate can be instanciated with default params. If done so, FilePath wil be "/tmp/rotating.log" and MaxAge will be 7.