-
Notifications
You must be signed in to change notification settings - Fork 2
/
logger.go
60 lines (47 loc) · 1.44 KB
/
logger.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
package main
import (
"fmt"
"log"
"os"
"strings"
"time"
)
var (
LoggerInfo CronLogger
LoggerError CronLogger
)
func initLogger() {
LoggerInfo = CronLogger{log.New(os.Stdout, LogPrefix, 0)}
LoggerError = CronLogger{log.New(os.Stderr, LogPrefix, 0)}
}
type CronLogger struct {
*log.Logger
}
func (CronLogger CronLogger) Verbose(message string) {
if opts.Verbose {
CronLogger.Println(message)
}
}
func (CronLogger CronLogger) CronjobToString(cronjob CrontabEntry) string {
parts := []string{}
parts = append(parts, fmt.Sprintf("spec:'%v'", cronjob.Spec))
parts = append(parts, fmt.Sprintf("usr:%v", cronjob.User))
parts = append(parts, fmt.Sprintf("cmd:'%v'", cronjob.Command))
return strings.Join(parts, " ")
}
func (CronLogger CronLogger) CronjobAdd(cronjob CrontabEntry) {
CronLogger.Printf("add: %v\n", CronLogger.CronjobToString(cronjob))
}
func (CronLogger CronLogger) CronjobExec(cronjob CrontabEntry) {
if opts.Verbose {
CronLogger.Printf("exec: %v\n", CronLogger.CronjobToString(cronjob))
}
}
func (CronLogger CronLogger) CronjobExecFailed(cronjob CrontabEntry, output string, err error, elapsed time.Duration) {
CronLogger.Printf("failed cronjob: cmd:%v out:%v err:%v time:%s\n", cronjob.Command, output, err, elapsed)
}
func (CronLogger CronLogger) CronjobExecSuccess(cronjob CrontabEntry, elapsed time.Duration) {
if opts.Verbose {
CronLogger.Printf("ok: %v time:%s\n", CronLogger.CronjobToString(cronjob), elapsed)
}
}