mozilla-services / heka

DEPRECATED: Data collection and processing made easy.
http://hekad.readthedocs.org/
Other
3.39k stars 529 forks source link

File Output rotation and time Format #1469

Open hamshiva opened 9 years ago

hamshiva commented 9 years ago

Hi,

I ran into an issue using File Output and rotation. Let take the following example config:

[counter_file]
type = "FileOutput"
message_matcher = "Type == 'heka.counter-output'"
path = "/var/log/apache2/access-2006-01-02.log"
rotation_interval = 24
encoder = "PayloadEncoder"

This will result in a file named corretly but the directory apache2 will be rename apache(dayofmonth) Using Go time Format, there is no way to avoid apache2 to become something else... And this will append to anythin that match a part of the Go datetime reference (Mon Jan 2 15:04:05 -0700 MST 2006)...

It seems that there is no way in Go to escape literals in the time Format layout. The only way I found was patch Heka to use strftime syntax provided by an external light weigth module https://github.com/jehiah/go-strftime

There is a git diff of my patch:

diff --git a/plugins/file/file_output.go b/plugins/file/file_output.go
index cdcaacd..792b901 100644
--- a/plugins/file/file_output.go
+++ b/plugins/file/file_output.go
@@ -20,6 +20,7 @@ import (
        "errors"
        "fmt"
        . "github.com/mozilla-services/heka/pipeline"
+       "github.com/jehiah/go-strftime"
        "github.com/mozilla-services/heka/plugins"
        "github.com/rafrombrc/go-notify"
        "os"
@@ -160,7 +161,7 @@ func (o *FileOutput) startRotateNotifier() {
        until := next.Sub(now)
        after := time.After(until)

-       o.path = last.Format(o.Path)
+       o.path = strftime.Format(o.Path, now)
        o.rotateChan = make(chan time.Time)

        go func() {

Best regards,

hamshiva commented 9 years ago

I missed this part of the patch:

@@ -333,7 +334,7 @@ func (o *FileOutput) committer(or OutputRunner, errChan chan error) {
                        }
                case rotateTime := <-o.rotateChan:
                        o.file.Close()
-                       o.path = rotateTime.Format(o.Path)
+                       o.path = strftime.Format(o.Path, rotateTime)
                        if err = o.openFile(); err != nil {