Skip to content

Commit

Permalink
refactor: gin logger middleware
Browse files Browse the repository at this point in the history
  • Loading branch information
labasubagia committed Sep 15, 2023
1 parent af9a426 commit f96bfcd
Show file tree
Hide file tree
Showing 8 changed files with 24 additions and 15 deletions.
1 change: 1 addition & 0 deletions .env.example
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,6 @@ ENVIRONMENT=development # production
POSTGRES_SOURCE=postgres://postgres:postgres@0.0.0.0:5432/realworld?sslmode=disable
POSTGRES_MIGRATION_URL=file://internal/adapter/repository/sql/db/migration
MONGO_SOURCE=mongodb://root:root@0.0.0.0:27017/realworld?authSource=admin
LOG_TYPE=zerolog
HTTP_SERVER_PORT=5000
TOKEN_SYMMETRIC_KEY=12345678901234567890123456789012
1 change: 1 addition & 0 deletions .env.test
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,6 @@ ENVIRONMENT=development
POSTGRES_SOURCE=postgres://postgres:postgres@0.0.0.0:5432/realworld?sslmode=disable
POSTGRES_MIGRATION_URL=file://../../../internal/adapter/repository/sql/db/migration
MONGO_SOURCE=mongodb://root:root@0.0.0.0:27017/realworld?authSource=admin
LOG_TYPE=zerolog
HTTP_SERVER_PORT=5000
TOKEN_SYMMETRIC_KEY=12345678901234567890123456789012
1 change: 1 addition & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
"dgrijalva",
"Favorited",
"gonic",
"interactor",
"jackc",
"labasubagia",
"mapstructure",
Expand Down
15 changes: 4 additions & 11 deletions cmd/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package cmd

import (
"fmt"
"os"
"strings"

"github.com/labasubagia/realworld-backend/internal/adapter/handler/restful"
Expand Down Expand Up @@ -47,17 +46,11 @@ var serverCmd = &cobra.Command{

// logger
logType, err := cmd.Flags().GetString("log")
if err != nil {
fmt.Fprintf(os.Stderr, "failed get log type flag: %s", err)
os.Exit(1)
}
newLogger, ok := logger.FnNewMap[logType]
if !ok {
logType = logger.DefaultType
newLogger = logger.NewLogger
if err == nil {
config.LogType = logType
}
logger := newLogger(config)
logger.Info().Msgf("use logger %s", logType)
logger := logger.NewLogger(config)
logger.Info().Msgf("use logger %s", config.LogType)

// db
dbType, err := cmd.Flags().GetString("database")
Expand Down
10 changes: 7 additions & 3 deletions internal/adapter/handler/restful/logger.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@ import (
"time"

"github.com/gin-gonic/gin"
"github.com/google/uuid"
"github.com/labasubagia/realworld-backend/internal/adapter/logger"
"github.com/labasubagia/realworld-backend/internal/core/domain"
"github.com/labasubagia/realworld-backend/internal/core/port"
)

Expand All @@ -16,13 +17,16 @@ func (s *Server) Logger() gin.HandlerFunc {
// request id
reqID := c.GetHeader("x-request-id")
if reqID == "" {
reqID = uuid.NewString()
reqID = domain.NewID().String()
}

// make logger and sub-logger
logger := s.logger.Field("request_id", reqID).Logger()
// ? make unique instance for each handler/interactor request
logger := logger.NewLogger(s.config).Field("request_id", reqID).Logger()
c.Set(port.SubLoggerCtxKey, logger)

// logger := s.logger.Field("request_id", reqID).Logger()

// process request
startTime := time.Now()
c.Next()
Expand Down
4 changes: 4 additions & 0 deletions internal/adapter/logger/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,5 +25,9 @@ func Keys() (keys []string) {
}

func NewLogger(config util.Config) port.Logger {
new, ok := FnNewMap[config.LogType]
if ok {
return new(config)
}
return FnNewMap[DefaultType](config)
}
4 changes: 4 additions & 0 deletions internal/core/domain/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@ import (

type ID string

func (id ID) String() string {
return string(id)
}

func NewID() ID {
return ID(ulid.Make().String())
}
Expand Down
3 changes: 2 additions & 1 deletion internal/core/util/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@ type Config struct {

MongoSource string `mapstructure:"MONGO_SOURCE"`

HTTPServerPort int `mapstructure:"HTTP_SERVER_PORT"`
HTTPServerPort int `mapstructure:"HTTP_SERVER_PORT"`
LogType string `mapstructure:"LOG_TYPE"`

TokenSymmetricKey string `mapstructure:"TOKEN_SYMMETRIC_KEY"`

Expand Down

0 comments on commit f96bfcd

Please sign in to comment.