From 68f71e980b8bc6565aa10be0fae2012972cd51fe Mon Sep 17 00:00:00 2001 From: wangyi-yd Date: Tue, 27 Oct 2015 16:50:06 +0100 Subject: [PATCH 1/2] add subsribers --- log/logrotate/logrotate.go | 64 ++++++++++++++++++++++++++++----- log/logrotate/logrotate_test.go | 28 +++++++++++++++ 2 files changed, 83 insertions(+), 9 deletions(-) diff --git a/log/logrotate/logrotate.go b/log/logrotate/logrotate.go index ff922e1..8751276 100644 --- a/log/logrotate/logrotate.go +++ b/log/logrotate/logrotate.go @@ -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 []Subcriber } func (l *Logrotate) open() error { @@ -63,12 +64,24 @@ func (l *Logrotate) close() error { } func (l *Logrotate) reload() error { + for _, subcriber := range l.subscribers { + if err := subcriber.OnPreReload(); err != nil { + return err + } + } + if err := l.close(); err != nil { return err } if err := l.open(); err != nil { return err } + + for _, subcriber := range l.subscribers { + if err := subcriber.OnPostReload(); err != nil { + return err + } + } return nil } @@ -80,13 +93,27 @@ func DateFormat(s string) { } func (l *Logrotate) rotate(t time.Time) error { + for _, subcriber := range l.subscribers { + if err := subcriber.OnPreRotate(t); err != nil { + return err + } + } + 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.OnPostRotate(target); err != nil { + return err + } + } + if err := l.open(); err != nil { return err } @@ -188,6 +215,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 Subcriber) { + 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{ @@ -200,3 +234,15 @@ func New(file string, interval time.Duration, format string, fields []string) (* } return l, l.open() } + +// Subcriber defines event handler functions inside logrotate: +// OnPreRotate is called BEFORE the file is rotated +// OnPostRotate is called AFTER the file is rotated +// OnPreReload is called BEFORE logrotate is reloaded +// OnPostReload is called AFTER logrotate is reloaded +type Subcriber interface { + OnPreRotate(t time.Time) error + OnPostRotate(rotateFilename string) error + OnPreReload() error + OnPostReload() error +} diff --git a/log/logrotate/logrotate_test.go b/log/logrotate/logrotate_test.go index df44d92..e27431d 100644 --- a/log/logrotate/logrotate_test.go +++ b/log/logrotate/logrotate_test.go @@ -28,6 +28,30 @@ import ( var tmpdir = fmt.Sprintf("%s%x/", os.TempDir(), rand.Int()) +type SubscriberMock struct { + Buf bytes.Buffer +} + +func (sub *SubscriberMock) OnPreRotate(t time.Time) error { + sub.Buf.WriteString(t.String() + "\n") + return nil +} + +func (sub *SubscriberMock) OnPostRotate(rotateFilename string) error { + sub.Buf.WriteString(rotateFilename + "\n") + return nil +} + +func (sub *SubscriberMock) OnPreReload() error { + sub.Buf.WriteString("onPreReload called!\n") + return nil +} + +func (sub *SubscriberMock) OnPostReload() error { + sub.Buf.WriteString("onPostReload called!\n") + return nil +} + func TestRotate(t *testing.T) { err := os.MkdirAll(tmpdir, 0777) if err != nil { @@ -43,6 +67,8 @@ func TestRotate(t *testing.T) { } defer sink.Close() + subscriber := &SubscriberMock{} + sink.AddSubscriber(subscriber) log.New(sink).Info("hello!") // now rotate @@ -50,6 +76,8 @@ func TestRotate(t *testing.T) { t.Fatal(err) } + t.Log(subscriber.Buf.String()) + // lets check if the rotated file exists files, err := ioutil.ReadDir(tmpdir) if err != nil { From 0f7ebe002799441a376f9c0f877f6978716608a2 Mon Sep 17 00:00:00 2001 From: wangyi-yd Date: Wed, 28 Oct 2015 16:41:45 +0100 Subject: [PATCH 2/2] refactor subscriber --- log/logrotate/logrotate.go | 37 +++++++-------------------------- log/logrotate/logrotate_test.go | 21 ++++--------------- 2 files changed, 11 insertions(+), 47 deletions(-) diff --git a/log/logrotate/logrotate.go b/log/logrotate/logrotate.go index 8751276..727b5e3 100644 --- a/log/logrotate/logrotate.go +++ b/log/logrotate/logrotate.go @@ -36,7 +36,7 @@ type Logrotate struct { err chan error stop chan bool mux sync.Mutex - subscribers []Subcriber + subscribers []Subscriber } func (l *Logrotate) open() error { @@ -64,12 +64,6 @@ func (l *Logrotate) close() error { } func (l *Logrotate) reload() error { - for _, subcriber := range l.subscribers { - if err := subcriber.OnPreReload(); err != nil { - return err - } - } - if err := l.close(); err != nil { return err } @@ -77,11 +71,6 @@ func (l *Logrotate) reload() error { return err } - for _, subcriber := range l.subscribers { - if err := subcriber.OnPostReload(); err != nil { - return err - } - } return nil } @@ -93,12 +82,6 @@ func DateFormat(s string) { } func (l *Logrotate) rotate(t time.Time) error { - for _, subcriber := range l.subscribers { - if err := subcriber.OnPreRotate(t); err != nil { - return err - } - } - if err := l.close(); err != nil { return err } @@ -109,7 +92,7 @@ func (l *Logrotate) rotate(t time.Time) error { } for _, subcriber := range l.subscribers { - if err := subcriber.OnPostRotate(target); err != nil { + if err := subcriber.OnRotate(target); err != nil { return err } } @@ -218,7 +201,7 @@ func (l *Logrotate) Stop() { // 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 Subcriber) { +func (l *Logrotate) AddSubscriber(sub Subscriber) { l.subscribers = append(l.subscribers, sub) } @@ -235,14 +218,8 @@ func New(file string, interval time.Duration, format string, fields []string) (* return l, l.open() } -// Subcriber defines event handler functions inside logrotate: -// OnPreRotate is called BEFORE the file is rotated -// OnPostRotate is called AFTER the file is rotated -// OnPreReload is called BEFORE logrotate is reloaded -// OnPostReload is called AFTER logrotate is reloaded -type Subcriber interface { - OnPreRotate(t time.Time) error - OnPostRotate(rotateFilename string) error - OnPreReload() error - OnPostReload() error +// Subscriber defines event handler functions inside logrotate. +// handle is called when the file is rotated. +type Subscriber interface { + OnRotate(filename string) error } diff --git a/log/logrotate/logrotate_test.go b/log/logrotate/logrotate_test.go index e27431d..a3910e0 100644 --- a/log/logrotate/logrotate_test.go +++ b/log/logrotate/logrotate_test.go @@ -32,26 +32,11 @@ type SubscriberMock struct { Buf bytes.Buffer } -func (sub *SubscriberMock) OnPreRotate(t time.Time) error { - sub.Buf.WriteString(t.String() + "\n") - return nil -} - -func (sub *SubscriberMock) OnPostRotate(rotateFilename string) error { +func (sub *SubscriberMock) OnRotate(rotateFilename string) error { sub.Buf.WriteString(rotateFilename + "\n") return nil } -func (sub *SubscriberMock) OnPreReload() error { - sub.Buf.WriteString("onPreReload called!\n") - return nil -} - -func (sub *SubscriberMock) OnPostReload() error { - sub.Buf.WriteString("onPostReload called!\n") - return nil -} - func TestRotate(t *testing.T) { err := os.MkdirAll(tmpdir, 0777) if err != nil { @@ -76,7 +61,9 @@ func TestRotate(t *testing.T) { t.Fatal(err) } - t.Log(subscriber.Buf.String()) + if subscriber.Buf.Len() == 0 { + t.Fatalf("subscriber is not called") + } // lets check if the rotated file exists files, err := ioutil.ReadDir(tmpdir)