Skip to content
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
41 changes: 32 additions & 9 deletions log/logrotate/logrotate.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,15 +27,16 @@ import (
// Logrotate is a special case of sink which writes to a file and is capable of
// rotating that file when certain conditions are met.
type Logrotate struct {
file *os.File
buf *bufio.Writer
filename string
format string
interval time.Duration
fields []string
err chan error
stop chan bool
mux sync.Mutex
file *os.File
buf *bufio.Writer
filename string
format string
interval time.Duration
fields []string
err chan error
stop chan bool
mux sync.Mutex
subscribers []Subscriber
}

func (l *Logrotate) open() error {
Expand Down Expand Up @@ -69,6 +70,7 @@ func (l *Logrotate) reload() error {
if err := l.open(); err != nil {
return err
}

return nil
}

Expand All @@ -83,10 +85,18 @@ func (l *Logrotate) rotate(t time.Time) error {
if err := l.close(); err != nil {
return err
}

target := l.filename + "." + t.Format(dateFormat)
if err := os.Rename(l.filename, target); err != nil {
return err
}

for _, subcriber := range l.subscribers {
if err := subcriber.OnRotate(target); err != nil {
return err
}
}

if err := l.open(); err != nil {
return err
}
Expand Down Expand Up @@ -188,6 +198,13 @@ func (l *Logrotate) Stop() {
l.stop <- true
}

// AddSubscriber appends a subscriber to handle events.
// The user can add multiple subscribers, each subcriber
// will be called in the order as it is appended.
func (l *Logrotate) AddSubscriber(sub Subscriber) {
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since we went with the simple interface, we might as well make it even easier by ditching the interface in favour of a function type.

How about this?

func (l *Logrotate) OnRotate(fn func(string) error) {}

l.subscribers = append(l.subscribers, sub)
}

// New returns a new Logrotate using the supplied arguments.
func New(file string, interval time.Duration, format string, fields []string) (*Logrotate, error) {
l := &Logrotate{
Expand All @@ -200,3 +217,9 @@ func New(file string, interval time.Duration, format string, fields []string) (*
}
return l, l.open()
}

// Subscriber defines event handler functions inside logrotate.
// handle is called when the file is rotated.
type Subscriber interface {
OnRotate(filename string) error
}
15 changes: 15 additions & 0 deletions log/logrotate/logrotate_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,15 @@ import (

var tmpdir = fmt.Sprintf("%s%x/", os.TempDir(), rand.Int())

type SubscriberMock struct {
Buf bytes.Buffer
}

func (sub *SubscriberMock) OnRotate(rotateFilename string) error {
sub.Buf.WriteString(rotateFilename + "\n")
return nil
}

func TestRotate(t *testing.T) {
err := os.MkdirAll(tmpdir, 0777)
if err != nil {
Expand All @@ -43,13 +52,19 @@ func TestRotate(t *testing.T) {
}
defer sink.Close()

subscriber := &SubscriberMock{}
sink.AddSubscriber(subscriber)
log.New(sink).Info("hello!")

// now rotate
if err = sink.Rotate(); err != nil {
t.Fatal(err)
}

if subscriber.Buf.Len() == 0 {
t.Fatalf("subscriber is not called")
}

// lets check if the rotated file exists
files, err := ioutil.ReadDir(tmpdir)
if err != nil {
Expand Down