Skip to content

Commit

Permalink
add custom logger only log if its not 200
Browse files Browse the repository at this point in the history
Signed-off-by: Rajiv Singh <[email protected]>
  • Loading branch information
rajiv-maersk committed Aug 20, 2024
1 parent e24ae3a commit a6953ea
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 9 deletions.
6 changes: 3 additions & 3 deletions deployment.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ spec:
- NET_RAW
env:
- name: PORT
value: "8082" # Ensure this matches the port your app listens on
value: "8080" # Ensure this matches the port your app listens on
- name: GIN_MODE
value: "release"
resources:
Expand All @@ -40,14 +40,14 @@ spec:
readinessProbe:
httpGet:
path: /readiness
port: 8082
port: 8080
initialDelaySeconds: 5
periodSeconds: 10
failureThreshold: 3
livenessProbe:
httpGet:
path: /liveness
port: 8082
port: 8080
initialDelaySeconds: 15
periodSeconds: 20
failureThreshold: 3
13 changes: 7 additions & 6 deletions main.go
Original file line number Diff line number Diff line change
@@ -1,13 +1,16 @@
package main

import (
"fmt"
"health/handlers"
"log"

"github.com/gin-gonic/gin"
"go.uber.org/zap"
)

var serviceName string = "accesscontrol"

// conditionalLogger logs requests only if the status code is not 200
func conditionalLogger() gin.HandlerFunc {
return func(c *gin.Context) {
Expand All @@ -25,8 +28,6 @@ func conditionalLogger() gin.HandlerFunc {
}
}

var serviceName string = "accesscontrol"

func main() {
logger, _ := zap.NewProduction()
defer logger.Sync()
Expand All @@ -44,14 +45,14 @@ func main() {
// Liveness probe handler
router.GET("/liveness", handlers.LivenessProbeHandler(serviceName))

// Launch the server in a goroutine
go func() {
if err := router.Run("localhost:8082"); err != nil {
// To run in localhost
// router.Run("localhost:8080")
if err := router.Run(); err != nil {
// Handle error if server fails to start
logger.Error("Failed to start server", zap.Error(err))
}
}()

// Keep the main function running
select {}
fmt.Println("Server is running on port 8080")
}

0 comments on commit a6953ea

Please sign in to comment.