Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore:unified logging #200

Merged
merged 1 commit into from
Sep 12, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
129 changes: 73 additions & 56 deletions client/localServer/localTestClient.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ func main() {

conn, err := grpc.Dial(localAddress, grpc.WithTransportCredentials(insecure.NewCredentials()))
if err != nil {
log.Info(err)
log.WithError(err).Error("could not dial localAddress")
}
c := pb.NewCampusClient(conn)
ctx, cancel := context.WithTimeout(context.Background(), time.Second*5)
Expand All @@ -50,8 +50,7 @@ func canteenHeadCount(c pb.CampusClient, ctx context.Context) {
if err != nil {
log.Error(err)
} else {
log.Info("Canteen HeadCount data request successful.")
log.Info(res)
log.WithField("res", res).Info("Canteen HeadCount data request successful.")
}
}

Expand Down Expand Up @@ -80,43 +79,52 @@ func queryDish(cafeteria string, dish string, c pb.CampusClient, ctx context.Con
})

if err != nil {
log.Info(err)
log.WithError(err).Info("failed to query dish")
} else {
log.Info("Result: ")
log.Info("\tavg : ", res.Avg)
log.Info("\tmin ", res.Min)
log.Info("\tmax ", res.Max)
log.Info("\tstd ", res.Std)
log.Info("Number of individual Ratings", len(res.Rating))
fields := log.Fields{
"avg": res.Avg,
"min": res.Min,
"max": res.Max,
"std": res.Std,
"individualRatings": len(res.Rating),
}
log.WithFields(fields).Info("succeeded to query dish")
path := fmt.Sprintf("%s%d%s", "./testImages/dishes/", time.Now().Unix(), "/")
for _, v := range res.Rating {
log.Info("\nRating: ", v.Points)
log.Info("Comment ", v.Comment)
log.Info("Number of Tag Ratings : ", len(v.RatingTags))
log.Info("Timestamp: ", v.Visited)
log.Info("ImageLength:", len(v.Image))
fields := log.Fields{
"Rating": v.Points,
"Comment": v.Comment,
"Number of Tag Ratings": len(v.RatingTags),
"Timestamp": v.Visited,
"ImageLength": len(v.Image),
}
log.WithFields(fields).Info("storing image")
if imageShouldBeStored {
_, err := storeImage(path, v.Image)
if err != nil {
log.Info("image was not saved successfully")
log.WithError(err).Error("image was not saved successfully")
}
}
}
log.Info("Rating Tags: ")
for _, v := range res.RatingTags {
log.Info("TagId: ", v.TagId)
log.Info("\tavg: ", v.Avg)
log.Info("\tmin: ", v.Min)
log.Info("\tmax: ", v.Max)
log.Info("\tstd: ", v.Std)
fields := log.Fields{
"avg": v.Avg,
"min": v.Min,
"max": v.Max,
"std": v.Std,
}
log.WithFields(fields).Info(v.TagId)
}
log.Info("nameTags: ")
for _, v := range res.NameTags {
log.Info("TagId: ", v.TagId)
log.Info("\tavg: ", v.Avg)
log.Info("\tmin: ", v.Min)
log.Info("\tmax: ", v.Max)
log.Info("\tstd: ", v.Std)
fields := log.Fields{
"avg": v.Avg,
"min": v.Min,
"max": v.Max,
"std": v.Std,
}
log.WithFields(fields).Info(v.TagId)
}
}
}
Expand All @@ -130,34 +138,42 @@ func queryCafeteria(s string, c pb.CampusClient, ctx context.Context, imageShoul
})

if err != nil {
log.Info(err)
log.WithError(err).Error("failed to query cafeteria")
} else {
log.Info("Result: ")
log.Info("avg: ", res.Avg)
log.Info("min", res.Min)
log.Info("max", res.Max)
log.Info("Number of individual Ratings", len(res.Rating))
fields := log.Fields{
"avg": res.Avg,
"min": res.Min,
"max": res.Max,
"std": res.Std,
"Number of individual Ratings": len(res.Rating),
}
log.WithFields(fields).Info("succeeded to query cafeteria")
path := fmt.Sprintf("%s%d%s", "./testImages/cafeteria/", time.Now().Unix(), "/")
for _, v := range res.Rating {
log.Info("\nRating: ", v.Points)
log.Info("Comment ", v.Comment)
log.Info("Number of Tag Ratings: ", len(v.RatingTags))
log.Info("Timestamp: ", v.Visited)
log.Info("ImageLength:", len(v.Image))
for i, v := range res.Rating {
fields := log.Fields{
"Rating": v.Points,
"Comment": v.Comment,
"Number of Tag Ratings": len(v.RatingTags),
"Timestamp": v.Visited,
"ImageLength": len(v.Image),
}
log.WithFields(fields).Infof("Rating %d", i)
if imageShouldBeStored {
_, err := storeImage(path, v.Image)
if err != nil {
log.Info("image was not saved successfully")
log.WithError(err).Error("image was not saved successfully")
}
}
}

for _, v := range res.RatingTags {
log.Info("\nTagId: ", v.TagId)
log.Info("avg: ", v.Avg)
log.Info("min", v.Min)
log.Info("max", v.Max)
log.Info("std", v.Std)
fields := log.Fields{
"avg": v.Avg,
"min": v.Min,
"max": v.Max,
"std": v.Std,
}
log.WithFields(fields).Info(v.TagId)
}
}
}
Expand All @@ -182,9 +198,9 @@ func generateCafeteriaRating(c pb.CampusClient, ctx context.Context, cafeteria s
})

