Skip to content

Commit 0b9f8c6

Browse files
committed
fix: improve logging to fix lint
1 parent d482dca commit 0b9f8c6

File tree

26 files changed

+149
-72
lines changed

26 files changed

+149
-72
lines changed

cmd/config.go

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
package cmd
22

33
import (
4-
"fmt"
54
"os"
65

76
"github.com/BurntSushi/toml"
7+
"github.com/bdlm/log"
88

99
"github.com/FreifunkBremen/yanic/database"
1010
"github.com/FreifunkBremen/yanic/respond"
@@ -29,7 +29,7 @@ var (
2929
func loadConfig() *Config {
3030
config, err := ReadConfigFile(configPath)
3131
if err != nil {
32-
fmt.Fprintln(os.Stderr, "unable to load config file:", err)
32+
log.WithError(err).Error("unable to load config file")
3333
os.Exit(2)
3434
}
3535
return config
@@ -43,7 +43,11 @@ func ReadConfigFile(path string) (config *Config, err error) {
4343
if err != nil {
4444
return nil, err
4545
}
46-
defer file.Close()
46+
defer func() {
47+
if err := file.Close(); err != nil {
48+
log.WithError(err).Error("failed to close after read config")
49+
}
50+
}()
4751

4852
_, err = toml.NewDecoder(file).Decode(config)
4953
if err != nil {

cmd/serve.go

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ var serveCmd = &cobra.Command{
2626

2727
err := allDatabase.Start(config.Database)
2828
if err != nil {
29-
log.Panicf("could not connect to database: %s", err)
29+
log.WithError(err).Panic("could not connect to database")
3030
}
3131
defer allDatabase.Close()
3232

@@ -35,15 +35,19 @@ var serveCmd = &cobra.Command{
3535

3636
err = allOutput.Start(nodes, config.Nodes)
3737
if err != nil {
38-
log.Panicf("error on init outputs: %s", err)
38+
log.WithError(err).Panicf("error on init outputs")
3939
}
4040
defer allOutput.Close()
4141

4242
if config.Webserver.Enable {
43-
log.Infof("starting webserver on %s", config.Webserver.Bind)
43+
log.WithField("address", config.Webserver.Bind).Info("starting webserver")
4444
srv := webserver.New(config.Webserver.Bind, config.Webserver.Webroot)
4545
go webserver.Start(srv)
46-
defer srv.Close()
46+
defer func() {
47+
if err := srv.Close(); err != nil {
48+
log.WithError(err).Panic("could not stop webserver")
49+
}
50+
}()
4751
}
4852

4953
if config.Respondd.Enable {

data/statistics.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ package data
55
They always return float.
66
*/
77

8-
//Statistics struct
8+
// Statistics struct
99
type Statistics struct {
1010
NodeID string `json:"node_id"`
1111
Clients Clients `json:"clients"`

database/graphite/database.go

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -58,17 +58,18 @@ func Connect(configuration map[string]interface{}) (database.Connection, error)
5858
func (c *Connection) Close() {
5959
close(c.points)
6060
if c.client.Connection != nil {
61-
c.client.Close()
61+
if err := c.client.Close(); err != nil {
62+
log.WithError(err).Error("unable close connection")
63+
}
6264
}
6365
}
6466

6567
func (c *Connection) addWorker() {
6668
defer c.wg.Done()
6769
defer c.Close()
6870
for point := range c.points {
69-
err := c.client.SendAll(point)
70-
if err != nil {
71-
log.WithField("database", "graphite").Fatal(err)
71+
if err := c.client.SendAll(point); err != nil {
72+
log.WithError(err).WithField("database", "graphite").Fatal("unable to store data")
7273
return
7374
}
7475
}

database/influxdb/database.go

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -142,7 +142,9 @@ func (conn *Connection) addPoint(name string, tags models.Tags, fields models.Fi
142142
func (conn *Connection) Close() {
143143
close(conn.points)
144144
conn.wg.Wait()
145-
conn.client.Close()
145+
if err := conn.client.Close(); err != nil {
146+
log.WithError(err).Error("during close connection")
147+
}
146148
}
147149

148150
// stores data points in batches into the influxdb
@@ -166,7 +168,7 @@ func (conn *Connection) addWorker() {
166168
// create new batch
167169
timer.Reset(batchTimeout)
168170
if bp, err = client.NewBatchPoints(bpConfig); err != nil {
169-
log.Fatal(err)
171+
log.WithError(err).Fatal("not able to create new batch for points")
170172
}
171173
}
172174
bp.AddPoint(point)
@@ -186,7 +188,7 @@ func (conn *Connection) addWorker() {
186188
log.WithField("count", len(bp.Points())).Info("saving points")
187189

188190
if err = conn.client.Write(bp); err != nil {
189-
log.Error(err)
191+
log.WithError(err).Error("not able to write batch of points")
190192
}
191193
writeNow = false
192194
bp = nil

database/influxdb/node_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -126,8 +126,8 @@ func TestToInflux(t *testing.T) {
126126
Subtarget string `json:"subtarget,omitempty"`
127127
ImageName string `json:"image_name,omitempty"`
128128
}{
129-
Base: "gluon",
130-
Target: "x86",
129+
Base: "gluon",
130+
Target: "x86",
131131
Subtarget: "64",
132132
ImageName: "x86-64",
133133
},

database/logging/file.go

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@ import (
1111
"os"
1212
"time"
1313

14+
logger "github.com/bdlm/log"
15+
1416
"github.com/FreifunkBremen/yanic/database"
1517
"github.com/FreifunkBremen/yanic/runtime"
1618
)
@@ -59,7 +61,9 @@ func (conn *Connection) PruneNodes(deleteAfter time.Duration) {
5961

6062
func (conn *Connection) Close() {
6163
conn.log("Close")
62-
conn.file.Close()
64+
if err := conn.file.Close(); err != nil {
65+
logger.WithError(err).Error("unable to close connection")
66+
}
6367
}
6468

6569
func (conn *Connection) log(v ...interface{}) {

database/logging/file_test.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package logging
22

33
import (
4+
"fmt"
45
"os"
56
"testing"
67
"time"
@@ -56,5 +57,7 @@ func TestStart(t *testing.T) {
5657
dat, _ = os.ReadFile(path)
5758
assert.Contains(string(dat), "Close")
5859

59-
os.Remove(path)
60+
if err := os.Remove(path); err != nil {
61+
fmt.Printf("during cleanup: %s\n", err)
62+
}
6063
}

database/respondd/main.go

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,11 @@ func (c Config) Address() string {
3333
return c["address"].(string)
3434
}
3535

36+
var logger *log.Entry
37+
3638
func init() {
3739
database.RegisterAdapter("respondd", Connect)
40+
logger = log.WithField("type", "database-yanic")
3841
}
3942

4043
func Connect(configuration map[string]interface{}) (database.Connection, error) {
@@ -59,26 +62,30 @@ func (conn *Connection) InsertNode(node *runtime.Node) {
5962

6063
flater, err := flate.NewWriter(writer, flate.BestCompression)
6164
if err != nil {
62-
log.Errorf("[database-yanic] could not create flater: %s", err)
65+
logger.WithError(err).Error("could not create flater")
6366
return
6467
}
65-
defer flater.Close()
68+
defer func() {
69+
if err := flater.Close(); err != nil {
70+
logger.WithError(err).Error("could not close flater")
71+
}
72+
}()
6673
err = json.NewEncoder(flater).Encode(res)
6774
if err != nil {
6875
nodeid := "unknown"
6976
if node.Nodeinfo != nil && node.Nodeinfo.NodeID != "" {
7077
nodeid = node.Nodeinfo.NodeID
7178
}
72-
log.WithField("node_id", nodeid).Errorf("[database-yanic] could not encode node: %s", err)
79+
logger.WithError(err).WithField("node_id", nodeid).Error("could not encode node")
7380
return
7481
}
7582
err = flater.Flush()
7683
if err != nil {
77-
log.Errorf("[database-yanic] could not compress: %s", err)
84+
logger.WithError(err).Error("could not compress")
7885
}
7986
err = writer.Flush()
8087
if err != nil {
81-
log.Errorf("[database-yanic] could not send: %s", err)
88+
logger.WithError(err).Error("could not send")
8289
}
8390
}
8491

@@ -92,5 +99,7 @@ func (conn *Connection) PruneNodes(deleteAfter time.Duration) {
9299
}
93100

94101
func (conn *Connection) Close() {
95-
conn.conn.Close()
102+
if err := conn.conn.Close(); err != nil {
103+
logger.WithError(err).Error("cound not close socket")
104+
}
96105
}

lib/jsontime/jsontime.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import (
88
// TimeFormat of JSONTime
99
const TimeFormat = "2006-01-02T15:04:05-0700"
1010

11-
//Time struct of JSONTime
11+
// Time struct of JSONTime
1212
type Time struct {
1313
time time.Time
1414
}
@@ -18,7 +18,7 @@ func Now() Time {
1818
return Time{time.Now()}
1919
}
2020

21-
//MarshalJSON to bytearray
21+
// MarshalJSON to bytearray
2222
func (t Time) MarshalJSON() ([]byte, error) {
2323
stamp := `"` + t.time.Format(TimeFormat) + `"`
2424
return []byte(stamp), nil

0 commit comments

Comments
 (0)