diff --git a/src/myaktion/handler/campaign.go b/src/myaktion/handler/campaign.go index 2b2db92ef77760bbb375dd4180cc68033ff4258b..6117d878b55aacea5bfe27e75030d10fc3533e03 100644 --- a/src/myaktion/handler/campaign.go +++ b/src/myaktion/handler/campaign.go @@ -10,19 +10,19 @@ import ( ) func getCampaign(r *http.Request) (*model.Campaign, error) { - var campaign *model.Campaign - err := json.NewDecoder(r.Body).Decode(campaign) + var campaign model.Campaign + err := json.NewDecoder(r.Body).Decode(&campaign) if err != nil { - log.Errorf("Can't serialize request body to campaign struct: %v", err) + log.Errorf("Can't serialize request body to campaign struct: %v %v", err) return nil, err } - return campaign, nil + return &campaign, nil } func GetCampaigns(w http.ResponseWriter, _ *http.Request) { campaigns, err := service.GetCampaigns() if err != nil { - log.Printf("Error calling service GetCampaigns: %v", err) + log.Errorf("Error calling service GetCampaigns: %v", err) http.Error(w, err.Error(), http.StatusInternalServerError) return } @@ -37,7 +37,7 @@ func CreateCampaign(w http.ResponseWriter, r *http.Request) { return } if err := service.CreateCampaign(campaign); err != nil { - log.Printf("Error calling service CreateCampaign: %v", err) + log.Errorf("Error calling service CreateCampaign: %v", err) http.Error(w, err.Error(), http.StatusInternalServerError) return } diff --git a/src/myaktion/handler/health.go b/src/myaktion/handler/health.go index bb62dc721e584bf5bd0959ed81692ba935a289e5..d72bd22969d2b7850eef8f6a799c01bfd70820b4 100644 --- a/src/myaktion/handler/health.go +++ b/src/myaktion/handler/health.go @@ -3,9 +3,12 @@ package handler import ( "io" "net/http" + + log "github.com/sirupsen/logrus" ) func Health(w http.ResponseWriter, r *http.Request) { + log.Info("Health check is successfully called") w.Header().Set("Content-Type", "application/json") io.WriteString(w, `{"alive": true}`) } diff --git a/src/myaktion/main.go b/src/myaktion/main.go index db230eb20e5e3515883db5996ce97749d44ca456..28e090a195a11b845ac475de0506f0534ad0aff9 100644 --- a/src/myaktion/main.go +++ b/src/myaktion/main.go @@ -51,7 +51,7 @@ func main() { }) port := 8000 - log.Printf("Starting MyAktion API server on port %v.\n", port) + log.Infof("Starting MyAktion API server on port %v.\n", port) router := mux.NewRouter() router.HandleFunc("/health", handler.Health).Methods("GET") router.HandleFunc("/campaigns", handler.GetCampaigns).Methods("GET") diff --git a/src/myaktion/service/campaign.go b/src/myaktion/service/campaign.go index f03182dc9917e7e2f1aec418146c68b531dd4244..d4274a3e540c03791574217b71afca91009920d9 100644 --- a/src/myaktion/service/campaign.go +++ b/src/myaktion/service/campaign.go @@ -1,7 +1,7 @@ package service import ( - "log" + log "github.com/sirupsen/logrus" "gitlab.reutlingen-university.de/yesildas/myaktion-go/src/myaktion/model" ) @@ -20,7 +20,8 @@ func GetCampaigns() ([]model.Campaign, error) { for _, campaign := range campaignStore { campaigns = append(campaigns, *campaign) } - log.Printf("Retrieved: %v", campaigns) + log.Infof("Successfully retrieved %d campaigns.", len(campaigns)) + log.Tracef("Retrieved: %v", campaigns) return campaigns, nil } @@ -29,8 +30,8 @@ func CreateCampaign(campaign *model.Campaign) error { campaign.ID = actCampaignId campaignStore[actCampaignId] = campaign actCampaignId += 1 - log.Printf("Successfully stored new campaign with ID %v in database.", campaign.ID) - log.Printf("Stored: %v", campaign) + log.Infof("Successfully stored new campaign with ID %v in database.", campaign.ID) + log.Tracef("Stored: %v", campaign) return nil }