-
Notifications
You must be signed in to change notification settings - Fork 0
/
zap.go
107 lines (96 loc) · 2.27 KB
/
zap.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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
package snitch
import (
"fmt"
"time"
"go.uber.org/zap"
"go.uber.org/zap/zapcore"
)
type Zap struct {
c chan<- string
Conf *Config
bot bot
L *zap.Logger
}
// Function OnZap constructs wrapper around zap.Logger.
func OnZap(logger *zap.Logger, conf *Config) (*Zap, error) {
c := make(chan string, 10)
b, err := newBot(conf)
if err != nil {
return nil, fmt.Errorf("failed to init tg bot: %w", err)
}
back, err := newBackend(conf, b, c)
if err != nil {
return nil, fmt.Errorf("failed to start backend: %w", err)
}
go back.start()
return &Zap{
c: c,
Conf: conf,
bot: b,
L: logger,
}, nil
}
// Debug directly calls logger.Debug().
func (s *Zap) Debug(msg string, fields ...zapcore.Field) {
if s.Conf.Level <= DebugLevel && msg != "" {
select {
case s.c <- DebugPrefix + msg:
default:
s.L.Debug("channel buzy, message dropped")
}
}
s.L.Debug(msg, fields...)
}
func (s *Zap) Info(msg string, fields ...zapcore.Field) {
if s.Conf.Level <= InfoLevel && msg != "" {
select {
case s.c <- InfoPrefix + msg:
default:
s.L.Info("channel buzy, message dropped")
}
}
s.L.Info(msg, fields...)
}
func (s *Zap) Warn(msg string, fields ...zapcore.Field) {
if s.Conf.Level <= WarnLevel && msg != "" {
select {
case s.c <- WarnPrefix + msg:
default:
s.L.Warn("channel buzy, message dropped")
}
}
s.L.Warn(msg, fields...)
}
func (s *Zap) Error(msg string, fields ...zapcore.Field) {
if s.Conf.Level <= ErrorLevel && msg != "" {
select {
case s.c <- ErrorPrefix + msg:
default:
s.L.Error("channel buzy, message dropped")
}
}
s.L.Error(msg, fields...)
}
// Panic snitches if level <= CritLevel and calls logger.Panic().
func (s *Zap) Panic(msg string, fields ...zapcore.Field) {
if s.Conf.Level <= CritLevel {
s.allHellBrokeLoose(msg)
}
s.L.Panic(msg, fields...)
}
// Fatal snitches if level <= CritLevel and calls logger.Fatal().
func (s *Zap) Fatal(msg string, fields ...zapcore.Field) {
if s.Conf.Level <= CritLevel {
s.allHellBrokeLoose(msg)
}
s.L.Fatal(msg, fields...)
}
func (s *Zap) allHellBrokeLoose(msg string) {
chat, err := s.bot.ChatByID(s.Conf.TGChatID)
if err != nil {
if _, e := s.bot.Send(chat, CritPrefix+msg); e != nil {
time.Sleep(50 * time.Millisecond)
_, _ = s.bot.Send(chat, "CRITICAL!")
}
}
}