This repository has been archived by the owner on Sep 3, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathoptions.go
95 lines (83 loc) · 2.45 KB
/
options.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
package zap2telegram
import (
"context"
"go.uber.org/zap"
"time"
"go.uber.org/zap/zapcore"
)
type Option func(*TelegramCore) error
// WithLevelEnabler uses the provided enabler to decide which level should be logged
func WithLevelEnabler(l zapcore.LevelEnabler) Option {
return func(h *TelegramCore) error {
h.enabler = l
return nil
}
}
// WithLevel sends messages equal or above specified level
func WithLevel(l zapcore.Level) Option {
return func(h *TelegramCore) error {
h.enabler = zap.NewAtomicLevelAt(l)
return nil
}
}
// WithStrongLevel sends only messages with specified level
func WithStrongLevel(l zapcore.Level) Option {
return func(h *TelegramCore) error {
h.enabler = zap.LevelEnablerFunc(func(lvl zapcore.Level) bool { return lvl == l })
return nil
}
}
// WithDisabledNotification disables Telegram message notification
func WithDisabledNotification() Option {
return func(h *TelegramCore) error {
h.telegramClient.disableNotification = true
return nil
}
}
// WithNotificationOn enables Telegram message notification on specified levels
func WithNotificationOn(levels []zapcore.Level) Option {
return func(h *TelegramCore) error {
h.telegramClient.disableNotification = true
h.telegramClient.enableNotificationOnLevels = levels
return nil
}
}
// WithParseMode sets parse mode for Telegram messages
// (E.g: "ModeMarkdown", "ModeMarkdownV2" or "ModeHTML")
// https://core.telegram.org/bots/api#formatting-options
func WithParseMode(parseMode string) Option {
return func(h *TelegramCore) error {
h.telegramClient.parseMode = &parseMode
return nil
}
}
// WithFormatter sets a custom Telegram message formatter
func WithFormatter(f func(e zapcore.Entry, fields []zapcore.Field) string) Option {
return func(h *TelegramCore) error {
h.telegramClient.formatter = f
return nil
}
}
// WithoutAsyncOpt disables default asynchronous mode and enables synchronous mode for messages sending (blocking)
func WithoutAsyncOpt() Option {
return func(h *TelegramCore) error {
if h.queue {
return ErrAsyncOpt
}
h.async = false
return nil
}
}
// WithQueue sends the messages to Telegram in batches (burst) at the specified interval
func WithQueue(ctx context.Context, interval time.Duration, queueSize int) Option {
return func(h *TelegramCore) error {
h.async = false
h.queue = true
h.intervalQueue = interval
h.entriesChan = make(chan chanEntry, queueSize)
go func() {
_ = h.consumeEntriesQueue(ctx)
}()
return nil
}
}