Skip to content
This repository has been archived by the owner on Aug 29, 2024. It is now read-only.

Commit

Permalink
Use Zerolog
Browse files Browse the repository at this point in the history
  • Loading branch information
JenswBE committed Jul 30, 2021
1 parent c3f2aae commit 40d0c98
Show file tree
Hide file tree
Showing 6 changed files with 24 additions and 15 deletions.
4 changes: 2 additions & 2 deletions api/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@ package main
import (
"errors"
"fmt"
"log"
"strconv"
"strings"

"github.com/JenswBE/go-commerce/utils/imageproxy"
"github.com/rs/zerolog/log"
"github.com/spf13/viper"
)

Expand Down Expand Up @@ -61,7 +61,7 @@ func parseConfig() (*Config, error) {
if _, ok := err.(viper.ConfigFileNotFoundError); !ok {
return nil, fmt.Errorf("failed reading config file: %w", err)
}
log.Printf("Warning: No config file found, expecting configuration through ENV variables %s", err)
log.Warn().Err(err).Msg("No config file found, expecting configuration through ENV variables")
}

// Bind ENV variables
Expand Down
19 changes: 13 additions & 6 deletions api/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,9 @@ package main

import (
"fmt"
"log"
"os"
"strconv"
"time"

"github.com/JenswBE/go-commerce/api/handler"
"github.com/JenswBE/go-commerce/api/presenter"
Expand All @@ -14,37 +15,43 @@ import (
"github.com/JenswBE/go-commerce/utils/shortid"
"github.com/gin-contrib/cors"
"github.com/gin-gonic/gin"
"github.com/rs/zerolog"
"github.com/rs/zerolog/log"
"gorm.io/driver/postgres"
"gorm.io/gorm"
)

func main() {
// Setup logging
output := zerolog.ConsoleWriter{Out: os.Stdout, TimeFormat: time.RFC3339}
log.Logger = log.Output(output)

// Parse config
config, err := parseConfig()
if err != nil {
log.Fatalf("Failed to parse config: %s", err)
log.Fatal().Err(err).Msg("Failed to parse config")
}

// DB
dsn := buildDSN(config)
productDB, err := gorm.Open(postgres.Open(dsn), &gorm.Config{})
if err != nil {
log.Fatalf("Failed to connect to db: %s", err.Error())
log.Fatal().Err(err).Msg("Failed to connect to db")
}

// Services
productDatabase := productpg.NewProductPostgres(productDB)
imageStorage, err := getStorageRepo(config.Storage.Images)
if err != nil {
log.Fatalf("Failed to create image storage repository: %s", err.Error())
log.Fatal().Err(err).Msg("Failed to create image storage repository")
}
allowedImageConfigs, err := parseAllowedImageConfigs(config.ImageProxy.AllowedConfigs)
if err != nil {
log.Fatalf("Failed to parse allowed image configs: %s", err.Error())
log.Fatal().Err(err).Msg("Failed to parse allowed image configs")
}
imageProxyService, err := imageproxy.NewImgProxyService(config.ImageProxy.BaseURL, config.ImageProxy.Key, config.ImageProxy.Salt, allowedImageConfigs)
if err != nil {
log.Fatalf("Failed to create image proxy service: %s", err.Error())
log.Fatal().Err(err).Msg("Failed to create image proxy service")
}
productService := product.NewService(productDatabase, imageProxyService, imageStorage)
shortIDService := shortid.NewBase58Service()
Expand Down
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ require (
github.com/gin-contrib/cors v1.3.1
github.com/gin-gonic/gin v1.7.1
github.com/google/uuid v1.2.0
github.com/rs/zerolog v1.23.0
github.com/spf13/viper v1.8.1
github.com/stretchr/testify v1.7.0
gorm.io/driver/postgres v1.1.0
Expand Down
5 changes: 4 additions & 1 deletion go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -430,8 +430,9 @@ github.com/performancecopilot/speed v3.0.0+incompatible/go.mod h1:/CLtqpZ5gBg1M9
github.com/pierrec/lz4 v1.0.2-0.20190131084431-473cd7ce01a1/go.mod h1:3/3N9NVKO0jef7pBehbT1qWhCMrIgbYNnFAZCqQ5LRc=
github.com/pierrec/lz4 v2.0.5+incompatible/go.mod h1:pdkljMzZIN41W+lC3N2tnIh5sFi+IEE17M5jbnwPHcY=
github.com/pkg/errors v0.8.0/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
github.com/pkg/errors v0.8.1 h1:iURUrRGxPUNPdy5/HRSm+Yj6okJ6UtLINN0Q9M4+h3I=
github.com/pkg/errors v0.8.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
github.com/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4=
github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
github.com/pkg/profile v1.2.1/go.mod h1:hJw3o1OdXxsrSjjVksARp5W95eeEaEfptyVZyv6JUPA=
github.com/pkg/sftp v1.10.1/go.mod h1:lYOWFsE0bwd1+KfKJaKeuokY15vzFx25BLbzYYoAxZI=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
Expand Down Expand Up @@ -460,6 +461,8 @@ github.com/rogpeppe/go-internal v1.3.0/go.mod h1:M8bDsm7K2OlrFYOpmOWEs/qY81heoFR
github.com/rs/xid v1.2.1/go.mod h1:+uKXf+4Djp6Md1KODXJxgGQPKngRmWyn10oCKFzNHOQ=
github.com/rs/zerolog v1.13.0/go.mod h1:YbFCdg8HfsridGWAh22vktObvhZbQsZXe4/zB0OKkWU=
github.com/rs/zerolog v1.15.0/go.mod h1:xYTKnLHcpfU2225ny5qZjxnj9NvkumZYjJHlAThCjNc=
github.com/rs/zerolog v1.23.0 h1:UskrK+saS9P9Y789yNNulYKdARjPZuS35B8gJF2x60g=
github.com/rs/zerolog v1.23.0/go.mod h1:6c7hFfxPOy7TacJc4Fcdi24/J0NKYGzjG8FWRI916Qo=
github.com/russross/blackfriday/v2 v2.0.1/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM=
github.com/ryanuber/columnize v0.0.0-20160712163229-9b3edd62028f/go.mod h1:sm1tb6uqfes/u+d4ooFouqFdy9/2g9QGwK3SQygK0Ts=
github.com/samuel/go-zookeeper v0.0.0-20190923202752-2cc03de413da/go.mod h1:gi+0XIa01GRL2eRQVjQkKGqKF3SF9vZR/HnPullcV2E=
Expand Down
5 changes: 2 additions & 3 deletions usecases/product/service_category.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
package product

import (
"log"

"github.com/JenswBE/go-commerce/entities"
"github.com/JenswBE/go-commerce/utils/imageproxy"
"github.com/rs/zerolog/log"
)

// GetCategory fetches a single category by ID
Expand Down Expand Up @@ -89,7 +88,7 @@ func (s *Service) UpsertCategoryImage(categoryID entities.ID, imageName string,
if oldImage != nil {
err = s.deleteImage(oldImage)
if err != nil {
log.Printf(`Failed to delete old image for category: %s`, err.Error())
log.Warn().Err(err).Msg("Failed to delete old image for category")
}
}

Expand Down
5 changes: 2 additions & 3 deletions usecases/product/service_manufacturer.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
package product

import (
"log"

"github.com/JenswBE/go-commerce/entities"
"github.com/JenswBE/go-commerce/utils/imageproxy"
"github.com/rs/zerolog/log"
)

// GetManufacturer fetches a single manufacturer by ID
Expand Down Expand Up @@ -89,7 +88,7 @@ func (s *Service) UpsertManufacturerImage(manufacturerID entities.ID, imageName
if oldImage != nil {
err = s.deleteImage(oldImage)
if err != nil {
log.Printf(`Failed to delete old image for category: %s`, err.Error())
log.Warn().Err(err).Msg("Failed to delete old image for category")
}
}

Expand Down

0 comments on commit 40d0c98

Please sign in to comment.