Skip to content

Commit ca43ada

Browse files
committed
log to logfile
1 parent cfd8e67 commit ca43ada

File tree

5 files changed

+57
-19
lines changed

5 files changed

+57
-19
lines changed

.gitignore

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
.DS_Store
22
blimp.toml
3-
blimp
3+
blimp
4+
blimp.log

go.mod

+2-2
Original file line numberDiff line numberDiff line change
@@ -10,15 +10,16 @@ require (
1010
github.com/knadh/koanf/providers/file v0.1.0
1111
github.com/knadh/koanf/providers/rawbytes v0.1.0
1212
github.com/knadh/koanf/v2 v2.0.1
13+
github.com/mmcdole/gofeed v1.2.1
1314
github.com/prometheus-community/pro-bing v0.3.0
1415
github.com/rivo/tview v0.0.0-20230826141931-7344139b5532
1516
github.com/rs/zerolog v1.30.0
17+
gopkg.in/natefinch/lumberjack.v2 v2.2.1
1618
)
1719

1820
require (
1921
github.com/PuerkitoBio/goquery v1.8.0 // indirect
2022
github.com/andybalholm/cascadia v1.3.1 // indirect
21-
github.com/briandowns/openweathermap v0.19.0 // indirect
2223
github.com/fsnotify/fsnotify v1.6.0 // indirect
2324
github.com/gdamore/encoding v1.0.0 // indirect
2425
github.com/google/uuid v1.3.0 // indirect
@@ -31,7 +32,6 @@ require (
3132
github.com/mitchellh/copystructure v1.2.0 // indirect
3233
github.com/mitchellh/mapstructure v1.5.0 // indirect
3334
github.com/mitchellh/reflectwalk v1.0.2 // indirect
34-
github.com/mmcdole/gofeed v1.2.1 // indirect
3535
github.com/mmcdole/goxpp v1.1.0 // indirect
3636
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect
3737
github.com/modern-go/reflect2 v1.0.2 // indirect

go.sum

+2-2
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,6 @@ github.com/PuerkitoBio/goquery v1.8.0 h1:PJTF7AmFCFKk1N6V6jmKfrNH9tV5pNE6lZMkG0g
22
github.com/PuerkitoBio/goquery v1.8.0/go.mod h1:ypIiRMtY7COPGk+I/YbZLbxsxn9g5ejnI2HSMtkjZvI=
33
github.com/andybalholm/cascadia v1.3.1 h1:nhxRkql1kdYCc8Snf7D5/D3spOX+dBgjA6u8x004T2c=
44
github.com/andybalholm/cascadia v1.3.1/go.mod h1:R4bJ1UQfqADjvDa4P6HZHLh/3OxWWEqc0Sk8XGwHqvA=
5-
github.com/briandowns/openweathermap v0.19.0 h1:nkopLMEtZLxbZI1th6dOG6xkajpszofqf53r5K8mT9k=
6-
github.com/briandowns/openweathermap v0.19.0/go.mod h1:0GLnknqicWxXnGi1IqoOaZIw+kIe5hkt+YM5WY3j8+0=
75
github.com/coreos/go-systemd/v22 v22.5.0/go.mod h1:Y58oyj3AT4RCenI/lSvhwexgC+NSVTIJ3seZv2GcEnc=
86
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
97
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
@@ -117,4 +115,6 @@ golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGm
117115
golang.org/x/tools v0.0.0-20191119224855-298f0cb1881e/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo=
118116
golang.org/x/tools v0.1.12/go.mod h1:hNGJHUnrk76NpqgfD5Aqm5Crs+Hm0VOH/i9J2+nxYbc=
119117
golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
118+
gopkg.in/natefinch/lumberjack.v2 v2.2.1 h1:bBRl1b0OH9s/DuPhuXpNl+VtCaJXFZ5/uEFST95x9zc=
119+
gopkg.in/natefinch/lumberjack.v2 v2.2.1/go.mod h1:YD8tP3GAjkrDg1eZH7EGmyESg/lsYskCTPBJVb9jqSc=
120120
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=

internal/logging/logging.go

+32
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
package logging
2+
3+
import (
4+
"io"
5+
"os"
6+
7+
"github.com/merlinfuchs/blimp/internal/config"
8+
"github.com/rs/zerolog"
9+
"github.com/rs/zerolog/log"
10+
"gopkg.in/natefinch/lumberjack.v2"
11+
)
12+
13+
func InitLogger() {
14+
logContext := log.With()
15+
16+
logWriters := make([]io.Writer, 0)
17+
logWriters = append(logWriters, &lumberjack.Logger{
18+
Filename: config.K.String("logging.filename"),
19+
MaxSize: config.K.Int("logging.max_size"),
20+
MaxAge: config.K.Int("logging.max_age"),
21+
MaxBackups: config.K.Int("logging.max_backups"),
22+
})
23+
if config.K.Bool("debug") {
24+
zerolog.SetGlobalLevel(zerolog.DebugLevel)
25+
logWriters = append(logWriters, zerolog.ConsoleWriter{Out: os.Stdout})
26+
} else {
27+
zerolog.SetGlobalLevel(zerolog.InfoLevel)
28+
}
29+
30+
writer := io.MultiWriter(logWriters...)
31+
log.Logger = logContext.Logger().Output(writer)
32+
}

internal/widgets/latency/latency.go

+19-14
Original file line numberDiff line numberDiff line change
@@ -33,31 +33,36 @@ func New() *LatencyView {
3333
}
3434

3535
func (l *LatencyView) Start() {
36-
pinger, err := ping.NewPinger(config.K.String("widgets.latency.target_host"))
37-
if err != nil {
38-
log.Fatal().Err(err).Msgf("Failed to create pinger, latency won't be displayed")
39-
return
40-
}
36+
var pinger *ping.Pinger
4137

4238
go func() {
4339
<-l.stopped
44-
pinger.Stop()
40+
if pinger != nil {
41+
pinger.Stop()
42+
}
4543
}()
4644

4745
go func() {
48-
pinger.SetPrivileged(false)
49-
pinger.Interval = time.Duration(config.K.Int("widgets.latency.update_interval")) * time.Millisecond
50-
51-
pinger.OnRecv = func(pkt *ping.Packet) {
52-
newValue := float64(pkt.Rtt.Microseconds()) / 1000
53-
l.data = append(l.data[1:], newValue)
54-
}
55-
5646
for {
5747
select {
5848
case <-l.stopped:
5949
return
6050
default:
51+
var err error
52+
pinger, err = ping.NewPinger(config.K.String("widgets.latency.target_host"))
53+
if err != nil {
54+
log.Error().Err(err).Msgf("Failed to create pinger")
55+
return
56+
}
57+
58+
pinger.SetPrivileged(false)
59+
pinger.Interval = time.Duration(config.K.Int("widgets.latency.update_interval")) * time.Millisecond
60+
61+
pinger.OnRecv = func(pkt *ping.Packet) {
62+
newValue := float64(pkt.Rtt.Microseconds()) / 1000
63+
l.data = append(l.data[1:], newValue)
64+
}
65+
6166
err = pinger.Run()
6267
if err != nil {
6368
log.Error().Err(err).Msgf("Failed to run pinger, latency won't be displayed")

0 commit comments

Comments
 (0)