summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJan Dittberner <jan@dittberner.info>2017-04-22 00:06:16 +0200
committerJan Dittberner <jan@dittberner.info>2017-04-22 00:14:38 +0200
commit5a449926f417f75acff415d1a359571d86be0983 (patch)
tree0457a5ab88841156e379c977442a6e87e64e3419
parentfd0a8ed972f6b9327d118292b46ab5c601a48ac3 (diff)
downloadcacert-boardvoting-5a449926f417f75acff415d1a359571d86be0983.tar.gz
cacert-boardvoting-5a449926f417f75acff415d1a359571d86be0983.tar.xz
cacert-boardvoting-5a449926f417f75acff415d1a359571d86be0983.zip
Use loggo for logging0.1.0
-rw-r--r--boardvoting.go66
-rw-r--r--jobs.go24
-rw-r--r--models.go123
-rw-r--r--notifications.go8
4 files changed, 115 insertions, 106 deletions
diff --git a/boardvoting.go b/boardvoting.go
index 539013a..1e62908 100644
--- a/boardvoting.go
+++ b/boardvoting.go
@@ -11,23 +11,22 @@ import (
"github.com/Masterminds/sprig"
"github.com/gorilla/sessions"
"github.com/jmoiron/sqlx"
+ "github.com/juju/loggo"
_ "github.com/mattn/go-sqlite3"
"gopkg.in/yaml.v2"
"html/template"
"io/ioutil"
- "log"
"net/http"
- "os"
"strconv"
"strings"
"time"
)
-var logger *log.Logger
var config *Config
var store *sessions.CookieStore
var version = "undefined"
var build = "undefined"
+var logger loggo.Logger
const sessionCookieName = "votesession"
@@ -243,13 +242,13 @@ type FlashMessageAction struct{}
func (a *FlashMessageAction) AddFlash(w http.ResponseWriter, r *http.Request, message interface{}, tags ...string) (err error) {
session, err := store.Get(r, sessionCookieName)
if err != nil {
- logger.Println("ERROR getting session:", err)
+ logger.Errorf("getting session failed: %s", err)
return
}
session.AddFlash(message, tags...)
session.Save(r, w)
if err != nil {
- logger.Println("ERROR saving session:", err)
+ logger.Errorf("saving session failed: %s", err)
return
}
return
@@ -283,7 +282,7 @@ func (a *withDrawMotionAction) Handle(w http.ResponseWriter, r *http.Request) {
decision.Status = voteStatusWithdrawn
decision.Modified = time.Now().UTC()
if err := decision.UpdateStatus(); err != nil {
- logger.Println("Error withdrawing motion:", err)
+ logger.Errorf("withdrawing motion failed: %s", err)
http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
return
}
@@ -336,7 +335,7 @@ func (h *newMotionHandler) Handle(w http.ResponseWriter, r *http.Request) {
data.Proposed = time.Now().UTC()
data.ProponentId = voter.Id
if err := data.Create(); err != nil {
- logger.Println("Error saving motion:", err)
+ logger.Errorf("saving motion failed: %s", err)
http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
return
}
@@ -401,7 +400,7 @@ func (a editMotionAction) Handle(w http.ResponseWriter, r *http.Request) {
} else {
data.Modified = time.Now().UTC()
if err := data.Update(); err != nil {
- logger.Println("Error updating motion:", err)
+ logger.Errorf("updating motion failed: %s", err)
http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
return
}
@@ -431,10 +430,6 @@ func (a editMotionAction) Handle(w http.ResponseWriter, r *http.Request) {
type motionsHandler struct{}
func (h motionsHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
- if err := db.Ping(); err != nil {
- logger.Fatal(err)
- }
-
subURL := r.URL.Path
var motionActionMap = map[string]motionActionHandler{
@@ -505,7 +500,7 @@ func (h *directVoteHandler) Handle(w http.ResponseWriter, r *http.Request) {
VoterId: voter.Id, Vote: vote, DecisionId: decision.Id, Voted: time.Now().UTC(),
Notes: fmt.Sprintf("Direct Vote\n\n%s", getPEMClientCert(r))}
if err := voteResult.Save(); err != nil {
- logger.Println("ERROR", "Problem saving vote:", err)
+ logger.Errorf("Problem saving vote: %s", err)
http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
return
}
@@ -589,7 +584,7 @@ func (h *proxyVoteHandler) Handle(w http.ResponseWriter, r *http.Request) {
proxy.Name, justification, getPEMClientCert(r))
if err := data.Save(); err != nil {
- logger.Println("Error saving vote:", err)
+ logger.Errorf("Error saving vote: %s", err)
http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
return
}
@@ -620,10 +615,6 @@ func (h *proxyVoteHandler) Handle(w http.ResponseWriter, r *http.Request) {
type decisionVoteHandler struct{}
func (h *decisionVoteHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
- if err := db.Ping(); err != nil {
- logger.Fatal(err)
- }
-
switch {
case strings.HasPrefix(r.URL.Path, "/proxy/"):
motionTag := r.URL.Path[len("/proxy/"):]
@@ -675,35 +666,42 @@ type Config struct {
}
func init() {
- logger = log.New(os.Stderr, "boardvoting: ", log.LstdFlags|log.LUTC|log.Lshortfile)
+ loggo.ConfigureLoggers("<root>=ERROR; boardvoting=INFO")
+ logger = loggo.GetLogger("boardvoting")
+ logger.Infof("Logger initialized")
source, err := ioutil.ReadFile("config.yaml")
if err != nil {
- logger.Fatal(err)
+ logger.Criticalf("Opening configuration file failed: %s", err)
+ panic(err)
}
if err := yaml.Unmarshal(source, &config); err != nil {
- logger.Fatal(err)
+ logger.Criticalf("Loading configuration failed: %s", err)
+ panic(err)
}
cookieSecret, err := base64.StdEncoding.DecodeString(config.CookieSecret)
if err != nil {
- logger.Fatal(err)
+ logger.Criticalf("Decoding cookie secret failed: %s", err)
+ panic(err)
}
if len(cookieSecret) < 32 {
- logger.Fatalln("Cookie secret is less than 32 bytes long")
+ logger.Criticalf("Cookie secret is less than 32 bytes long")
+ panic("Cookie secret too short")
}
store = sessions.NewCookieStore(cookieSecret)
- logger.Println("read configuration")
+ logger.Infof("Read configuration")
db, err = sqlx.Open("sqlite3", config.DatabaseFile)
if err != nil {
- logger.Fatal(err)
+ logger.Criticalf("Opening database failed: %s", err)
+ panic(err)
}
- logger.Println("opened database connection")
+ logger.Infof("opened database connection")
}
func main() {
- logger.Printf("CAcert Board Voting version %s, build %s\n", version, build)
+ logger.Infof("CAcert Board Voting version %s, build %s", version, build)
defer db.Close()
@@ -725,11 +723,13 @@ func main() {
// load CA certificates for client authentication
caCert, err := ioutil.ReadFile(config.ClientCACertificates)
if err != nil {
- logger.Fatal(err)
+ logger.Criticalf("Error reading client certificate CAs", err)
+ panic(err)
}
caCertPool := x509.NewCertPool()
if !caCertPool.AppendCertsFromPEM(caCert) {
- logger.Fatal("could not initialize client CA certificate pool")
+ logger.Criticalf("could not initialize client CA certificate pool")
+ panic("client certificate CA pool initialization failed")
}
// setup HTTPS server
@@ -744,7 +744,7 @@ func main() {
TLSConfig: tlsConfig,
}
- logger.Printf("Launching application on https://localhost%s/\n", server.Addr)
+ logger.Infof("Launching application on https://localhost%s/", server.Addr)
errs := make(chan error, 1)
go func() {
@@ -755,9 +755,11 @@ func main() {
}()
if err = server.ListenAndServeTLS(config.ServerCert, config.ServerKey); err != nil {
- logger.Fatal("ListenAndServerTLS: ", err)
+ logger.Criticalf("ListenAndServerTLS failed: %s", err)
+ panic(err)
}
if err := <-errs; err != nil {
- logger.Fatal("ListenAndServe: ", err)
+ logger.Criticalf("ListenAndServe failed: %s", err)
+ panic(err)
}
}
diff --git a/jobs.go b/jobs.go
index 2535ef1..8d0bf13 100644
--- a/jobs.go
+++ b/jobs.go
@@ -22,19 +22,19 @@ func JobScheduler(quitChannel chan int) {
JobIdCloseDecisions: NewCloseDecisionsJob(),
JobIdRemindVotersJob: NewRemindVotersJob(),
}
- logger.Println("INFO started job scheduler")
+ logger.Infof("started job scheduler")
for {
select {
case jobId := <-rescheduleChannel:
job := jobs[jobId]
- logger.Println("INFO reschedule job", job)
+ logger.Infof("reschedule job %s", job)
job.Schedule()
case <-quitChannel:
for _, job := range jobs {
job.Stop()
}
- logger.Println("INFO stop job scheduler")
+ logger.Infof("stop job scheduler")
return
}
}
@@ -54,7 +54,7 @@ func (j *CloseDecisionsJob) Schedule() {
var nextDue *time.Time
nextDue, err := GetNextPendingDecisionDue()
if err != nil {
- logger.Fatal("ERROR Could not get next pending due date")
+ logger.Errorf("Could not get next pending due date")
if j.timer != nil {
j.timer.Stop()
j.timer = nil
@@ -62,11 +62,11 @@ func (j *CloseDecisionsJob) Schedule() {
return
}
if nextDue == nil {
- logger.Println("INFO no next planned execution of CloseDecisionsJob")
+ logger.Infof("no next planned execution of CloseDecisionsJob")
j.Stop()
} else {
nextDue := nextDue.Add(time.Minute)
- logger.Println("INFO scheduling CloseDecisionsJob for", nextDue)
+ logger.Infof("scheduling CloseDecisionsJob for %s", nextDue)
when := nextDue.Sub(time.Now())
if j.timer != nil {
j.timer.Reset(when)
@@ -84,10 +84,10 @@ func (j *CloseDecisionsJob) Stop() {
}
func (j *CloseDecisionsJob) Run() {
- logger.Println("INFO running CloseDecisionsJob")
+ logger.Infof("running CloseDecisionsJob")
err := CloseDecisions()
if err != nil {
- logger.Println("ERROR closing decisions", err)
+ logger.Errorf("closing decisions %s", err)
}
rescheduleChannel <- JobIdCloseDecisions
}
@@ -109,7 +109,7 @@ func NewRemindVotersJob() *RemindVotersJob {
func (j *RemindVotersJob) Schedule() {
year, month, day := time.Now().UTC().Date()
nextExecution := time.Date(year, month, day, 0, 0, 0, 0, time.UTC).AddDate(0, 0, 3)
- logger.Println("INFO scheduling RemindVotersJob for", nextExecution)
+ logger.Infof("scheduling RemindVotersJob for %s", nextExecution)
when := nextExecution.Sub(time.Now())
if j.timer != nil {
j.timer.Reset(when)
@@ -126,19 +126,19 @@ func (j *RemindVotersJob) Stop() {
}
func (j *RemindVotersJob) Run() {
- logger.Println("INFO running RemindVotersJob")
+ logger.Infof("running RemindVotersJob")
defer func() { rescheduleChannel <- JobIdRemindVotersJob }()
voters, err := GetReminderVoters()
if err != nil {
- logger.Println("ERROR problem getting voters", err)
+ logger.Errorf("problem getting voters %s", err)
return
}
for _, voter := range *voters {
decisions, err := FindUnvotedDecisionsForVoter(&voter)
if err != nil {
- logger.Println("ERROR problem getting unvoted decisions")
+ logger.Errorf("problem getting unvoted decisions")
return
}
if len(*decisions) > 0 {
diff --git a/models.go b/models.go
index f6dc884..35511ed 100644
--- a/models.go
+++ b/models.go
@@ -126,14 +126,19 @@ WHERE decision=$1 AND voter=$2`,
var db *sqlx.DB
func init() {
+ failed_statements := make([]string, 0)
for _, sqlStatement := range sqlStatements {
var stmt *sqlx.Stmt
stmt, err := db.Preparex(sqlStatement)
if err != nil {
- logger.Fatalf("ERROR parsing statement %s: %s", sqlStatement, err)
+ logger.Criticalf("ERROR parsing statement %s: %s", sqlStatement, err)
+ failed_statements = append(failed_statements, sqlStatement)
}
stmt.Close()
}
+ if len(failed_statements) > 0 {
+ panic(fmt.Sprintf("%d statements failed", len(failed_statements)))
+ }
migrateConf := &goose.DBConf{
MigrationsDir: config.MigrationsPath,
@@ -148,12 +153,14 @@ func init() {
latest, err := goose.GetMostRecentDBVersion(migrateConf.MigrationsDir)
if err != nil {
- logger.Panicln(err)
+ logger.Criticalf("getting the most recent database repository version failed: %s", err)
+ panic(err)
}
err = goose.RunMigrationsOnDb(migrateConf, migrateConf.MigrationsDir, latest, db.DB)
if err != nil {
- logger.Panicln(err)
+ logger.Criticalf("running database migration failed: %s", err)
+ panic(err)
}
}
@@ -278,27 +285,27 @@ type Vote struct {
func (v *Vote) Save() (err error) {
insertVoteStmt, err := db.PrepareNamed(sqlStatements[sqlCreateVote])
if err != nil {
- logger.Println("Error preparing statement:", err)
+ logger.Errorf("preparing statement failed: %s", err)
return
}
defer insertVoteStmt.Close()
_, err = insertVoteStmt.Exec(v)
if err != nil {
- logger.Println("Error saving vote:", err)
+ logger.Errorf("saving vote failed: %s", err)
return
}
getVoteStmt, err := db.Preparex(sqlStatements[sqlLoadVote])
if err != nil {
- logger.Println("Error preparing statement:", err)
+ logger.Errorf("preparing statement failed: %s", err)
return
}
defer getVoteStmt.Close()
err = getVoteStmt.Get(v, v.DecisionId, v.VoterId)
if err != nil {
- logger.Println("Error getting inserted vote:", err)
+ logger.Errorf("getting inserted vote failed: %s", err)
}
return
@@ -341,7 +348,7 @@ type DecisionForDisplay struct {
func FindDecisionForDisplayByTag(tag string) (decision *DecisionForDisplay, err error) {
decisionStmt, err := db.Preparex(sqlStatements[sqlLoadDecisionByTag])
if err != nil {
- logger.Println("Error preparing statement:", err)
+ logger.Errorf("preparing statement failed: %s", err)
return
}
defer decisionStmt.Close()
@@ -352,7 +359,7 @@ func FindDecisionForDisplayByTag(tag string) (decision *DecisionForDisplay, err
decision = nil
err = nil
} else {
- logger.Printf("Error getting motion %s: %v\n", tag, err)
+ logger.Errorf("getting motion %s failed: %v", tag, err)
}
}
decision.VoteSums, err = decision.Decision.VoteSums()
@@ -372,7 +379,7 @@ func FindDecisionsForDisplayOnPage(page int64, unvoted bool, voter *Voter) (deci
decisionsStmt, err = db.Preparex(sqlStatements[sqlLoadDecisions])
}
if err != nil {
- logger.Println("Error preparing statement:", err)
+ logger.Errorf("preparing statement failed: %s", err)
return
}
defer decisionsStmt.Close()
@@ -384,7 +391,7 @@ func FindDecisionsForDisplayOnPage(page int64, unvoted bool, voter *Voter) (deci
rows, err = decisionsStmt.Queryx(page - 1)
}
if err != nil {
- logger.Printf("Error loading motions for page %d: %v\n", page, err)
+ logger.Errorf("loading motions for page %d failed: %v", page, err)
return
}
defer rows.Close()
@@ -392,7 +399,7 @@ func FindDecisionsForDisplayOnPage(page int64, unvoted bool, voter *Voter) (deci
for rows.Next() {
var d DecisionForDisplay
if err = rows.StructScan(&d); err != nil {
- logger.Printf("Error loading motions for page %d: %v\n", page, err)
+ logger.Errorf("loading motions for page %d failed: %v", page, err)
return
}
d.VoteSums, err = d.Decision.VoteSums()
@@ -407,14 +414,14 @@ func FindDecisionsForDisplayOnPage(page int64, unvoted bool, voter *Voter) (deci
func (d *Decision) VoteSums() (sums *VoteSums, err error) {
votesStmt, err := db.Preparex(sqlStatements[sqlLoadVoteCountsForDecision])
if err != nil {
- logger.Println("Error preparing statement:", err)
+ logger.Errorf("preparing statement failed: %s", err)
return
}
defer votesStmt.Close()
voteRows, err := votesStmt.Queryx(d.Id)
if err != nil {
- logger.Printf("Error fetching vote sums for motion %s: %v\n", d.Tag, err)
+ logger.Errorf("fetching vote sums for motion %s failed: %v", d.Tag, err)
return
}
defer voteRows.Close()
@@ -424,7 +431,7 @@ func (d *Decision) VoteSums() (sums *VoteSums, err error) {
var vote VoteChoice
var count int
if err = voteRows.Scan(&vote, &count); err != nil {
- logger.Printf("Error fetching vote sums for motion %s: %v\n", d.Tag, err)
+ logger.Errorf("fetching vote sums for motion %s failed: %v", d.Tag, err)
return
}
switch vote {
@@ -442,13 +449,13 @@ func (d *Decision) VoteSums() (sums *VoteSums, err error) {
func (d *DecisionForDisplay) LoadVotes() (err error) {
votesStmt, err := db.Preparex(sqlStatements[sqlLoadVotesForDecision])
if err != nil {
- logger.Println("Error preparing statement:", err)
+ logger.Errorf("preparing statement failed: %s", err)
return
}
defer votesStmt.Close()
err = votesStmt.Select(&d.Votes, d.Id)
if err != nil {
- logger.Printf("Error selecting votes for motion %s: %v\n", d.Tag, err)
+ logger.Errorf("selecting votes for motion %s failed: %v", d.Tag, err)
}
return
}
@@ -461,18 +468,18 @@ func (d *Decision) OlderExists(unvoted bool, voter *Voter) (result bool, err err
olderStmt, err = db.Preparex(sqlStatements[sqlCountOlderThanDecision])
}
if err != nil {
- logger.Println("Error preparing statement:", err)
+ logger.Errorf("preparing statement failed: %s", err)
return
}
defer olderStmt.Close()
if unvoted && voter != nil {
if err = olderStmt.Get(&result, d.Proposed, voter.Id); err != nil {
- logger.Printf("Error finding older motions than %s: %v\n", d.Tag, err)
+ logger.Errorf("finding older motions than %s failed: %v", d.Tag, err)
}
} else {
if err = olderStmt.Get(&result, d.Proposed); err != nil {
- logger.Printf("Error finding older motions than %s: %v\n", d.Tag, err)
+ logger.Errorf("finding older motions than %s failed: %v", d.Tag, err)
}
}
@@ -482,34 +489,34 @@ func (d *Decision) OlderExists(unvoted bool, voter *Voter) (result bool, err err
func (d *Decision) Create() (err error) {
insertDecisionStmt, err := db.PrepareNamed(sqlStatements[sqlCreateDecision])
if err != nil {
- logger.Println("Error preparing statement:", err)
+ logger.Errorf("preparing statement failed: %s", err)
return
}
defer insertDecisionStmt.Close()
result, err := insertDecisionStmt.Exec(d)
if err != nil {
- logger.Println("Error creating motion:", err)
+ logger.Errorf("creating motion failed: %s", err)
return
}
lastInsertId, err := result.LastInsertId()
if err != nil {
- logger.Println("Error getting id of inserted motion:", err)
+ logger.Errorf("getting id of inserted motion failed: %s", err)
return
}
rescheduleChannel <- JobIdCloseDecisions
getDecisionStmt, err := db.Preparex(sqlStatements[sqlLoadDecisionById])
if err != nil {
- logger.Println("Error preparing statement:", err)
+ logger.Errorf("preparing statement failed: %s", err)
return
}
defer getDecisionStmt.Close()
err = getDecisionStmt.Get(d, lastInsertId)
if err != nil {
- logger.Println("Error getting inserted motion:", err)
+ logger.Errorf("getting inserted motion failed: %s", err)
}
return
@@ -518,14 +525,14 @@ func (d *Decision) Create() (err error) {
func (d *Decision) LoadWithId() (err error) {
getDecisionStmt, err := db.Preparex(sqlStatements[sqlLoadDecisionById])
if err != nil {
- logger.Println("Error preparing statement:", err)
+ logger.Errorf("preparing statement failed: %s", err)
return
}
defer getDecisionStmt.Close()
err = getDecisionStmt.Get(d, d.Id)
if err != nil {
- logger.Println("Error loading updated motion:", err)
+ logger.Errorf("loading updated motion failed: %s", err)
}
return
@@ -534,22 +541,22 @@ func (d *Decision) LoadWithId() (err error) {
func (d *Decision) Update() (err error) {
updateDecisionStmt, err := db.PrepareNamed(sqlStatements[sqlUpdateDecision])
if err != nil {
- logger.Println("Error preparing statement:", err)
+ logger.Errorf("preparing statement failed: %s", err)
return
}
defer updateDecisionStmt.Close()
result, err := updateDecisionStmt.Exec(d)
if err != nil {
- logger.Println("Error updating motion:", err)
+ logger.Errorf("updating motion failed: %s", err)
return
}
affectedRows, err := result.RowsAffected()
if err != nil {
- logger.Print("Problem determining the affected rows")
+ logger.Errorf("Problem determining the affected rows")
return
} else if affectedRows != 1 {
- logger.Printf("WARNING wrong number of affected rows: %d (1 expected)\n", affectedRows)
+ logger.Warningf("wrong number of affected rows: %d (1 expected)", affectedRows)
}
rescheduleChannel <- JobIdCloseDecisions
@@ -560,22 +567,22 @@ func (d *Decision) Update() (err error) {
func (d *Decision) UpdateStatus() (err error) {
updateStatusStmt, err := db.PrepareNamed(sqlStatements[sqlUpdateDecisionStatus])
if err != nil {
- logger.Println("Error preparing statement:", err)
+ logger.Errorf("preparing statement failed: %s", err)
return
}
defer updateStatusStmt.Close()
result, err := updateStatusStmt.Exec(d)
if err != nil {
- logger.Println("Error setting motion status:", err)
+ logger.Errorf("setting motion status failed: %s", err)
return
}
affectedRows, err := result.RowsAffected()
if err != nil {
- logger.Print("Problem determining the affected rows")
+ logger.Errorf("determining the affected rows failed: %s", err)
return
} else if affectedRows != 1 {
- logger.Printf("WARNING wrong number of affected rows: %d (1 expected)\n", affectedRows)
+ logger.Warningf("wrong number of affected rows: %d (1 expected)", affectedRows)
}
rescheduleChannel <- JobIdCloseDecisions
@@ -590,7 +597,7 @@ func (d *Decision) String() string {
func FindVoterByAddress(emailAddress string) (voter *Voter, err error) {
findVoterStmt, err := db.Preparex(sqlStatements[sqlLoadEnabledVoterByEmail])
if err != nil {
- logger.Println("Error preparing statement:", err)
+ logger.Errorf("preparing statement failed: %s", err)
return
}
defer findVoterStmt.Close()
@@ -598,7 +605,7 @@ func FindVoterByAddress(emailAddress string) (voter *Voter, err error) {
voter = &Voter{}
if err = findVoterStmt.Get(voter, emailAddress); err != nil {
if err != sql.ErrNoRows {
- logger.Printf("Error getting voter for address %s: %v\n", emailAddress, err)
+ logger.Errorf("getting voter for address %s failed: %v", emailAddress, err)
} else {
err = nil
voter = nil
@@ -613,7 +620,7 @@ func (d *Decision) Close() (err error) {
voteSums, err := d.VoteSums()
if err != nil {
- logger.Println("Error getting vote sums")
+ logger.Errorf("getting vote sums failed: %s", err)
return
}
votes := voteSums.VoteCount()
@@ -631,22 +638,22 @@ func (d *Decision) Close() (err error) {
closeDecisionStmt, err := db.PrepareNamed(sqlStatements[sqlUpdateDecisionStatus])
if err != nil {
- logger.Println("Error preparing statement:", err)
+ logger.Errorf("preparing statement failed: %s", err)
return
}
defer closeDecisionStmt.Close()
result, err := closeDecisionStmt.Exec(d)
if err != nil {
- logger.Println("Error closing vote:", err)
+ logger.Errorf("closing vote failed: %s", err)
return
}
affectedRows, err := result.RowsAffected()
if err != nil {
- logger.Println("Error getting affected rows:", err)
+ logger.Errorf("getting affected rows failed: %s", err)
}
if affectedRows != 1 {
- logger.Printf("WARNING wrong number of affected rows: %d (1 expected)\n", affectedRows)
+ logger.Warningf("wrong number of affected rows: %d (1 expected)", affectedRows)
}
NotifyMailChannel <- NewNotificationClosedDecision(d, voteSums)
@@ -657,7 +664,7 @@ func (d *Decision) Close() (err error) {
func CloseDecisions() (err error) {
getClosableDecisionsStmt, err := db.PrepareNamed(sqlStatements[sqlSelectClosableDecisions])
if err != nil {
- logger.Println("Error preparing statement:", err)
+ logger.Errorf("preparing statement failed: %s", err)
return
}
defer getClosableDecisionsStmt.Close()
@@ -665,14 +672,14 @@ func CloseDecisions() (err error) {
decisions := make([]*Decision, 0)
rows, err := getClosableDecisionsStmt.Queryx(struct{ Now time.Time }{time.Now().UTC()})
if err != nil {
- logger.Println("Error fetching closable decisions", err)
+ logger.Errorf("fetching closable decisions failed: %s", err)
return
}
defer rows.Close()
for rows.Next() {
decision := &Decision{}
if err = rows.StructScan(decision); err != nil {
- logger.Println("Error scanning row", err)
+ logger.Errorf("scanning row failed: %s", err)
return
}
decisions = append(decisions, decision)
@@ -680,9 +687,9 @@ func CloseDecisions() (err error) {
rows.Close()
for _, decision := range decisions {
- logger.Println("DEBUG found closable decision", decision)
+ logger.Debugf("found closable decision %s", decision.Tag)
if err = decision.Close(); err != nil {
- logger.Printf("Error closing decision %s: %s\n", decision, err)
+ logger.Errorf("closing decision %s failed: %s", decision.Tag, err)
return
}
}
@@ -693,7 +700,7 @@ func CloseDecisions() (err error) {
func GetNextPendingDecisionDue() (due *time.Time, err error) {
getNextPendingDecisionDueStmt, err := db.Preparex(sqlStatements[sqlGetNextPendingDecisionDue])
if err != nil {
- logger.Println("Error preparing statement:", err)
+ logger.Errorf("preparing statement failed: %s", err)
return
}
defer getNextPendingDecisionDueStmt.Close()
@@ -703,10 +710,10 @@ func GetNextPendingDecisionDue() (due *time.Time, err error) {
var dueTimestamp time.Time
if err = row.Scan(&dueTimestamp); err != nil {
if err == sql.ErrNoRows {
- logger.Println("DEBUG No pending decisions")
+ logger.Debugf("No pending decisions")
return nil, nil
}
- logger.Println("Error parsing result", err)
+ logger.Errorf("parsing result failed: %s", err)
return
}
due = &dueTimestamp
@@ -717,7 +724,7 @@ func GetNextPendingDecisionDue() (due *time.Time, err error) {
func GetReminderVoters() (voters *[]Voter, err error) {
getReminderVotersStmt, err := db.Preparex(sqlStatements[sqlGetReminderVoters])
if err != nil {
- logger.Println("Error preparing statement:", err)
+ logger.Errorf("preparing statement failed: %s", err)
return
}
defer getReminderVotersStmt.Close()
@@ -725,7 +732,7 @@ func GetReminderVoters() (voters *[]Voter, err error) {
voterSlice := make([]Voter, 0)
if err = getReminderVotersStmt.Select(&voterSlice); err != nil {
- logger.Println("Error getting voters:", err)
+ logger.Errorf("getting voters failed: %s", err)
return
}
voters = &voterSlice
@@ -736,7 +743,7 @@ func GetReminderVoters() (voters *[]Voter, err error) {
func FindUnvotedDecisionsForVoter(voter *Voter) (decisions *[]Decision, err error) {
findUnvotedDecisionsForVoterStmt, err := db.Preparex(sqlStatements[sqlFindUnvotedDecisionsForVoter])
if err != nil {
- logger.Println("Error preparing statement:", err)
+ logger.Errorf("preparing statement failed: %s", err)
return
}
defer findUnvotedDecisionsForVoterStmt.Close()
@@ -744,7 +751,7 @@ func FindUnvotedDecisionsForVoter(voter *Voter) (decisions *[]Decision, err erro
decisionsSlice := make([]Decision, 0)
if err = findUnvotedDecisionsForVoterStmt.Select(&decisionsSlice, voter.Id); err != nil {
- logger.Println("Error getting unvoted decisions:", err)
+ logger.Errorf("getting unvoted decisions failed: %s", err)
return
}
decisions = &decisionsSlice
@@ -755,14 +762,14 @@ func FindUnvotedDecisionsForVoter(voter *Voter) (decisions *[]Decision, err erro
func GetVoterById(id int64) (voter *Voter, err error) {
getVoterByIdStmt, err := db.Preparex(sqlStatements[sqlGetEnabledVoterById])
if err != nil {
- logger.Println("Error preparing statement:", err)
+ logger.Errorf("preparing statement failed: %s", err)
return
}
defer getVoterByIdStmt.Close()
voter = &Voter{}
if err = getVoterByIdStmt.Get(voter, id); err != nil {
- logger.Println("Error getting voter:", err)
+ logger.Errorf("getting voter failed: %s", err)
return
}
@@ -772,7 +779,7 @@ func GetVoterById(id int64) (voter *Voter, err error) {
func GetVotersForProxy(proxy *Voter, decision *Decision) (voters *[]Voter, err error) {
getVotersForProxyStmt, err := db.Preparex(sqlStatements[sqlGetVotersForProxy])
if err != nil {
- logger.Println("Error preparing statement:", err)
+ logger.Errorf("preparing statement failed: %s", err)
return
}
defer getVotersForProxyStmt.Close()
@@ -780,7 +787,7 @@ func GetVotersForProxy(proxy *Voter, decision *Decision) (voters *[]Voter, err e
votersSlice := make([]Voter, 0)
if err = getVotersForProxyStmt.Select(&votersSlice, proxy.Id, decision.Id); err != nil {
- logger.Println("Error getting voters for proxy:", err)
+ logger.Errorf("Error getting voters for proxy failed: %s", err)
return
}
voters = &votersSlice
diff --git a/notifications.go b/notifications.go
index aa8c7a8..5061a2a 100644
--- a/notifications.go
+++ b/notifications.go
@@ -34,14 +34,14 @@ type NotificationMail interface {
var NotifyMailChannel = make(chan NotificationMail, 1)
func MailNotifier(quitMailNotifier chan int) {
- logger.Println("Launched mail notifier")
+ logger.Infof("Launched mail notifier")
for {
select {
case notification := <-NotifyMailChannel:
content := notification.GetNotificationContent()
mailText, err := buildMail(content.template, content.data)
if err != nil {
- logger.Println("ERROR building mail:", err)
+ logger.Errorf("building mail failed: %s", err)
continue
}
@@ -58,10 +58,10 @@ func MailNotifier(quitMailNotifier chan int) {
d := gomail.NewDialer(config.MailServer.Host, config.MailServer.Port, "", "")
if err := d.DialAndSend(m); err != nil {
- logger.Println("ERROR sending mail:", err)
+ logger.Errorf("sending mail failed: %s", err)
}
case <-quitMailNotifier:
- fmt.Println("Ending mail notifier")
+ logger.Infof("Ending mail notifier")
return
}
}