1
0
Fork 0
shezmu/stats/logger.go

49 lines
683 B
Go
Raw Normal View History

2015-10-23 23:41:41 +00:00
package stats
import (
"io"
"os"
"time"
)
type Logger struct {
base
2015-10-23 23:41:41 +00:00
out io.Writer
interval time.Duration
}
func NewLogger(out io.Writer, interval time.Duration) *Logger {
l := &Logger{
out: out,
interval: interval,
}
l.init()
2015-10-23 23:41:41 +00:00
go l.printWithInterval()
return l
}
func NewStdoutLogger(interval time.Duration) *Logger {
2015-10-25 11:35:39 +00:00
return NewLogger(os.Stdout, interval)
2015-10-23 23:41:41 +00:00
}
func (l *Logger) Print() {
for _, s := range l.stats {
l.out.Write([]byte(s.String()))
l.out.Write([]byte{'\n'})
2015-10-23 23:41:41 +00:00
s.time.Clear()
s.errors.Clear()
}
}
func (l *Logger) printWithInterval() {
if l.interval == 0 {
return
}
for range time.NewTicker(l.interval).C {
l.Print()
}
}