-
Notifications
You must be signed in to change notification settings - Fork 0
/
logger.go
65 lines (53 loc) · 1.22 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
64
65
package xivish
import (
"fmt"
"time"
)
type LoggerQueueCfg struct {
Enabled bool
Interval time.Duration
stop chan bool
}
type Logger struct {
DisableAll bool
Queue LoggerQueueCfg
}
// AttachLogger attaches a logger to the scraper
func (s *Scraper) AttachLogger(logger Logger) {
s.logger = logger
}
// log logs a message with a prefix and status
func (l *Logger) log(status string, prefix string, msg any) {
if !l.DisableAll {
fmt.Printf("[%s] %s: %v\n", status, prefix, msg)
}
}
// LogInfo logs an info message
func (l *Logger) LogInfo(prefix string, msg any) {
l.log("INFO", prefix, msg)
}
// LogError logs an error message
func (l *Logger) LogError(prefix string, msg any) {
l.log("ERROR", prefix, msg)
}
// DisableAll disables all logging
func (l *Logger) Disable() {
l.DisableAll = true
l.Close()
}
// Close stops the logging
func (l *Logger) Close() {
if l.Queue.stop != nil {
l.Queue.stop <- true
}
}
// SetQueueState sets whether the queue progress should be logged and the interval
func (l *Logger) SetQueueState(enabled bool, interval *time.Duration) {
l.Queue.Enabled = enabled
if interval != nil {
l.Queue.Interval = *interval
}
if !enabled && l.Queue.stop != nil {
l.Queue.stop <- true
}
}