This repository has been archived by the owner on Jan 17, 2023. It is now read-only.
forked from webdevops/go-crond
-
Notifications
You must be signed in to change notification settings - Fork 3
/
logger.go
63 lines (50 loc) · 1.57 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
61
62
63
package main
import (
"fmt"
"log"
"strings"
"os"
"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))
if len(cronjob.Env) >= 1 {
parts = append(parts, fmt.Sprintf("env:'%v'", cronjob.Env))
}
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, output string, err error, elapsed time.Duration) {
if opts.Verbose {
CronLogger.Printf("ok: cronjob: cmd:%v out:%v err:%v time:%s\n", cronjob.Command, output, err, elapsed)
}
}