Skip to content

Commit

Permalink
Issue cihub#96.
Browse files Browse the repository at this point in the history
  • Loading branch information
goodsign committed Nov 2, 2015
1 parent 5841159 commit 0f89bb7
Showing 1 changed file with 24 additions and 11 deletions.
35 changes: 24 additions & 11 deletions writers_rollingfilewriter.go
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,7 @@ type rollerVirtual interface {
// Creates a new froll history file using the contents of current file and special filename of the latest roll (prefix/ postfix).
// If lastRollName is empty (""), then it means that there is no latest roll (current is the first one)
getNewHistoryRollFileName(lastRollName string) string
getCurrentModifiedFileName(originalFileName string) string // Returns filename modified according to specific logger rules
getCurrentModifiedFileName(originalFileName string, first bool) (string, error) // Returns filename modified according to specific logger rules
}

// rollingFileWriter writes received messages to a file, until time interval passes
Expand Down Expand Up @@ -230,7 +230,7 @@ func (rw *rollingFileWriter) getSortedLogHistory() ([]string, error) {
return validSortedFiles, nil
}

func (rw *rollingFileWriter) createFileAndFolderIfNeeded() error {
func (rw *rollingFileWriter) createFileAndFolderIfNeeded(first bool) error {
var err error

if len(rw.currentDirPath) != 0 {
Expand All @@ -241,7 +241,10 @@ func (rw *rollingFileWriter) createFileAndFolderIfNeeded() error {
}
}

rw.fileName = rw.self.getCurrentModifiedFileName(rw.originalFileName)
rw.fileName, err = rw.self.getCurrentModifiedFileName(rw.originalFileName, first)
if err != nil {
return err
}
filePath := filepath.Join(rw.currentDirPath, rw.fileName)

// If exists
Expand Down Expand Up @@ -338,7 +341,7 @@ func (rw *rollingFileWriter) getFileRollName(fileName string) string {

func (rw *rollingFileWriter) Write(bytes []byte) (n int, err error) {
if rw.currentFile == nil {
err := rw.createFileAndFolderIfNeeded()
err := rw.createFileAndFolderIfNeeded(true)
if err != nil {
return 0, err
}
Expand Down Expand Up @@ -412,7 +415,7 @@ func (rw *rollingFileWriter) Write(bytes []byte) (n int, err error) {
}
}

err = rw.createFileAndFolderIfNeeded()
err = rw.createFileAndFolderIfNeeded(false)
if err != nil {
return 0, err
}
Expand Down Expand Up @@ -493,8 +496,8 @@ func (rws *rollingFileWriterSize) getNewHistoryRollFileName(lastRollName string)
return fmt.Sprintf("%d", v+1)
}

func (rws *rollingFileWriterSize) getCurrentModifiedFileName(originalFileName string) string {
return originalFileName
func (rws *rollingFileWriterSize) getCurrentModifiedFileName(originalFileName string, first bool) (string, error) {
return originalFileName, nil
}

func (rws *rollingFileWriterSize) String() string {
Expand Down Expand Up @@ -591,14 +594,24 @@ func (rwt *rollingFileWriterTime) getNewHistoryRollFileName(lastRollName string)
return ""
}

func (rwt *rollingFileWriterTime) getCurrentModifiedFileName(originalFileName string) string {
func (rwt *rollingFileWriterTime) getCurrentModifiedFileName(originalFileName string, first bool) (string, error) {
if first {
history, err := rwt.getSortedLogHistory()
if err != nil {
return "", err
}
if len(history) > 0 {
return history[len(history)-1], nil
}
}

switch rwt.nameMode {
case rollingNameModePostfix:
return originalFileName + rollingLogHistoryDelimiter + time.Now().Format(rwt.timePattern)
return originalFileName + rollingLogHistoryDelimiter + time.Now().Format(rwt.timePattern), nil
case rollingNameModePrefix:
return time.Now().Format(rwt.timePattern) + rollingLogHistoryDelimiter + originalFileName
return time.Now().Format(rwt.timePattern) + rollingLogHistoryDelimiter + originalFileName, nil
}
return ""
return "", fmt.Errorf("Unknown rolling writer mode. Either postfix or prefix must be used")
}

func (rwt *rollingFileWriterTime) String() string {
Expand Down

0 comments on commit 0f89bb7

Please sign in to comment.