if err != nil {
log.Info(err)
log.WithError(err).Error("could not store new Cafeteria Rating")
} else {
log.Info("Request successfully: Cafeteria Rating should be stored")
log.Info("Cafeteria Rating successfully be stored")
}
}

Expand Down Expand Up @@ -213,9 +229,9 @@ func generateDishRating(c pb.CampusClient, ctx context.Context, cafeteria string
})

if err != nil {
log.Info(err)
log.WithError(err).Error("failed to store dish rating")
} else {
log.Info("Request successfully: Dish Rating should be stored")
log.Info("Dish Rating successfully stored")
}
}

Expand All @@ -231,28 +247,29 @@ func getImageToBytes(path string) []byte {
defer func(file *os.File) {
err := file.Close()
if err != nil {
log.Info("Request successfully: Dish Rating should be stored")
log.WithError(err).Error("could not close file")
}
}(file)

fileInfo, _ := file.Stat()
var size = fileInfo.Size()
byteArray := make([]byte, size)

buffer := bufio.NewReader(file)
_, err = buffer.Read(byteArray)
n, err := bufio.NewReader(file).Read(byteArray)
fields := log.Fields{"readBytes": n, "len(byteArray)": len(byteArray)}
if err != nil {
log.Info("Unable to read the byteArray")
log.WithError(err).Error("Unable to read the byteArray")
} else {
log.WithFields(fields).Info("read image to byteArray")
}
log.Info("Length of the image as bytes: ", len(byteArray))
return byteArray
}

