Skip to content

Commit

Permalink
fix: initial logging implementation using logrus (#32)
Browse files Browse the repository at this point in the history
* fix: use varchar(128) for clientId

* fix: enforce clientId max length at app layer

* fix: save client timestamp in database

* fix: initial logging implementation with logrus

* fix: add comment

* fix: exit if invalid log options supplied
  • Loading branch information
Dara Hayes authored Mar 1, 2018
1 parent dcc91f8 commit 2445620
Show file tree
Hide file tree
Showing 7 changed files with 82 additions and 11 deletions.
23 changes: 22 additions & 1 deletion Gopkg.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 4 additions & 0 deletions Gopkg.toml
Original file line number Diff line number Diff line change
Expand Up @@ -17,3 +17,7 @@
[[constraint]]
name = "github.com/stretchr/testify"
version = "1.2.1"

[[constraint]]
name = "github.com/sirupsen/logrus"
version = "1.0.4"
26 changes: 24 additions & 2 deletions cmd/metrics-api/metrics-api.go
Original file line number Diff line number Diff line change
@@ -1,19 +1,21 @@
package main

import (
"log"
"net/http"

"github.com/aerogear/aerogear-app-metrics/pkg/config"
"github.com/aerogear/aerogear-app-metrics/pkg/dao"
"github.com/aerogear/aerogear-app-metrics/pkg/mobile"
"github.com/aerogear/aerogear-app-metrics/pkg/web"
log "github.com/sirupsen/logrus"
)

func main() {

config := config.GetConfig()

initLogger(config.LogLevel, config.LogFormat)

dbHandler := dao.DatabaseHandler{}

err := dbHandler.Connect(config.DBConnectionString, config.DBMaxConnections)
Expand Down Expand Up @@ -42,10 +44,30 @@ func main() {
web.HealthzRoute(router, healthHandler)
}

log.Printf("Starting application... going to listen on %v", config.ListenAddress)
log.WithFields(log.Fields{"listenAddress": config.ListenAddress}).Info("Starting application")

//start
if err := http.ListenAndServe(config.ListenAddress, router); err != nil {
panic("failed to start " + err.Error())
}
}

func initLogger(level, format string) {
logLevel, err := log.ParseLevel(level)

if err != nil {
log.Fatalf("log level %v is not allowed. Must be one of [debug, info, warning, error, fatal, panic]", level)
logLevel = log.InfoLevel
}

log.SetLevel(logLevel)

switch format {
case "json":
log.SetFormatter(&log.JSONFormatter{})
case "text":
log.SetFormatter(&log.TextFormatter{DisableColors: true})
default:
log.Fatalf("log format %v is not allowed. Must be one of [text, json]", format)
}
}
4 changes: 4 additions & 0 deletions pkg/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,17 @@ type config struct {
DBConnectionString string
DBMaxConnections int
ListenAddress string
LogLevel string
LogFormat string
}

func GetConfig() config {
return config{
DBConnectionString: getDBConnectionString(),
DBMaxConnections: getEnvInt("DBMAX_CONNECTIONS", 100),
ListenAddress: fmt.Sprintf(":%v", getEnvInt("PORT", 3000)),
LogLevel: strings.ToLower(getEnv("LOG_LEVEL", "info")),
LogFormat: strings.ToLower(getEnv("LOG_FORMAT", "text")), //can be text or json
}
}

Expand Down
14 changes: 14 additions & 0 deletions pkg/config/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ func TestConfig(t *testing.T) {
ListenAddress: ":3000",
DBMaxConnections: 100,
DBConnectionString: "connect_timeout=5 dbname=aerogear_mobile_metrics host=localhost password=postgres port=5432 sslmode=disable user=postgresql",
LogFormat: "text",
LogLevel: "info",
},
EnvVars: map[string]string{},
},
Expand All @@ -28,13 +30,17 @@ func TestConfig(t *testing.T) {
ListenAddress: ":3000",
DBMaxConnections: 100,
DBConnectionString: "connect_timeout=5 dbname=testing host=testing password=testing port=5432 sslmode=testing user=testing",
LogFormat: "testing",
LogLevel: "testing",
},
EnvVars: map[string]string{
"PGHOST": "testing",
"PGUSER": "testing",
"PGPASSWORD": "testing",
"PGDATABASE": "testing",
"PGSSLMODE": "testing",
"LOG_LEVEL": "testing",
"LOG_FORMAT": "testing",
},
},
{
Expand All @@ -43,6 +49,8 @@ func TestConfig(t *testing.T) {
ListenAddress: ":3000",
DBMaxConnections: 100,
DBConnectionString: "connect_timeout=5 dbname=aerogear_mobile_metrics host=localhost password=postgres port=5432 sslmode=disable user=postgresql",
LogFormat: "text",
LogLevel: "info",
},
EnvVars: map[string]string{
"PGHOST": "",
Expand All @@ -51,6 +59,8 @@ func TestConfig(t *testing.T) {
"PGDATABASE": "",
"PGSSLMODE": "",
"PORT": "",
"LOG_LEVEL": "",
"LOG_FORMAT": "",
},
},
{
Expand All @@ -59,6 +69,8 @@ func TestConfig(t *testing.T) {
ListenAddress: ":4000",
DBMaxConnections: 5,
DBConnectionString: "connect_timeout=5 dbname=aerogear_mobile_metrics host=localhost password=postgres port=5432 sslmode=disable user=postgresql",
LogFormat: "text",
LogLevel: "info",
},
EnvVars: map[string]string{
"DBMAX_CONNECTIONS": "5",
Expand All @@ -71,6 +83,8 @@ func TestConfig(t *testing.T) {
ListenAddress: ":3000",
DBMaxConnections: 100,
DBConnectionString: "connect_timeout=5 dbname=aerogear_mobile_metrics host=localhost password=postgres port=5432 sslmode=disable user=postgresql",
LogFormat: "text",
LogLevel: "info",
},
EnvVars: map[string]string{
"DBMAX_CONNECTIONS": "not an integer",
Expand Down
7 changes: 6 additions & 1 deletion pkg/web/metricsHandler.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (

"github.com/aerogear/aerogear-app-metrics/pkg/mobile"
"github.com/darahayes/go-boom"
log "github.com/sirupsen/logrus"
)

type metricsHandler struct {
Expand All @@ -23,11 +24,13 @@ func (mh *metricsHandler) CreateMetric(w http.ResponseWriter, r *http.Request) {

// decode the client payload into the metric var
if err := json.NewDecoder(r.Body).Decode(&metric); err != nil {
boom.BadRequest(w, "Invalid Data")
log.WithFields(log.Fields{"error": err.Error()}).Error("error parsing client payload")
boom.BadRequest(w, "invalid JSON payload")
return
}

if valid, reason := metric.Validate(); !valid {
log.WithFields(log.Fields{"reason": reason}).Info("invalid client payload")
boom.BadRequest(w, reason)
return
}
Expand All @@ -37,11 +40,13 @@ func (mh *metricsHandler) CreateMetric(w http.ResponseWriter, r *http.Request) {

// handle errors
if err != nil {
log.WithFields(log.Fields{"error": err.Error()}).Error("error creating metric")
boom.BadImplementation(w)
return
}

if err := withJSON(w, 200, result); err != nil {
log.WithFields(log.Fields{"error": err.Error()}).Error("error responding to client")
boom.BadImplementation(w)
return
}
Expand Down
15 changes: 8 additions & 7 deletions pkg/web/middlewares.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
package web

import (
"log"
"net/http"
"time"

log "github.com/sirupsen/logrus"
)

func loggerMiddleWare(next http.Handler) http.Handler {
Expand All @@ -12,12 +13,12 @@ func loggerMiddleWare(next http.Handler) http.Handler {

next.ServeHTTP(w, r)

log.Printf(
"%s\t%s\t%s",
r.Method,
r.RequestURI,
time.Since(start),
)
log.WithFields(log.Fields{
"method": r.Method,
"path": r.RequestURI,
"client_ip": r.RemoteAddr,
"response_time": time.Since(start),
}).Info()
})
}

Expand Down

0 comments on commit 2445620

Please sign in to comment.