README

dailyrotate is a Go package for rotating (log) files daily.

Documentation:

Documentation

Overview

Package dailyrotate provides a file that is rotated daily (at midnight in specified location).

You provide a pattern for a file path. That pattern will be formatted with time.Format to generate a real path. It should be unique for each day e.g. 2006-01-02.txt.

You Write to a file and the code takes care of closing existing file and opening a new file when we're crossing daily boundaries.

For a full tutorial see https://presstige.io/p/dailyrotate-a-Go-library-for-rotating-files-daily-6fb283a94e604f879e0d97a5f788dee6

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type File

type File struct {
	sync.Mutex

	Location *time.Location
	// contains filtered or unexported fields
}

File describes a file that gets rotated daily

func NewFile

func NewFile(pathFormat string, onClose func(path string, didRotate bool)) (*File, error)

NewFile creates a new file that will be rotated daily (at midnight in specified location). pathFormat is file format accepted by time.Format that will be used to generate a name of the file. It should be unique in a given day e.g. 2006-01-02.txt. If you need more flexibility, use NewFileWithPathGenerator which accepts a function that generates a file path. onClose is an optional function that will be called every time existing file is closed, either as a result calling Close or due to being rotated. didRotate will be true if it was closed due to rotation. If onClose() takes a long time, you should do it in a background goroutine (it blocks all other operations, including writes) Warning: time.Format might format more than you expect e.g. time.Now().Format(`/logs/dir-2/2006-01-02.txt`) will change "-2" in "dir-2" to current day. For better control over path generation, use NewFileWithPathGenerator

func NewFileWithPathGenerator

func NewFileWithPathGenerator(pathGenerator func(time.Time) string, onClose func(path string, didRotate bool)) (*File, error)

NewFileWithPathGenerator creates a new file that will be rotated daily (at midnight in timezone specified by in specified location). pathGenerator is a function that will return a path for a daily log file. It should be unique in a given day e.g. time.Format of "2006-01-02.txt" creates a string unique for the day. onClose is an optional function that will be called every time existing file is closed, either as a result calling Close or due to being rotated. didRotate will be true if it was closed due to rotation. If onClose() takes a long time, you should do it in a background goroutine (it blocks all other operations, including writes)

func (*File) Close

func (f *File) Close() error

Close closes the file

func (*File) Flush

func (f *File) Flush() error

Flush flushes the file

func (*File) Path

func (f *File) Path() string

Path returns full path of the file we're currently writing to

func (*File) Write

func (f *File) Write(d []byte) (int, error)

Write writes data to a file

func (*File) Write2

func (f *File) Write2(d []byte, flush bool) (string, int64, int, error)

Write2 writes data to a file, optionally flushes. To enable users to later seek to where the data was written, it returns name of the file where data was written, offset at which the data was written, number of bytes and error