Skip to content

Commit

Permalink
[TASK] improve logging
Browse files Browse the repository at this point in the history
  • Loading branch information
genofire committed Jan 17, 2019
1 parent abae92b commit 3b779f3
Show file tree
Hide file tree
Showing 20 changed files with 152 additions and 72 deletions.
3 changes: 2 additions & 1 deletion cmd/config.go
Expand Up @@ -5,11 +5,12 @@ import (
"io/ioutil"
"os"

"github.com/naoina/toml"

"github.com/FreifunkBremen/yanic/database"
"github.com/FreifunkBremen/yanic/respond"
"github.com/FreifunkBremen/yanic/runtime"
"github.com/FreifunkBremen/yanic/webserver"
"github.com/naoina/toml"
)

// Config represents the whole configuration
Expand Down
8 changes: 4 additions & 4 deletions cmd/import.go
@@ -1,12 +1,12 @@
package cmd

import (
"log"
"github.com/bdlm/log"
"github.com/spf13/cobra"

allDatabase "github.com/FreifunkBremen/yanic/database/all"
"github.com/FreifunkBremen/yanic/rrd"
"github.com/FreifunkBremen/yanic/runtime"
"github.com/spf13/cobra"
)

// importCmd represents the import command
Expand All @@ -23,11 +23,11 @@ var importCmd = &cobra.Command{

err := allDatabase.Start(config.Database)
if err != nil {
panic(err)
log.Panicf("could not connect to database: %s", err)
}
defer allDatabase.Close()

log.Println("importing RRD from", path)
log.Infof("importing RRD from %s", path)

for ds := range rrd.Read(path) {
allDatabase.Conn.InsertGlobals(
Expand Down
17 changes: 11 additions & 6 deletions cmd/query.go
Expand Up @@ -2,14 +2,16 @@ package cmd

import (
"encoding/json"
"log"
"fmt"
"net"
"strings"
"time"

"github.com/bdlm/log"
"github.com/spf13/cobra"

"github.com/FreifunkBremen/yanic/respond"
"github.com/FreifunkBremen/yanic/runtime"
"github.com/spf13/cobra"
)

var (
Expand All @@ -28,7 +30,10 @@ var queryCmd = &cobra.Command{
ifaces := strings.Split(args[0], ",")
dstAddress := net.ParseIP(args[1])

log.Printf("Sending request address=%s ifaces=%s", dstAddress, ifaces)
log.WithFields(map[string]interface{}{
"address": dstAddress,
"ifaces": ifaces,
}).Info("sending request")

var ifacesConfigs []respond.InterfaceConfig
for _, iface := range ifaces {
Expand All @@ -52,13 +57,13 @@ var queryCmd = &cobra.Command{
for id, data := range nodes.List {
jq, err := json.Marshal(data)
if err != nil {
log.Printf("%s: %+v", id, data)
fmt.Printf("%s: %+v", id, data)
} else {
jqNeighbours, err := json.Marshal(data.Neighbours)
if err != nil {
log.Printf("%s: %s neighbours: %+v", id, string(jq), data.Neighbours)
fmt.Printf("%s: %s neighbours: %+v", id, string(jq), data.Neighbours)
} else {
log.Printf("%s: %s neighbours: %s", id, string(jq), string(jqNeighbours))
fmt.Printf("%s: %s neighbours: %s", id, string(jq), string(jqNeighbours))
}
}
}
Expand Down
14 changes: 8 additions & 6 deletions cmd/root.go
Expand Up @@ -2,14 +2,16 @@ package cmd

import (
"fmt"
"log"
"os"

"github.com/bdlm/log"
"github.com/bdlm/std/logger"
"github.com/spf13/cobra"
)

var (
timestamps bool
loglevel uint32
)

// RootCmd represents the base command when called without any subcommands
Expand All @@ -35,12 +37,12 @@ func init() {
// Cobra supports persistent flags, which, if defined here,
// will be global for your application.
RootCmd.PersistentFlags().BoolVar(&timestamps, "timestamps", false, "Enables timestamps for log output")
RootCmd.PersistentFlags().Uint32Var(&loglevel, "loglevel", 40, "Show log message starting at level")
}

func initConfig() {
if timestamps {
log.SetFlags(log.Lshortfile)
} else {
log.SetFlags(log.LstdFlags | log.Lshortfile)
}
log.SetLevel(logger.Level(loglevel))
log.SetFormatter(&log.TextFormatter{
DisableTimestamp: timestamps,
})
}
15 changes: 8 additions & 7 deletions cmd/serve.go
@@ -1,18 +1,19 @@
package cmd

import (
"log"
"os"
"os/signal"
"syscall"
"time"

"github.com/bdlm/log"
"github.com/spf13/cobra"

allDatabase "github.com/FreifunkBremen/yanic/database/all"
allOutput "github.com/FreifunkBremen/yanic/output/all"
"github.com/FreifunkBremen/yanic/respond"
"github.com/FreifunkBremen/yanic/runtime"
"github.com/FreifunkBremen/yanic/webserver"
"github.com/spf13/cobra"
)

// serveCmd represents the serve command
Expand All @@ -25,7 +26,7 @@ var serveCmd = &cobra.Command{

err := allDatabase.Start(config.Database)
if err != nil {
panic(err)
log.Panicf("could not connect to database: %s", err)
}
defer allDatabase.Close()

Expand All @@ -34,12 +35,12 @@ var serveCmd = &cobra.Command{

err = allOutput.Start(nodes, config.Nodes)
if err != nil {
panic(err)
log.Panicf("error on init outputs: %s", err)
}
defer allOutput.Close()

if config.Webserver.Enable {
log.Println("starting webserver on", config.Webserver.Bind)
log.Infof("starting webserver on %s", config.Webserver.Bind)
srv := webserver.New(config.Webserver.Bind, config.Webserver.Webroot)
go webserver.Start(srv)
defer srv.Close()
Expand All @@ -50,7 +51,7 @@ var serveCmd = &cobra.Command{
if duration := config.Respondd.Synchronize.Duration; duration > 0 {
now := time.Now()
delay := duration - now.Sub(now.Truncate(duration))
log.Printf("delaying %0.1f seconds", delay.Seconds())
log.Infof("delaying %0.1f seconds", delay.Seconds())
time.Sleep(delay)
}

Expand All @@ -63,7 +64,7 @@ var serveCmd = &cobra.Command{
sigs := make(chan os.Signal, 1)
signal.Notify(sigs, syscall.SIGINT, syscall.SIGTERM)
sig := <-sigs
log.Println("received", sig)
log.Infof("received %s", sig)

},
}
Expand Down
5 changes: 3 additions & 2 deletions database/all/connection.go
Expand Up @@ -2,9 +2,10 @@ package all

import (
"fmt"
"log"
"time"

"github.com/bdlm/log"

"github.com/FreifunkBremen/yanic/database"
"github.com/FreifunkBremen/yanic/runtime"
)
Expand All @@ -19,7 +20,7 @@ func Connect(allConnection map[string]interface{}) (database.Connection, error)
for dbType, conn := range database.Adapters {
configForType := allConnection[dbType]
if configForType == nil {
log.Printf("the output type '%s' has no configuration", dbType)
log.WithField("database", dbType).Infof("no configuration found")
continue
}
dbConfigs, ok := configForType.([]interface{})
Expand Down
7 changes: 4 additions & 3 deletions database/graphite/database.go
@@ -1,11 +1,12 @@
package graphite

import (
"log"
"sync"

"github.com/FreifunkBremen/yanic/database"
"github.com/bdlm/log"
"github.com/fgrosse/graphigo"

"github.com/FreifunkBremen/yanic/database"
)

const (
Expand Down Expand Up @@ -69,7 +70,7 @@ func (c *Connection) addWorker() {
for point := range c.points {
err := c.client.SendAll(point)
if err != nil {
log.Fatal(err)
log.WithField("database", "graphite").Fatal(err)
return
}
}
Expand Down
13 changes: 8 additions & 5 deletions database/influxdb/database.go
@@ -1,10 +1,10 @@
package influxdb

import (
"log"
"sync"
"time"

"github.com/bdlm/log"
"github.com/influxdata/influxdb1-client/models"
"github.com/influxdata/influxdb1-client/v2"

Expand Down Expand Up @@ -100,13 +100,16 @@ func (conn *Connection) addPoint(name string, tags models.Tags, fields models.Fi
if value, ok := valueInterface.(string); ok && tags.Get([]byte(tag)) == nil {
tags.SetString(tag, value)
} else {
log.Println(name, "could not saved configured value of tag", tag)
log.WithFields(map[string]interface{}{
"name": name,
"tag": tag,
}).Warnf("count not save tag configuration on point")
}
}
}
point, err := client.NewPoint(name, tags.Map(), fields, t...)
if err != nil {
panic(err)
log.Panicf("count not save points: %s", err)
}
conn.points <- point
}
Expand Down Expand Up @@ -156,10 +159,10 @@ func (conn *Connection) addWorker() {

// write batch now?
if bp != nil && (writeNow || closed || len(bp.Points()) >= batchMaxSize) {
log.Println("saving", len(bp.Points()), "points")
log.WithField("count", len(bp.Points())).Info("saving points")

if err = conn.client.Write(bp); err != nil {
log.Print(err)
log.Error(err)
}
writeNow = false
bp = nil
Expand Down
3 changes: 1 addition & 2 deletions database/logging/file.go
Expand Up @@ -7,7 +7,6 @@ package logging
*/
import (
"fmt"
"log"
"os"
"time"

Expand Down Expand Up @@ -64,6 +63,6 @@ func (conn *Connection) Close() {
}

func (conn *Connection) log(v ...interface{}) {
log.Println(v...)
fmt.Println(v...)
conn.file.WriteString(fmt.Sprintln("[", time.Now().String(), "]", v))
}
11 changes: 6 additions & 5 deletions database/respondd/main.go
Expand Up @@ -7,10 +7,11 @@ import (
"bufio"
"compress/flate"
"encoding/json"
"log"
"net"
"time"

"github.com/bdlm/log"

"github.com/FreifunkBremen/yanic/data"
"github.com/FreifunkBremen/yanic/database"
"github.com/FreifunkBremen/yanic/runtime"
Expand Down Expand Up @@ -59,7 +60,7 @@ func (conn *Connection) InsertNode(node *runtime.Node) {

flater, err := flate.NewWriter(writer, flate.BestCompression)
if err != nil {
log.Printf("[database-yanic] could not create flater: %s", err)
log.Errorf("[database-yanic] could not create flater: %s", err)
return
}
defer flater.Close()
Expand All @@ -69,16 +70,16 @@ func (conn *Connection) InsertNode(node *runtime.Node) {
if node.Nodeinfo != nil && node.Nodeinfo.NodeID != "" {
nodeid = node.Nodeinfo.NodeID
}
log.Printf("[database-yanic] could not encode %s node: %s", nodeid, err)
log.WithField("node_id", nodeid).Errorf("[database-yanic] could not encode node: %s", err)
return
}
err = flater.Flush()
if err != nil {
log.Printf("[database-yanic] could not compress: %s", err)
log.Errorf("[database-yanic] could not compress: %s", err)
}
err = writer.Flush()
if err != nil {
log.Printf("[database-yanic] could not send: %s", err)
log.Errorf("[database-yanic] could not send: %s", err)
}
}

Expand Down
37 changes: 36 additions & 1 deletion main.go
@@ -1,7 +1,42 @@
package main

import "github.com/FreifunkBremen/yanic/cmd"
import (
"os"

"github.com/bdlm/log"
stdLogger "github.com/bdlm/std/logger"

"github.com/FreifunkBremen/yanic/cmd"
)

type Hook struct{}

func (hook *Hook) Fire(entry *log.Entry) error {
switch entry.Level {
case log.PanicLevel:
entry.Logger.Out = os.Stderr
case log.FatalLevel:
entry.Logger.Out = os.Stderr
case log.ErrorLevel:
entry.Logger.Out = os.Stderr
case log.WarnLevel:
entry.Logger.Out = os.Stdout
case log.InfoLevel:
entry.Logger.Out = os.Stdout
case log.DebugLevel:
entry.Logger.Out = os.Stdout
default:
}

return nil
}

func (hook *Hook) Levels() []stdLogger.Level {
return log.AllLevels
}

func main() {
log.AddHook(&Hook{})

cmd.Execute()
}

0 comments on commit 3b779f3

Please sign in to comment.