Removed conig option DisableLogging. Add DiscardingLogger instead.
This commit is contained in:
parent
0c51ff11d1
commit
3b09eddf4d
52
tail.go
52
tail.go
|
@ -8,6 +8,7 @@ import (
|
||||||
"github.com/ActiveState/tail/util"
|
"github.com/ActiveState/tail/util"
|
||||||
"github.com/ActiveState/tail/watch"
|
"github.com/ActiveState/tail/watch"
|
||||||
"io"
|
"io"
|
||||||
|
"io/ioutil"
|
||||||
"launchpad.net/tomb"
|
"launchpad.net/tomb"
|
||||||
"log"
|
"log"
|
||||||
"os"
|
"os"
|
||||||
|
@ -48,9 +49,9 @@ type Config struct {
|
||||||
Follow bool // Continue looking for new lines (tail -f)
|
Follow bool // Continue looking for new lines (tail -f)
|
||||||
MaxLineSize int // If non-zero, split longer lines into multiple lines
|
MaxLineSize int // If non-zero, split longer lines into multiple lines
|
||||||
|
|
||||||
// Logging
|
// Logger, when nil, is set to tail.DefaultLogger
|
||||||
DisableLogging bool // when true, disables all logging (i.e. Config.Logger isn't used)
|
// To disable logging: set field to tail.DiscardingLogger
|
||||||
Logger *log.Logger // when not specified (nil), this will be set to `log.New(os.Stderr, "", log.LstdFlags)`
|
Logger *log.Logger
|
||||||
}
|
}
|
||||||
|
|
||||||
type Tail struct {
|
type Tail struct {
|
||||||
|
@ -67,6 +68,13 @@ type Tail struct {
|
||||||
tomb.Tomb // provides: Done, Kill, Dying
|
tomb.Tomb // provides: Done, Kill, Dying
|
||||||
}
|
}
|
||||||
|
|
||||||
|
var (
|
||||||
|
// DefaultLogger is used when Config.Logger == nil
|
||||||
|
DefaultLogger = log.New(os.Stderr, "", log.LstdFlags)
|
||||||
|
// DiscardingLogger can be used to disable logging output
|
||||||
|
DiscardingLogger = log.New(ioutil.Discard, "", 0)
|
||||||
|
)
|
||||||
|
|
||||||
// TailFile begins tailing the file. Output stream is made available
|
// TailFile begins tailing the file. Output stream is made available
|
||||||
// via the `Tail.Lines` channel. To handle errors during tailing,
|
// via the `Tail.Lines` channel. To handle errors during tailing,
|
||||||
// invoke the `Wait` or `Err` method after finishing reading from the
|
// invoke the `Wait` or `Err` method after finishing reading from the
|
||||||
|
@ -145,9 +153,7 @@ func (tail *Tail) reopen() error {
|
||||||
tail.file, err = os.Open(tail.Filename)
|
tail.file, err = os.Open(tail.Filename)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
if os.IsNotExist(err) {
|
if os.IsNotExist(err) {
|
||||||
if !tail.DisableLogging {
|
tail.Logger.Printf("Waiting for %s to appear...", tail.Filename)
|
||||||
tail.Logger.Printf("Waiting for %s to appear...", tail.Filename)
|
|
||||||
}
|
|
||||||
if err := tail.watcher.BlockUntilExists(&tail.Tomb); err != nil {
|
if err := tail.watcher.BlockUntilExists(&tail.Tomb); err != nil {
|
||||||
if err == tomb.ErrDying {
|
if err == tomb.ErrDying {
|
||||||
return err
|
return err
|
||||||
|
@ -186,9 +192,7 @@ func (tail *Tail) tailFileSync() {
|
||||||
// Seek to requested location on first open of the file.
|
// Seek to requested location on first open of the file.
|
||||||
if tail.Location != nil {
|
if tail.Location != nil {
|
||||||
_, err := tail.file.Seek(tail.Location.Offset, tail.Location.Whence)
|
_, err := tail.file.Seek(tail.Location.Offset, tail.Location.Whence)
|
||||||
if !tail.DisableLogging { //
|
tail.Logger.Printf("Seeked %s - %+v\n", tail.Filename, tail.Location)
|
||||||
tail.Logger.Printf("Seeked %s - %+v\n", tail.Filename, tail.Location)
|
|
||||||
}
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
tail.Killf("Seek error on %s: %s", tail.Filename, err)
|
tail.Killf("Seek error on %s: %s", tail.Filename, err)
|
||||||
return
|
return
|
||||||
|
@ -271,34 +275,24 @@ func (tail *Tail) waitForChanges() error {
|
||||||
tail.changes = nil
|
tail.changes = nil
|
||||||
if tail.ReOpen {
|
if tail.ReOpen {
|
||||||
// XXX: we must not log from a library.
|
// XXX: we must not log from a library.
|
||||||
if !tail.DisableLogging {
|
tail.Logger.Printf("Re-opening moved/deleted file %s ...", tail.Filename)
|
||||||
tail.Logger.Printf("Re-opening moved/deleted file %s ...", tail.Filename)
|
|
||||||
}
|
|
||||||
if err := tail.reopen(); err != nil {
|
if err := tail.reopen(); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
if !tail.DisableLogging {
|
tail.Logger.Printf("Successfully reopened %s", tail.Filename)
|
||||||
tail.Logger.Printf("Successfully reopened %s", tail.Filename)
|
|
||||||
}
|
|
||||||
tail.reader = bufio.NewReader(tail.file)
|
tail.reader = bufio.NewReader(tail.file)
|
||||||
return nil
|
return nil
|
||||||
} else {
|
} else {
|
||||||
if !tail.DisableLogging {
|
tail.Logger.Printf("Stopping tail as file no longer exists: %s", tail.Filename)
|
||||||
tail.Logger.Printf("Stopping tail as file no longer exists: %s", tail.Filename)
|
|
||||||
}
|
|
||||||
return ErrStop
|
return ErrStop
|
||||||
}
|
}
|
||||||
case <-tail.changes.Truncated:
|
case <-tail.changes.Truncated:
|
||||||
// Always reopen truncated files (Follow is true)
|
// Always reopen truncated files (Follow is true)
|
||||||
if !tail.DisableLogging {
|
tail.Logger.Printf("Re-opening truncated file %s ...", tail.Filename)
|
||||||
tail.Logger.Printf("Re-opening truncated file %s ...", tail.Filename)
|
|
||||||
}
|
|
||||||
if err := tail.reopen(); err != nil {
|
if err := tail.reopen(); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
if !tail.DisableLogging {
|
tail.Logger.Printf("Successfully reopened truncated %s", tail.Filename)
|
||||||
tail.Logger.Printf("Successfully reopened truncated %s", tail.Filename)
|
|
||||||
}
|
|
||||||
tail.reader = bufio.NewReader(tail.file)
|
tail.reader = bufio.NewReader(tail.file)
|
||||||
return nil
|
return nil
|
||||||
case <-tail.Dying():
|
case <-tail.Dying():
|
||||||
|
@ -324,12 +318,10 @@ func (tail *Tail) sendLine(line []byte) bool {
|
||||||
tail.Lines <- &Line{line, now, nil}
|
tail.Lines <- &Line{line, now, nil}
|
||||||
rate := tail.rateMon.Tick(nowUnix)
|
rate := tail.rateMon.Tick(nowUnix)
|
||||||
if tail.LimitRate > 0 && rate > tail.LimitRate {
|
if tail.LimitRate > 0 && rate > tail.LimitRate {
|
||||||
if !tail.DisableLogging {
|
tail.Logger.Printf("Rate limit (%v < %v) reached on file (%v); entering 1s cooloff period.\n",
|
||||||
tail.Logger.Printf("Rate limit (%v < %v) reached on file (%v); entering 1s cooloff period.\n",
|
tail.LimitRate,
|
||||||
tail.LimitRate,
|
rate,
|
||||||
rate,
|
tail.Filename)
|
||||||
tail.Filename)
|
|
||||||
}
|
|
||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue