2016-02-19 20:55:15 +00:00
|
|
|
package logging
|
|
|
|
|
|
|
|
import (
|
2016-02-23 16:51:21 +00:00
|
|
|
"bufio"
|
2016-02-19 20:55:15 +00:00
|
|
|
"fmt"
|
|
|
|
"io/ioutil"
|
|
|
|
"log"
|
|
|
|
"os"
|
|
|
|
"path/filepath"
|
|
|
|
"sort"
|
|
|
|
"strconv"
|
|
|
|
"strings"
|
2016-02-23 16:51:21 +00:00
|
|
|
"time"
|
2016-02-19 20:55:15 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
2016-02-23 16:51:21 +00:00
|
|
|
flushDur = 100 * time.Millisecond
|
2016-02-19 20:55:15 +00:00
|
|
|
)
|
|
|
|
|
2016-02-19 22:11:31 +00:00
|
|
|
// FileRotator writes bytes to a rotated set of files
|
2016-02-19 20:55:15 +00:00
|
|
|
type FileRotator struct {
|
2016-02-20 05:58:44 +00:00
|
|
|
MaxFiles int // MaxFiles is the maximum number of rotated files allowed in a path
|
|
|
|
FileSize int64 // FileSize is the size a rotated file is allowed to grow
|
|
|
|
|
|
|
|
path string // path is the path on the file system where the rotated set of files are opened
|
|
|
|
baseFileName string // baseFileName is the base file name of the rotated files
|
|
|
|
logFileIdx int // logFileIdx is the current index of the rotated files
|
|
|
|
oldestLogFileIdx int // oldestLogFileIdx is the index of the oldest log file in a path
|
|
|
|
|
|
|
|
currentFile *os.File // currentFile is the file that is currently getting written
|
|
|
|
currentWr int64 // currentWr is the number of bytes written to the current file
|
2016-02-23 17:46:10 +00:00
|
|
|
bufw *bufio.Writer
|
2016-02-19 20:55:15 +00:00
|
|
|
|
2016-02-23 16:51:21 +00:00
|
|
|
flushTicker *time.Ticker
|
|
|
|
logger *log.Logger
|
|
|
|
purgeCh chan struct{}
|
2016-02-23 17:43:14 +00:00
|
|
|
doneCh chan struct{}
|
2016-02-19 20:55:15 +00:00
|
|
|
}
|
|
|
|
|
2016-02-19 22:11:31 +00:00
|
|
|
// NewFileRotator returns a new file rotator
|
2016-02-19 22:01:07 +00:00
|
|
|
func NewFileRotator(path string, baseFile string, maxFiles int,
|
|
|
|
fileSize int64, logger *log.Logger) (*FileRotator, error) {
|
2016-02-19 20:55:15 +00:00
|
|
|
rotator := &FileRotator{
|
|
|
|
MaxFiles: maxFiles,
|
|
|
|
FileSize: fileSize,
|
|
|
|
|
|
|
|
path: path,
|
|
|
|
baseFileName: baseFile,
|
|
|
|
|
2016-02-23 16:51:21 +00:00
|
|
|
flushTicker: time.NewTicker(flushDur),
|
|
|
|
logger: logger,
|
|
|
|
purgeCh: make(chan struct{}, 1),
|
2016-02-23 17:43:14 +00:00
|
|
|
doneCh: make(chan struct{}, 1),
|
2016-02-19 20:55:15 +00:00
|
|
|
}
|
|
|
|
if err := rotator.lastFile(); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
go rotator.purgeOldFiles()
|
2016-02-23 16:51:21 +00:00
|
|
|
go rotator.flushPeriodically()
|
2016-02-19 20:55:15 +00:00
|
|
|
return rotator, nil
|
|
|
|
}
|
|
|
|
|
2016-02-19 22:11:31 +00:00
|
|
|
// Write writes a byte array to a file and rotates the file if it's size becomes
|
|
|
|
// equal to the maximum size the user has defined.
|
2016-02-19 20:55:15 +00:00
|
|
|
func (f *FileRotator) Write(p []byte) (n int, err error) {
|
|
|
|
n = 0
|
|
|
|
var nw int
|
|
|
|
|
|
|
|
for n < len(p) {
|
2016-02-20 05:58:44 +00:00
|
|
|
// Check if we still have space in the current file, otherwise close and
|
2016-02-19 22:11:31 +00:00
|
|
|
// open the next file
|
2016-02-19 20:55:15 +00:00
|
|
|
if f.currentWr >= f.FileSize {
|
2016-02-23 17:46:10 +00:00
|
|
|
f.bufw.Flush()
|
2016-02-19 20:55:15 +00:00
|
|
|
f.currentFile.Close()
|
|
|
|
if err := f.nextFile(); err != nil {
|
|
|
|
return 0, err
|
|
|
|
}
|
|
|
|
}
|
2016-02-20 05:58:44 +00:00
|
|
|
// Calculate the remaining size on this file
|
2016-02-19 20:55:15 +00:00
|
|
|
remainingSize := f.FileSize - f.currentWr
|
2016-02-20 05:58:44 +00:00
|
|
|
|
|
|
|
// Check if the number of bytes that we have to write is less than the
|
2016-02-19 22:11:31 +00:00
|
|
|
// remaining size of the file
|
2016-02-19 20:55:15 +00:00
|
|
|
if remainingSize < int64(len(p[n:])) {
|
2016-02-20 05:58:44 +00:00
|
|
|
// Write the number of bytes that we can write on the current file
|
2016-02-19 20:55:15 +00:00
|
|
|
li := int64(n) + remainingSize
|
2016-02-23 17:46:10 +00:00
|
|
|
nw, err = f.bufw.Write(p[n:li])
|
2016-02-23 16:51:21 +00:00
|
|
|
//nw, err = f.currentFile.Write(p[n:li])
|
2016-02-19 20:55:15 +00:00
|
|
|
} else {
|
2016-02-20 05:58:44 +00:00
|
|
|
// Write all the bytes in the current file
|
2016-02-23 17:46:10 +00:00
|
|
|
nw, err = f.bufw.Write(p[n:])
|
2016-02-23 16:51:21 +00:00
|
|
|
//nw, err = f.currentFile.Write(p[n:])
|
2016-02-19 20:55:15 +00:00
|
|
|
}
|
2016-02-19 22:11:31 +00:00
|
|
|
|
2016-02-20 05:58:44 +00:00
|
|
|
// Increment the number of bytes written so far in this method
|
|
|
|
// invocation
|
2016-02-19 20:55:15 +00:00
|
|
|
n += nw
|
2016-02-20 05:58:44 +00:00
|
|
|
|
|
|
|
// Increment the total number of bytes in the file
|
2016-02-19 20:55:15 +00:00
|
|
|
f.currentWr += int64(n)
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2016-02-19 22:11:31 +00:00
|
|
|
// nextFile opens the next file and purges older files if the number of rotated
|
|
|
|
// files is larger than the maximum files configured by the user
|
2016-02-19 20:55:15 +00:00
|
|
|
func (f *FileRotator) nextFile() error {
|
|
|
|
nextFileIdx := f.logFileIdx
|
|
|
|
for {
|
|
|
|
nextFileIdx += 1
|
|
|
|
logFileName := filepath.Join(f.path, fmt.Sprintf("%s.%d", f.baseFileName, nextFileIdx))
|
|
|
|
if fi, err := os.Stat(logFileName); err == nil {
|
2016-02-20 05:58:44 +00:00
|
|
|
if fi.IsDir() || fi.Size() >= f.FileSize {
|
2016-02-19 20:55:15 +00:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
}
|
|
|
|
f.logFileIdx = nextFileIdx
|
|
|
|
if err := f.createFile(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
break
|
|
|
|
}
|
|
|
|
// Purge old files if we have more files than MaxFiles
|
|
|
|
if f.logFileIdx-f.oldestLogFileIdx >= f.MaxFiles {
|
|
|
|
select {
|
2016-02-19 23:15:59 +00:00
|
|
|
case f.purgeCh <- struct{}{}:
|
2016-02-19 20:55:15 +00:00
|
|
|
default:
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2016-02-20 05:58:44 +00:00
|
|
|
// lastFile finds out the rotated file with the largest index in a path.
|
2016-02-19 20:55:15 +00:00
|
|
|
func (f *FileRotator) lastFile() error {
|
|
|
|
finfos, err := ioutil.ReadDir(f.path)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
prefix := fmt.Sprintf("%s.", f.baseFileName)
|
|
|
|
for _, fi := range finfos {
|
|
|
|
if fi.IsDir() {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
if strings.HasPrefix(fi.Name(), prefix) {
|
|
|
|
fileIdx := strings.TrimPrefix(fi.Name(), prefix)
|
|
|
|
n, err := strconv.Atoi(fileIdx)
|
|
|
|
if err != nil {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
if n > f.logFileIdx {
|
|
|
|
f.logFileIdx = n
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if err := f.createFile(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2016-02-19 22:11:31 +00:00
|
|
|
// createFile opens a new or existing file for writing
|
2016-02-19 20:55:15 +00:00
|
|
|
func (f *FileRotator) createFile() error {
|
|
|
|
logFileName := filepath.Join(f.path, fmt.Sprintf("%s.%d", f.baseFileName, f.logFileIdx))
|
|
|
|
cFile, err := os.OpenFile(logFileName, os.O_RDWR|os.O_CREATE|os.O_APPEND, 0666)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
f.currentFile = cFile
|
|
|
|
fi, err := f.currentFile.Stat()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
f.currentWr = fi.Size()
|
2016-02-23 17:46:10 +00:00
|
|
|
if f.bufw == nil {
|
|
|
|
f.bufw = bufio.NewWriter(f.currentFile)
|
2016-02-23 16:51:21 +00:00
|
|
|
} else {
|
2016-02-23 17:46:10 +00:00
|
|
|
f.bufw.Reset(f.currentFile)
|
2016-02-23 16:51:21 +00:00
|
|
|
}
|
2016-02-19 20:55:15 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2016-02-23 17:43:14 +00:00
|
|
|
// flushPeriodically flushes the buffered writer every 100ms to the underlying
|
|
|
|
// file
|
2016-02-23 16:51:21 +00:00
|
|
|
func (f *FileRotator) flushPeriodically() {
|
|
|
|
for _ = range f.flushTicker.C {
|
2016-02-23 17:46:10 +00:00
|
|
|
if f.bufw != nil {
|
|
|
|
f.bufw.Flush()
|
2016-02-23 16:51:21 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-02-23 17:43:14 +00:00
|
|
|
func (f *FileRotator) Close() {
|
|
|
|
// Stop the ticker and flush for one last time
|
|
|
|
f.flushTicker.Stop()
|
2016-02-23 17:46:10 +00:00
|
|
|
if f.bufw != nil {
|
|
|
|
f.bufw.Flush()
|
2016-02-23 17:43:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Stop the purge go routine
|
|
|
|
f.doneCh <- struct{}{}
|
|
|
|
close(f.purgeCh)
|
|
|
|
}
|
|
|
|
|
2016-02-19 22:11:31 +00:00
|
|
|
// purgeOldFiles removes older files and keeps only the last N files rotated for
|
2016-02-19 20:55:15 +00:00
|
|
|
// a file
|
|
|
|
func (f *FileRotator) purgeOldFiles() {
|
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case <-f.purgeCh:
|
|
|
|
var fIndexes []int
|
|
|
|
files, err := ioutil.ReadDir(f.path)
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
// Inserting all the rotated files in a slice
|
|
|
|
for _, fi := range files {
|
|
|
|
if strings.HasPrefix(fi.Name(), f.baseFileName) {
|
|
|
|
fileIdx := strings.TrimPrefix(fi.Name(), fmt.Sprintf("%s.", f.baseFileName))
|
|
|
|
n, err := strconv.Atoi(fileIdx)
|
|
|
|
if err != nil {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
fIndexes = append(fIndexes, n)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Sorting the file indexes so that we can purge the older files and keep
|
|
|
|
// only the number of files as configured by the user
|
|
|
|
sort.Sort(sort.IntSlice(fIndexes))
|
|
|
|
var toDelete []int
|
|
|
|
toDelete = fIndexes[0 : len(fIndexes)-f.MaxFiles]
|
|
|
|
for _, fIndex := range toDelete {
|
|
|
|
fname := filepath.Join(f.path, fmt.Sprintf("%s.%d", f.baseFileName, fIndex))
|
|
|
|
os.RemoveAll(fname)
|
|
|
|
}
|
|
|
|
f.oldestLogFileIdx = fIndexes[0]
|
2016-02-23 17:43:14 +00:00
|
|
|
case <-f.doneCh:
|
|
|
|
return
|
2016-02-19 20:55:15 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|