-
Notifications
You must be signed in to change notification settings - Fork 0
/
console_sender.go
68 lines (61 loc) · 1.45 KB
/
console_sender.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
64
65
66
67
68
package messaging
import (
"encoding/json"
"fmt"
"github.com/rs/zerolog/log"
)
var (
_ Sender = (*ConsoleSender)(nil)
)
type ConsoleSender struct {
sender `mapstructure:",squash"`
}
func (c *ConsoleSender) Send(events []*Event) error {
for _, e := range events {
if e == nil {
continue
}
if err := c.sendOne(e); err != nil {
return err
}
}
return nil
}
func (c *ConsoleSender) sendOne(e *Event) error {
if e.Json {
var bytes []byte
var err error
if e.IsLogEvent() {
bytes, err = json.Marshal(e.Log)
} else if e.IsSmartlockEvent() {
bytes, err = json.Marshal(e.Smartlock)
} else {
err = fmt.Errorf("unable to determine the event type to send")
}
if err != nil {
return err
}
fmt.Println(string(bytes))
return nil
}
// Regular output
if e.IsLogEvent() {
values := e.GetValues(c.IncludeDate, false, c.Timezone)
logger := log.With().Logger()
for k, v := range values {
logger = logger.With().Str(k, v).Logger()
}
logger.Info().Send()
} else if e.IsSmartlockEvent() {
log.Info().
Str("name", e.Smartlock.Name).
Bool("battery_critical", e.Smartlock.State.BatteryCritical).
Bool("keypad_battery_critical", e.Smartlock.State.KeypadBatteryCritical).
Bool("doorsensor_battery_critical", e.Smartlock.State.DoorsensorBatteryCritical).
Int32("battery_percent", e.Smartlock.State.BatteryCharge).
Send()
} else {
return fmt.Errorf("unable to determine the event type to send")
}
return nil
}