diff --git a/ajax.go b/ajax.go index cbe716f..2ae60f7 100644 --- a/ajax.go +++ b/ajax.go @@ -8,6 +8,7 @@ import ( "github.com/flosch/pongo2" "github.com/gorilla/sessions" + log "github.com/sirupsen/logrus" ) type ajaxResponse struct { @@ -33,7 +34,7 @@ func ajaxGetHandler(res http.ResponseWriter, r *http.Request, session *sessions. userFileRaw, err := storage.Read(user.UserFile) if err != nil { - fmt.Printf("ERR: Unable to read user file: %s\n", err) + log.WithError(err).Error("Could not read user file from storage") res.Write(ajaxResponse{Error: true}.Bytes()) return nil, nil } @@ -60,7 +61,7 @@ func ajaxPostHandler(res http.ResponseWriter, r *http.Request, session *sessions userFileRaw, err := storage.Read(user.UserFile) if err != nil { - fmt.Printf("ERR: Unable to read user file: %s\n", err) + log.WithError(err).Error("Could not read user file from storage") res.Write(ajaxResponse{Error: true, Type: "storage_error"}.Bytes()) return nil, nil } @@ -84,7 +85,7 @@ func ajaxPostHandler(res http.ResponseWriter, r *http.Request, session *sessions } if err := storage.Backup(user.UserFile); err != nil { - fmt.Printf("ERR: Unable to backup user file: %s\n", err) + log.WithError(err).Error("Could not create backup of user file") res.Write(ajaxResponse{Error: true, Type: "storage_error"}.Bytes()) return nil, nil } @@ -95,7 +96,7 @@ func ajaxPostHandler(res http.ResponseWriter, r *http.Request, session *sessions d, _ := userFile.GetData() if err := storage.Write(user.UserFile, d); err != nil { - fmt.Printf("ERR: Unable to write user file: %s\n", err) + log.WithError(err).Error("Could not write user file to storage") res.Write(ajaxResponse{Error: true, Type: "storage_error"}.Bytes()) return nil, nil } diff --git a/httpHelper.go b/httpHelper.go index f6e20a4..4a0176c 100644 --- a/httpHelper.go +++ b/httpHelper.go @@ -1,11 +1,11 @@ package main import ( - "fmt" "net/http" "github.com/flosch/pongo2" "github.com/gorilla/sessions" + log "github.com/sirupsen/logrus" ) type httpHelperFunc func(res http.ResponseWriter, r *http.Request, session *sessions.Session, ctx *pongo2.Context) (*string, error) @@ -22,7 +22,7 @@ func httpHelper(f httpHelperFunc) http.HandlerFunc { template, err := f(res, r, sess, &ctx) if err != nil { http.Error(res, "An error ocurred.", http.StatusInternalServerError) - fmt.Printf("ERR: %s\n", err) + log.WithError(err).Error("Unable to execute template") return } @@ -31,13 +31,17 @@ func httpHelper(f httpHelperFunc) http.HandlerFunc { ts.SetBaseDirectory("templates") tpl, err := ts.FromFile(*template) if err != nil { - fmt.Printf("ERR: Could not parse template '%s': %s\n", *template, err) + log.WithError(err).WithFields(log.Fields{ + "template": *template, + }).Error("Could not parse template") http.Error(res, "An error ocurred.", http.StatusInternalServerError) return } out, err := tpl.Execute(ctx) if err != nil { - fmt.Printf("ERR: Unable to execute template '%s': %s\n", *template, err) + log.WithError(err).WithFields(log.Fields{ + "template": *template, + }).Error("Could not execute template") http.Error(res, "An error ocurred.", http.StatusInternalServerError) return } diff --git a/login.go b/login.go index 0177780..870c00d 100644 --- a/login.go +++ b/login.go @@ -12,6 +12,7 @@ import ( "github.com/flosch/pongo2" "github.com/gorilla/mux" "github.com/gorilla/sessions" + log "github.com/sirupsen/logrus" ) func loginHandler(res http.ResponseWriter, r *http.Request, session *sessions.Session, ctx *pongo2.Context) (*string, error) { @@ -28,7 +29,7 @@ func loginHandler(res http.ResponseWriter, r *http.Request, session *sessions.Se userFileRaw, err := storage.Read(createUserFilename(username)) if err != nil { - fmt.Printf("ERR: Unable to read user file: %s\n", err) + log.WithError(err).Error("Unable to read user file") (*ctx)["error"] = true return stringPointer("login.html"), nil } diff --git a/main.go b/main.go index 47cc861..80c8890 100644 --- a/main.go +++ b/main.go @@ -13,6 +13,7 @@ import ( "github.com/gorilla/mux" "github.com/gorilla/sessions" "github.com/satori/go.uuid" + log "github.com/sirupsen/logrus" ) var ( @@ -30,18 +31,18 @@ func init() { } if _, err := cfg.ParsedStorage(); err != nil { - fmt.Printf("ERR: Please provide a valid storage URI\n") + log.WithError(err).Error("Unable to parse storage URI") os.Exit(1) } if cfg.CookieSigningKey == "" { cfg.CookieSigningKey = uuid.NewV4().String()[:32] - fmt.Printf("WRN: cookie-authkey was set randomly, this will break your sessions!\n") + log.Warn("cookie-authkey was set randomly, this will break your sessions!") } if cfg.CookieEncryptKey == "" { cfg.CookieEncryptKey = uuid.NewV4().String()[:32] - fmt.Printf("WRN: cookie-encryptkey was set randomly, this will break your sessions!\n") + log.Warn("cookie-encryptkey was set randomly, this will break your sessions!") } cookieStore = sessions.NewCookieStore( @@ -53,8 +54,7 @@ func init() { func main() { s, err := getStorageAdapter(cfg) if err != nil { - fmt.Printf("ERR: Could not instanciate storage: %s\n", err) - os.Exit(1) + log.WithError(err).Fatal("Could not instanciate storage") } storage = s diff --git a/register.go b/register.go index 6afd2d9..cd1d868 100644 --- a/register.go +++ b/register.go @@ -1,7 +1,6 @@ package main import ( - "fmt" "net/http" "strings" @@ -9,6 +8,7 @@ import ( "github.com/flosch/pongo2" "github.com/gorilla/sessions" + log "github.com/sirupsen/logrus" ) func registerHandler(res http.ResponseWriter, r *http.Request, session *sessions.Session, ctx *pongo2.Context) (*string, error) { @@ -29,7 +29,7 @@ func registerHandler(res http.ResponseWriter, r *http.Request, session *sessions hashedPassword, err := bcrypt.GenerateFromPassword([]byte(password), bcrypt.DefaultCost) if err != nil { - fmt.Printf("ERR: Unable to hash users password: %s\n", err) + log.WithError(err).Error("Could not hash user password") (*ctx)["error"] = true return stringPointer("register.html"), nil } @@ -39,7 +39,7 @@ func registerHandler(res http.ResponseWriter, r *http.Request, session *sessions data, _ := d.GetData() if err := storage.Write(createUserFilename(username), data); err != nil { - fmt.Printf("ERR: Unable to write user file: %s\n", err) + log.WithError(err).Error("Could not write user file to storage") (*ctx)["error"] = true return stringPointer("register.html"), nil } diff --git a/storageRedis.go b/storageRedis.go index 01cb018..dd4e116 100644 --- a/storageRedis.go +++ b/storageRedis.go @@ -2,7 +2,6 @@ package main import ( "bytes" - "fmt" "io" "io/ioutil" "net/url" @@ -10,6 +9,7 @@ import ( "strings" "time" + log "github.com/sirupsen/logrus" "github.com/xuyu/goredis" ) @@ -57,7 +57,10 @@ func (r *RedisStorage) Read(identifier string) (io.Reader, error) { func (r *RedisStorage) IsPresent(identifier string) bool { e, err := r.conn.Exists(r.prefix + identifier) if err != nil { - fmt.Printf("ERR: %s\n", err) + log.WithError(err).WithFields(log.Fields{ + "storagedriver": "redis", + "identifier": identifier, + }).Error("Unable to check key existence") } return e && err == nil }