func storeImage(path string, i []byte) (string, error) {
if _, err := os.Stat(path); errors.Is(err, os.ErrNotExist) {
err := os.MkdirAll(path, os.ModePerm)
if err != nil {
log.Info(err)
log.WithError(err).Error("could not make dir")
}
}
img, _, _ := image.Decode(bytes.NewReader(i))
Expand All @@ -271,12 +288,12 @@ func storeImage(path string, i []byte) (string, error) {

out, errFile := os.Create(imgPath)
if errFile != nil {
log.Info("Unable to create the new testfile")
log.WithError(errFile).Error("Unable to create the new testfile")
}
defer func(out *os.File) {
err := out.Close()
if err != nil {
log.Info("File was not closed successfully")
log.WithError(err).Error("File was not closed successfully")
}
}(out)
var opts jpeg.Options
Expand Down
23 changes: 14 additions & 9 deletions client/publicServer/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,11 @@ import (
"context"
"crypto/x509"
pb "github.com/TUM-Dev/Campus-Backend/api"
log "github.com/sirupsen/logrus"
"google.golang.org/grpc"
"google.golang.org/grpc/credentials"
"google.golang.org/grpc/metadata"
"google.golang.org/protobuf/types/known/emptypb"
"log"
"time"
)

Expand All @@ -18,16 +18,21 @@ const (

func main() {
// Set up a connection to the server.
log.Println("Connecting...")
log.Info("Connecting...")
pool, _ := x509.SystemCertPool()
// error handling omitted
creds := credentials.NewClientTLSFromCert(pool, "")

conn, err := grpc.Dial(address, grpc.WithTransportCredentials(creds))
if err != nil {
log.Fatalf("did not connect: %v", err)
log.WithError(err).Fatalf("did not connect")
}
defer conn.Close()
defer func(conn *grpc.ClientConn) {
err := conn.Close()
if err != nil {
log.WithError(err).Error("did not close connection")
}
}(conn)
c := pb.NewCampusClient(conn)

ctx, cancel := context.WithTimeout(context.Background(), time.Second*5)
Expand All @@ -37,10 +42,10 @@ func main() {
md := metadata.New(map[string]string{"x-device-id": "grpc-tests"})
ctx = metadata.NewOutgoingContext(ctx, md)

log.Println("Trying to fetch top news")
r, err := c.GetTopNews(ctx, &emptypb.Empty{})
if err != nil {
log.Fatalf("could not greet: %v", err)
log.Info("Trying to fetch top news")
if r, err := c.GetTopNews(ctx, &emptypb.Empty{}); err != nil {
log.WithError(err).Fatal("could not greet")
} else {
log.WithField("topNewsResponse", r.String()).Info("fetched top news successfully")
}
log.Printf("Greeting: %s", r.String())
}
22 changes: 10 additions & 12 deletions server/backend/cafeteriaRatingDBInitializer.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ func addEntriesForCronJob(db *gorm.DB, cronName string, interval int32) {
Error

if err != nil {
log.WithError(err).Errorf("Error while checking if cronjob with name %s already exists in database", cronName)
log.WithError(err).WithField("cronName", cronName).Error("Error while checking if cronjob already exists in database")
} else if count == 0 {
errCreate := db.Model(&model.Crontab{}).
Create(&model.Crontab{
Expand Down Expand Up @@ -78,26 +78,24 @@ func updateNameTagOptions(db *gorm.DB) {
Where("EN LIKE ? AND DE LIKE ?", v.TagNameEnglish, v.TagNameGerman).
Select("DishNameTagOption").
Scan(&parentId)
fields := log.Fields{"en": v.TagNameEnglish, "de": v.TagNameGerman}
if res.Error != nil {
log.WithError(res.Error).Errorf("Unable to load tag with En %s and De %s", v.TagNameEnglish, v.TagNameGerman)
log.WithError(res.Error).WithFields(fields).Error("Unable to load tag")
}
if res.RowsAffected == 0 || res.Error != nil {
parent := model.DishRatingTagOption{
DE: v.TagNameGerman,
EN: v.TagNameEnglish,
}

errCreate := db.Model(&model.DishNameTagOption{}).
Create(&parent).Error
if errCreate != nil {
log.WithError(errCreate).Error("Error while creating tag {}, {}.", v.TagNameGerman, v.TagNameEnglish)
if err := db.Model(&model.DishNameTagOption{}).Create(&parent).Error; err != nil {
log.WithError(err).WithFields(fields).Error("Error while creating tag")
}
parentId = parent.DishRatingTagOption
}

addCanBeIncluded(parentId, db, v)
addNotIncluded(parentId, db, v)

}
}

Expand Down Expand Up @@ -161,20 +159,20 @@ func updateTagTable(path string, db *gorm.DB, tagType modelType) {
insertModel := getTagModel(tagType, db)
for _, v := range tagsDish.MultiLanguageTags {
var count int64

fields := log.Fields{"de": v.TagNameGerman, "en": v.TagNameEnglish}
if tagType == CAFETERIA {
countError := db.Model(&model.CafeteriaRatingTagOption{}).
Where("EN LIKE ? AND DE LIKE ?", v.TagNameEnglish, v.TagNameGerman).
Select("cafeteriaRatingTagOption").Count(&count).Error
if countError != nil {
log.WithError(countError).Errorf("Unable to find cafeteria rating tag with En %s and De %s", v.TagNameGerman, v.TagNameEnglish)
log.WithError(countError).WithFields(fields).Error("Unable to find cafeteria rating tag")
}
} else {
countError := db.Model(&model.DishRatingTagOption{}).
Where("EN LIKE ? AND DE LIKE ?", v.TagNameEnglish, v.TagNameGerman).
Select("dishRatingTagOption").Count(&count).Error
if countError != nil {
log.WithError(countError).Errorf("Unable to find dish rating tag with En %s and De %s", v.TagNameGerman, v.TagNameEnglish)
log.WithError(countError).WithFields(fields).Error("Unable to find dish rating tag")
}
}

Expand All @@ -185,9 +183,9 @@ func updateTagTable(path string, db *gorm.DB, tagType modelType) {
}
createError := insertModel.Create(&element).Error
if createError != nil {
log.WithError(createError).Errorf("Unable to create new can be excluded tag with En %s and De %s", v.TagNameGerman, v.TagNameEnglish)
log.WithError(createError).WithFields(fields).Error("Unable to create new can be excluded tag")
} else {
log.Infof("New Entry with En %s and De %s successfully created.", v.TagNameGerman, v.TagNameEnglish)
log.WithFields(fields).Info("New Entry successfully created")
}
}
}
Expand Down
Loading