change to log15-based network logging

This commit is contained in:
Seán C McCord 2018-05-18 14:13:25 -04:00
parent ea6a8615bb
commit 78d0af12cc
3 changed files with 26 additions and 24 deletions

View file

@ -4,6 +4,7 @@ import (
"bytes"
"encoding/json"
"errors"
"fmt"
"html/template"
"net/http"
"os"
@ -33,13 +34,13 @@ func contactRequest(c echo.Context) (err error) {
req := new(ContactRequest)
if err = cc.Bind(req); err != nil {
cc.Log.Warnf("failed to parse input: %s", err.Error())
cc.Log.Warn("failed to parse input", "error", err)
return c.JSON(http.StatusBadRequest, NewError(errors.New("failed to read request")))
}
cc.Log.Debugf(`received contact request from "%s" <%s> (%s)`, req.Name, req.Email, c.RealIP())
cc.Log.Debug(fmt.Sprintf(`received contact request from "%s" <%s> (%s)`, req.Name, req.Email, c.RealIP()))
if err = db.LogContact(req.Name, req.Email); err != nil {
cc.Log.Warn("failed to write contact record to database")
cc.Log.Warn("failed to write contact record to database", "error", err)
}
if req.Name == "" {
@ -53,7 +54,7 @@ func contactRequest(c echo.Context) (err error) {
emailBody, err := renderContactEmail(req.Name, req.Email)
if err != nil {
cc.Log.Errorf("failed to render email body: %s", err.Error())
cc.Log.Error("failed to render email body", "error", err)
return c.JSON(http.StatusInternalServerError, NewError(errors.New("internal error; please retry")))
}
@ -69,7 +70,7 @@ func contactRequest(c echo.Context) (err error) {
}
if err = msg.Send(os.Getenv("SENDINBLUE_APIKEY")); err != nil {
cc.Log.Errorf("failed to send contact email: %s", err.Error())
cc.Log.Error("failed to send contact email", "error", err)
enc := json.NewEncoder(os.Stdout)
enc.SetIndent("", " ")

View file

@ -3,7 +3,7 @@ package main
import (
"github.com/jmoiron/sqlx"
"github.com/labstack/echo"
"go.uber.org/zap"
"github.com/revel/log15"
)
// Context is the custom context for this web server
@ -14,5 +14,5 @@ type Context struct {
DB *sqlx.DB
// Log is the core logger
Log *zap.SugaredLogger
Log log15.Logger
}

35
main.go
View file

@ -4,11 +4,12 @@ import (
"flag"
"html/template"
"net/http"
"os"
"github.com/CyCoreSystems/cycore-web/db"
"github.com/labstack/echo"
"github.com/labstack/echo/middleware"
"go.uber.org/zap"
"github.com/revel/log15"
)
var addr string
@ -35,24 +36,20 @@ func main() {
flag.Parse()
var err error
var logger *zap.Logger
if debug {
logger, err = zap.NewDevelopment()
} else {
logger, err = zap.NewProduction()
log := log15.New()
if os.Getenv("KUBERNETES_SERVICE_HOST") != "" {
h, err := log15.NetHandler("tcp", "oklog.log", log15.JsonFormat())
if err != nil {
log.Error("failed to construct network logger", "error", err)
} else {
log.SetHandler(h)
}
}
if err != nil {
panic("failed to create logger: " + err.Error())
}
defer logger.Sync() // nolint
log := logger.Sugar()
err = db.Connect()
err := db.Connect()
if err != nil {
log.Panicf("failed to open database: %v", err)
log.Crit("failed to open database", "error", err)
os.Exit(1)
}
defer db.Get().Close() // nolint
@ -88,7 +85,11 @@ func main() {
e.POST("/contact/request", contactRequest)
log.Fatal(e.Start(addr))
if err = e.Start(addr); err != nil {
log.Crit(err.Error())
os.Exit(1)
}
os.Exit(0)
}
func home(c echo.Context) error {