Skip to content
This repository has been archived by the owner on Feb 13, 2025. It is now read-only.

Commit

Permalink
feat: added fiber logging via zap
Browse files Browse the repository at this point in the history
  • Loading branch information
ChristianSch committed Dec 14, 2023
1 parent 44f632a commit 8ef1e5e
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 2 deletions.
12 changes: 11 additions & 1 deletion adapters/inbound/fiber_web_server.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,10 @@ package inbound
import (
"fmt"

outboundAdapters "github.com/ChristianSch/Theta/adapters/outbound"
"github.com/ChristianSch/Theta/domain/ports/inbound"
"github.com/ChristianSch/Theta/domain/ports/outbound"
"github.com/gofiber/contrib/fiberzap"
"github.com/gofiber/fiber/v2"
"github.com/gofiber/template/html/v2"
"github.com/gofiber/websocket/v2"
Expand Down Expand Up @@ -33,10 +35,18 @@ func NewFiberWebServer(cfg FiberWebServerConfig, adapters FiberWebServerAdapters
Views: html.New(cfg.TemplatesPath, cfg.TemplatesExtension),
})

zapLogger, ok := adapters.Log.(*outboundAdapters.ZapLogger)
if ok {
server.Use(fiberzap.New(fiberzap.Config{
Logger: zapLogger.GetLogger(),
}))
adapters.Log.Debug("added zap logger middleware")
}

server.Static("/static", cfg.StaticResourcesPath)

server.Use("/ws", func(c *fiber.Ctx) error {
adapters.Log.Debug("handling websocket upgrade requests request")
adapters.Log.Debug("handling websocket upgrade requests request", outbound.LogField{Key: "isUpgrade", Value: websocket.IsWebSocketUpgrade(c)})

// IsWebSocketUpgrade returns true if the client
// requested upgrade to the WebSocket protocol.
Expand Down
4 changes: 4 additions & 0 deletions adapters/outbound/zap_logger.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,3 +61,7 @@ func (z *ZapLogger) Info(msg string, fields ...outbound.LogField) {
func (z *ZapLogger) Error(msg string, fields ...outbound.LogField) {
z.log.Error(msg, z.convertFields(fields...)...)
}

func (z *ZapLogger) GetLogger() *zap.Logger {
return z.log
}
3 changes: 2 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ require (
github.com/mitchellh/copystructure v1.0.0 // indirect
github.com/mitchellh/reflectwalk v1.0.0 // indirect
github.com/pkoukk/tiktoken-go v0.1.2 // indirect
github.com/rivo/uniseg v0.2.0 // indirect
github.com/rivo/uniseg v0.4.3 // indirect
github.com/savsgio/gotils v0.0.0-20230208104028-c358bd845dee // indirect
github.com/shopspring/decimal v1.2.0 // indirect
github.com/spf13/cast v1.3.1 // indirect
Expand All @@ -40,6 +40,7 @@ require (
)

require (
github.com/gofiber/contrib/fiberzap v1.0.2
github.com/gofiber/fiber/v2 v2.51.0
github.com/gofiber/template/html/v2 v2.0.5
github.com/gofiber/websocket/v2 v2.2.1
Expand Down
4 changes: 4 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ github.com/dlclark/regexp2 v1.8.1 h1:6Lcdwya6GjPUNsBct8Lg/yRPwMhABj269AAzdGSiR+0
github.com/dlclark/regexp2 v1.8.1/go.mod h1:DHkYz0B9wPfa6wondMfaivmHpzrQ3v9q8cnmRbL6yW8=
github.com/fasthttp/websocket v1.5.3 h1:TPpQuLwJYfd4LJPXvHDYPMFWbLjsT91n3GpWtCQtdek=
github.com/fasthttp/websocket v1.5.3/go.mod h1:46gg/UBmTU1kUaTcwQXpUxtRwG2PvIZYeA8oL6vF3Fs=
github.com/gofiber/contrib/fiberzap v1.0.2 h1:EQwhggtszVfIdBeXxN9Xrmld71es34Ufs+ef8VMqZxc=
github.com/gofiber/contrib/fiberzap v1.0.2/go.mod h1:jGO8BHU4gRI9U0JtM6zj2CIhYfgVmW5JxziN8NTgVwE=
github.com/gofiber/fiber/v2 v2.51.0 h1:JNACcZy5e2tGApWB2QrRpenTWn0fq0hkFm6k0C86gKQ=
github.com/gofiber/fiber/v2 v2.51.0/go.mod h1:xaQRZQJGqnKOQnbQw+ltvku3/h8QxvNi8o6JiJ7Ll0U=
github.com/gofiber/template v1.8.2 h1:PIv9s/7Uq6m+Fm2MDNd20pAFFKt5wWs7ZBd8iV9pWwk=
Expand Down Expand Up @@ -53,6 +55,8 @@ github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZb
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/rivo/uniseg v0.2.0 h1:S1pD9weZBuJdFmowNwbpi7BJ8TNftyUImj/0WQi72jY=
github.com/rivo/uniseg v0.2.0/go.mod h1:J6wj4VEh+S6ZtnVlnTBMWIodfgj8LQOQFoIToxlJtxc=
github.com/rivo/uniseg v0.4.3 h1:utMvzDsuh3suAEnhH0RdHmoPbU648o6CvXxTx4SBMOw=
github.com/rivo/uniseg v0.4.3/go.mod h1:FN3SvrM+Zdj16jyLfmOkMNblXMcoc8DfTHruCPUcx88=
github.com/savsgio/gotils v0.0.0-20230208104028-c358bd845dee h1:8Iv5m6xEo1NR1AvpV+7XmhI4r39LGNzwUL4YpMuL5vk=
github.com/savsgio/gotils v0.0.0-20230208104028-c358bd845dee/go.mod h1:qwtSXrKuJh/zsFQ12yEE89xfCrGKK63Rr7ctU/uCo4g=
github.com/shopspring/decimal v1.2.0 h1:abSATXmQEYyShuxI4/vyW3tV1MrKAJzCZ/0zLUXYbsQ=
Expand Down

0 comments on commit 8ef1e5e

Please sign in to comment.