Skip to content
This repository was archived by the owner on Feb 19, 2019. It is now read-only.
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
*.sw[op]
.DS_Store
.idea/
6 changes: 5 additions & 1 deletion config.go
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ func (log Logger) LoadConfiguration(filename string) {
continue
}

log[xmlfilt.Tag] = &Filter{lvl, filt}
log[xmlfilt.Tag] = &Filter{lvl, filt, xmlfilt.Tag}
}
}

Expand Down Expand Up @@ -180,6 +180,7 @@ func xmlToFileLogWriter(filename string, props []xmlProperty, enabled bool) (*Fi
format := "[%D %T] [%L] (%S) %M"
maxlines := 0
maxsize := 0
hourly := false
daily := false
rotate := false

Expand All @@ -194,6 +195,8 @@ func xmlToFileLogWriter(filename string, props []xmlProperty, enabled bool) (*Fi
maxlines = strToNumSuffix(strings.Trim(prop.Value, " \r\n"), 1000)
case "maxsize":
maxsize = strToNumSuffix(strings.Trim(prop.Value, " \r\n"), 1024)
case "hourly":
hourly = strings.Trim(prop.Value, " \r\n") != "false"
case "daily":
daily = strings.Trim(prop.Value, " \r\n") != "false"
case "rotate":
Expand All @@ -218,6 +221,7 @@ func xmlToFileLogWriter(filename string, props []xmlProperty, enabled bool) (*Fi
flw.SetFormat(format)
flw.SetRotateLines(maxlines)
flw.SetRotateSize(maxsize)
flw.SetRotateHourly(hourly)
flw.SetRotateDaily(daily)
return flw, true
}
Expand Down
47 changes: 40 additions & 7 deletions filelog.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"fmt"
"os"
"time"
"strings"
)

// This log writer sends output to a file
Expand Down Expand Up @@ -35,6 +36,10 @@ type FileLogWriter struct {
daily bool
daily_opendate int

// Rotate hourly
hourly bool
hourly_opendate int

// Keep old logfiles (.001, .002, etc)
rotate bool
maxbackup int
Expand Down Expand Up @@ -97,7 +102,8 @@ func NewFileLogWriter(fname string, rotate bool) *FileLogWriter {
now := time.Now()
if (w.maxlines > 0 && w.maxlines_curlines >= w.maxlines) ||
(w.maxsize > 0 && w.maxsize_cursize >= w.maxsize) ||
(w.daily && now.Day() != w.daily_opendate) {
(w.hourly && now.Hour() != w.hourly_opendate) ||
(w.daily && now.Day() != w.daily_opendate) {
if err := w.intRotate(); err != nil {
fmt.Fprintf(os.Stderr, "FileLogWriter(%q): %s\n", w.filename, err)
return
Expand Down Expand Up @@ -135,17 +141,19 @@ func (w *FileLogWriter) intRotate() error {
}

// If we are keeping log files, move it to the next available number
fileName := getActualPathReplacePattern(w.filename)
if w.rotate {
_, err := os.Lstat(w.filename)
_, err := os.Lstat(fileName)
if err == nil { // file exists
// Find the next available number
num := 1
fname := ""
//cutting by daily
if w.daily && time.Now().Day() != w.daily_opendate {
yesterday := time.Now().AddDate(0, 0, -1).Format("2006-01-02")

for ; err == nil && num <= w.maxbackup; num++ {
fname = w.filename + fmt.Sprintf(".%s.%03d", yesterday, num)
fname = fileName + fmt.Sprintf(".%s.%03d", yesterday, num)
_, err = os.Lstat(fname)
}
// return error if the last file checked still existed
Expand All @@ -155,8 +163,8 @@ func (w *FileLogWriter) intRotate() error {
} else {
num = w.maxbackup - 1
for ; num >= 1; num-- {
fname = w.filename + fmt.Sprintf(".%d", num)
nfname := w.filename + fmt.Sprintf(".%d", num+1)
fname = fileName + fmt.Sprintf(".%d", num)
nfname := fileName + fmt.Sprintf(".%d", num+1)
_, err = os.Lstat(fname)
if err == nil {
os.Rename(fname, nfname)
Expand All @@ -166,15 +174,15 @@ func (w *FileLogWriter) intRotate() error {

w.file.Close()
// Rename the file to its newfound home
err = os.Rename(w.filename, fname)
err = os.Rename(fileName, fname)
if err != nil {
return fmt.Errorf("Rotate: %s\n", err)
}
}
}

// Open the log file
fd, err := os.OpenFile(w.filename, os.O_WRONLY|os.O_APPEND|os.O_CREATE, 0660)
fd, err := os.OpenFile(fileName, os.O_WRONLY|os.O_APPEND|os.O_CREATE, 0660)
if err != nil {
return err
}
Expand All @@ -185,6 +193,7 @@ func (w *FileLogWriter) intRotate() error {

// Set the daily open date to the current date
w.daily_opendate = now.Day()
w.hourly_opendate = now.Hour()

// initialize rotation values
w.maxlines_curlines = 0
Expand Down Expand Up @@ -227,6 +236,13 @@ func (w *FileLogWriter) SetRotateSize(maxsize int) *FileLogWriter {
return w
}

// Set rotate hourly (chainable). Must be called before the first log message is
// written.
func (w *FileLogWriter) SetRotateHourly(hourly bool) *FileLogWriter {
w.hourly = hourly
return w
}

// Set rotate daily (chainable). Must be called before the first log message is
// written.
func (w *FileLogWriter) SetRotateDaily(daily bool) *FileLogWriter {
Expand Down Expand Up @@ -262,3 +278,20 @@ func NewXMLLogWriter(fname string, rotate bool) *FileLogWriter {
<message>%M</message>
</record>`).SetHeadFoot("<log created=\"%D %T\">", "</log>")
}


func getActualPathReplacePattern(pattern string) string {
now := time.Now()
Y := fmt.Sprintf("%d",now.Year())
M := fmt.Sprintf("%02d",now.Month())
D := fmt.Sprintf("%02d",now.Day())
H := fmt.Sprintf("%02d",now.Hour())
m := fmt.Sprintf("%02d",now.Minute())

pattern = strings.Replace(pattern, "%Y", Y, -1)
pattern = strings.Replace(pattern, "%M", M, -1)
pattern = strings.Replace(pattern, "%D", D, -1)
pattern = strings.Replace(pattern, "%H", H, -1)
pattern = strings.Replace(pattern, "%m", m, -1)
return pattern
}
Loading