-
Notifications
You must be signed in to change notification settings - Fork 7
/
console.go
62 lines (52 loc) · 1.17 KB
/
console.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
package notifier
import (
"encoding/json"
"fmt"
"github.com/PxyUp/fitter/pkg/builder"
"github.com/PxyUp/fitter/pkg/config"
"github.com/PxyUp/fitter/pkg/logger"
"os"
)
type console struct {
logger logger.Logger
name string
cfg *config.ConsoleConfig
}
func (o *console) GetLogger() logger.Logger {
return o.logger
}
func (o *console) notify(record *singleRecord, input builder.Interfacable) error {
if o.cfg.OnlyResult {
_, errOut := fmt.Fprintln(os.Stdout, string(record.Body))
if errOut != nil {
o.logger.Errorw("cant send to stdout", "error", errOut.Error())
return errOut
}
return nil
}
bb, err := json.Marshal(record)
if err != nil {
o.logger.Errorw("cant unmarshal record", "error", err.Error())
return err
}
_, errOut := fmt.Fprintln(os.Stdout, string(bb))
if errOut != nil {
o.logger.Errorw("cant send to stdout", "error", errOut.Error())
return errOut
}
return nil
}
var (
_ Notifier = &console{}
)
func NewConsole(name string, cfg *config.ConsoleConfig) *console {
return &console{
logger: logger.Null,
name: name,
cfg: cfg,
}
}
func (o *console) WithLogger(logger logger.Logger) *console {
o.logger = logger
return o
}