Skip to content

Commit

Permalink
fix(logging): inject the logger into sane places and remove old debug…
Browse files Browse the repository at this point in the history
…gig log lines
  • Loading branch information
willejs committed Aug 20, 2024
1 parent 8cb4b52 commit b945ba2
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 12 deletions.
8 changes: 3 additions & 5 deletions cmd/api/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,13 @@ package main
import (
"context"
"log"
"log/slog"
"net/http"
"os"
"os/signal"
"syscall"
"time"

"golang.org/x/exp/slog"

"github.com/prometheus/client_golang/prometheus/promhttp"
adapterhttp "github.com/willejs/ports-service/internal/adapter/http"
"github.com/willejs/ports-service/internal/adapter/repository"
Expand All @@ -33,19 +32,18 @@ func main() {
defer otelCleanup()

// Initialize MemDB
db, err := memdb.NewMemDB()
db, err := memdb.NewMemDB(logger)
if err != nil {
logger.Error("Failed to initalize memdb", slog.String("component", "main"), slog.Any("error", err))
}

// Create repository, service, controller, and handler
portRepo := repository.NewMemDBPortRepository(db)
portService := app.NewPortService(portRepo)
portController := controller.NewPortController(portService)
portController := controller.NewPortController(logger, portService)
portHandler := adapterhttp.NewPortHandler(portController)

// Load ports data from JSON file
log.Println("Upserting ports from file...")
if err := portController.UpsertPortsFromFile(); err != nil {
logger.Error("Failed to upsert all ports from file", slog.String("component", "main"), slog.Any("error", err))
os.Exit(1)
Expand Down
10 changes: 6 additions & 4 deletions internal/controller/port_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package controller
import (
"encoding/json"
"io/ioutil"
"log"
"log/slog"

"github.com/willejs/ports-service/internal/app"
"github.com/willejs/ports-service/internal/domain/entity"
Expand All @@ -12,11 +12,12 @@ import (
// PortController defines the controller for handling port-related operations.
type PortController struct {
service *app.PortService
logger *slog.Logger
}

// NewPortController creates a new PortController.
func NewPortController(service *app.PortService) *PortController {
return &PortController{service: service}
func NewPortController(logger *slog.Logger, service *app.PortService) *PortController {
return &PortController{service: service, logger: logger}
}

// ListAllPorts retrieves all ports and returns them.
Expand All @@ -28,9 +29,9 @@ func (c *PortController) ListAllPorts() ([]*entity.Port, error) {
func (c *PortController) UpsertPortsFromFile() error {
// Load ports data from JSON file
// I should open the file and get the handle here instead of reading it all into memory
c.logger.Info("Upserting ports from file", slog.String("component", "controller/port_controller"))
data, err := ioutil.ReadFile("../../data/ports.json")
if err != nil {
log.Fatalf("error reading ports file: %v", err)
return err
}

Expand All @@ -45,6 +46,7 @@ func (c *PortController) UpsertPortsFromFile() error {
for key, port := range ports {
// the data sometimes missess the code attribute, instead of any logic to santize or normalise data lets just use the key of the map.
if port.Code == "" {
c.logger.Debug("Port does not have a code attribute, using key as code", slog.String("component", "controller/port_controller"), slog.String("key", key))
// if port does not have a arrtibute code use key as code
port.Code = key
}
Expand Down
3 changes: 1 addition & 2 deletions internal/infrastructure/http/logging_middleware.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
package http

import (
"log/slog"
"net/http"
"time"

"golang.org/x/exp/slog"
)

// LoggingMiddleware logs HTTP requests.
Expand Down
5 changes: 4 additions & 1 deletion internal/infrastructure/memdb/memdb.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
package memdb

import (
"log/slog"

"github.com/hashicorp/go-memdb"
)

// NewMemDB initializes a new MemDB instance.
func NewMemDB() (*memdb.MemDB, error) {
func NewMemDB(logger *slog.Logger) (*memdb.MemDB, error) {
// Define the schema for the Port entity
schema := &memdb.DBSchema{
Tables: map[string]*memdb.TableSchema{
Expand All @@ -23,6 +25,7 @@ func NewMemDB() (*memdb.MemDB, error) {
}

// Create a new database instance
logger.Info("Creating new MemDB instance", slog.String("component", "infrastructure/memdb"))
db, err := memdb.NewMemDB(schema)
if err != nil {
return nil, err
Expand Down

0 comments on commit b945ba2

Please sign in to comment.