2020-01-28 23:50:41 +00:00
|
|
|
package logging
|
2018-08-29 20:56:58 +00:00
|
|
|
|
|
|
|
import (
|
2019-07-19 21:36:34 +00:00
|
|
|
"fmt"
|
2018-08-29 20:56:58 +00:00
|
|
|
"os"
|
|
|
|
"path/filepath"
|
|
|
|
"strconv"
|
|
|
|
"strings"
|
|
|
|
"sync"
|
|
|
|
"time"
|
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
|
|
|
now = time.Now
|
|
|
|
)
|
|
|
|
|
|
|
|
//LogFile is used to setup a file based logger that also performs log rotation
|
|
|
|
type LogFile struct {
|
|
|
|
//Name of the log file
|
|
|
|
fileName string
|
|
|
|
|
|
|
|
//Path to the log file
|
|
|
|
logPath string
|
|
|
|
|
|
|
|
//Duration between each file rotation operation
|
|
|
|
duration time.Duration
|
|
|
|
|
|
|
|
//LastCreated represents the creation time of the latest log
|
|
|
|
LastCreated time.Time
|
|
|
|
|
|
|
|
//FileInfo is the pointer to the current file being written to
|
|
|
|
FileInfo *os.File
|
|
|
|
|
|
|
|
//MaxBytes is the maximum number of desired bytes for a log file
|
|
|
|
MaxBytes int
|
|
|
|
|
|
|
|
//BytesWritten is the number of bytes written in the current log file
|
|
|
|
BytesWritten int64
|
|
|
|
|
2019-07-19 21:36:34 +00:00
|
|
|
// Max rotated files to keep before removing them.
|
|
|
|
MaxFiles int
|
|
|
|
|
2018-08-29 20:56:58 +00:00
|
|
|
//acquire is the mutex utilized to ensure we have no concurrency issues
|
|
|
|
acquire sync.Mutex
|
|
|
|
}
|
|
|
|
|
2019-07-19 21:36:34 +00:00
|
|
|
func (l *LogFile) fileNamePattern() string {
|
2019-03-06 17:13:28 +00:00
|
|
|
// Extract the file extension
|
2018-08-29 20:56:58 +00:00
|
|
|
fileExt := filepath.Ext(l.fileName)
|
|
|
|
// If we have no file extension we append .log
|
|
|
|
if fileExt == "" {
|
|
|
|
fileExt = ".log"
|
|
|
|
}
|
2019-03-06 17:13:28 +00:00
|
|
|
// Remove the file extension from the filename
|
2019-07-19 21:36:34 +00:00
|
|
|
return strings.TrimSuffix(l.fileName, fileExt) + "-%s" + fileExt
|
|
|
|
}
|
|
|
|
|
|
|
|
func (l *LogFile) openNew() error {
|
|
|
|
fileNamePattern := l.fileNamePattern()
|
2019-12-18 21:31:22 +00:00
|
|
|
|
2018-08-29 20:56:58 +00:00
|
|
|
createTime := now()
|
2019-07-19 21:36:34 +00:00
|
|
|
newfileName := fmt.Sprintf(fileNamePattern, strconv.FormatInt(createTime.UnixNano(), 10))
|
2018-08-29 20:56:58 +00:00
|
|
|
newfilePath := filepath.Join(l.logPath, newfileName)
|
2019-12-18 21:31:22 +00:00
|
|
|
|
2018-08-29 20:56:58 +00:00
|
|
|
// Try creating a file. We truncate the file because we are the only authority to write the logs
|
2019-02-15 22:01:48 +00:00
|
|
|
filePointer, err := os.OpenFile(newfilePath, os.O_CREATE|os.O_TRUNC|os.O_WRONLY, 0640)
|
2018-08-29 20:56:58 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2019-07-19 21:36:34 +00:00
|
|
|
|
2018-08-29 20:56:58 +00:00
|
|
|
l.FileInfo = filePointer
|
|
|
|
// New file, new bytes tracker, new creation time :)
|
|
|
|
l.LastCreated = createTime
|
|
|
|
l.BytesWritten = 0
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (l *LogFile) rotate() error {
|
|
|
|
// Get the time from the last point of contact
|
|
|
|
timeElapsed := time.Since(l.LastCreated)
|
|
|
|
// Rotate if we hit the byte file limit or the time limit
|
|
|
|
if (l.BytesWritten >= int64(l.MaxBytes) && (l.MaxBytes > 0)) || timeElapsed >= l.duration {
|
|
|
|
l.FileInfo.Close()
|
2019-07-19 21:36:34 +00:00
|
|
|
if err := l.pruneFiles(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2018-08-29 20:56:58 +00:00
|
|
|
return l.openNew()
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2019-07-19 21:36:34 +00:00
|
|
|
func (l *LogFile) pruneFiles() error {
|
|
|
|
if l.MaxFiles == 0 {
|
|
|
|
return nil
|
|
|
|
}
|
2020-10-22 18:31:51 +00:00
|
|
|
|
|
|
|
pattern := filepath.Join(l.logPath, fmt.Sprintf(l.fileNamePattern(), "*"))
|
|
|
|
matches, err := filepath.Glob(pattern)
|
2019-07-19 21:36:34 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2020-10-22 18:31:51 +00:00
|
|
|
|
|
|
|
switch {
|
|
|
|
case l.MaxFiles < 0:
|
|
|
|
return removeFiles(matches)
|
|
|
|
case len(matches) < l.MaxFiles:
|
|
|
|
return nil
|
2019-12-18 21:31:22 +00:00
|
|
|
}
|
2020-10-22 18:31:51 +00:00
|
|
|
|
|
|
|
last := len(matches) - l.MaxFiles
|
|
|
|
return removeFiles(matches[:last])
|
|
|
|
}
|
|
|
|
|
|
|
|
func removeFiles(files []string) error {
|
|
|
|
for _, file := range files {
|
|
|
|
if err := os.Remove(file); err != nil {
|
2019-07-19 21:36:34 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2019-04-11 16:04:28 +00:00
|
|
|
// Write is used to implement io.Writer
|
2018-08-29 20:56:58 +00:00
|
|
|
func (l *LogFile) Write(b []byte) (n int, err error) {
|
|
|
|
l.acquire.Lock()
|
|
|
|
defer l.acquire.Unlock()
|
2019-12-18 21:31:22 +00:00
|
|
|
// Create a new file if we have no file to write to
|
2018-08-29 20:56:58 +00:00
|
|
|
if l.FileInfo == nil {
|
|
|
|
if err := l.openNew(); err != nil {
|
|
|
|
return 0, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// Check for the last contact and rotate if necessary
|
|
|
|
if err := l.rotate(); err != nil {
|
|
|
|
return 0, err
|
|
|
|
}
|
|
|
|
l.BytesWritten += int64(len(b))
|
|
|
|
return l.FileInfo.Write(b)
|
|
|
|
}
|