jobrun: per-job logger

This commit is contained in:
Christian Schwarz
2017-05-03 18:13:50 +02:00
parent 3b6d79ec67
commit 55463e5f26
2 changed files with 20 additions and 8 deletions
+13 -2
View File
@@ -1,6 +1,7 @@
package jobrun
import (
"fmt"
"time"
)
@@ -8,9 +9,18 @@ type Logger interface {
Printf(format string, v ...interface{})
}
type jobLogger struct {
MainLog Logger
JobName string
}
func (l jobLogger) Printf(format string, v ...interface{}) {
l.MainLog.Printf(fmt.Sprintf("job[%s]: %s", l.JobName, format), v...)
}
type Job struct {
Name string
RunFunc func() (err error)
RunFunc func(log Logger) (err error)
LastStart time.Time
LastError error
Interval time.Duration
@@ -114,7 +124,8 @@ loop:
job.LastStart = now
go func(job Job) {
if err := job.RunFunc(); err != nil {
jobLog := jobLogger{r.logger, job.Name}
if err := job.RunFunc(jobLog); err != nil {
job.LastError = err
r.notificationChan <- job
}