Initial commit: MyCrib API in Go
Complete rewrite of Django REST API to Go with: - Gin web framework for HTTP routing - GORM for database operations - GoAdmin for admin panel - Gorush integration for push notifications - Redis for caching and job queues Features implemented: - User authentication (login, register, logout, password reset) - Residence management (CRUD, sharing, share codes) - Task management (CRUD, kanban board, completions) - Contractor management (CRUD, specialties) - Document management (CRUD, warranties) - Notifications (preferences, push notifications) - Subscription management (tiers, limits) Infrastructure: - Docker Compose for local development - Database migrations and seed data - Admin panel for data management 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude <noreply@anthropic.com>
This commit is contained in:
96
pkg/utils/logger.go
Normal file
96
pkg/utils/logger.go
Normal file
@@ -0,0 +1,96 @@
|
||||
package utils
|
||||
|
||||
import (
|
||||
"io"
|
||||
"os"
|
||||
"time"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
"github.com/rs/zerolog"
|
||||
"github.com/rs/zerolog/log"
|
||||
)
|
||||
|
||||
// InitLogger initializes the zerolog logger
|
||||
func InitLogger(debug bool) {
|
||||
zerolog.TimeFieldFormat = time.RFC3339
|
||||
|
||||
var output io.Writer = os.Stdout
|
||||
|
||||
if debug {
|
||||
// Pretty console output for development
|
||||
output = zerolog.ConsoleWriter{
|
||||
Out: os.Stdout,
|
||||
TimeFormat: "15:04:05",
|
||||
}
|
||||
zerolog.SetGlobalLevel(zerolog.DebugLevel)
|
||||
} else {
|
||||
// JSON output for production
|
||||
zerolog.SetGlobalLevel(zerolog.InfoLevel)
|
||||
}
|
||||
|
||||
log.Logger = zerolog.New(output).With().Timestamp().Caller().Logger()
|
||||
}
|
||||
|
||||
// GinLogger returns a Gin middleware for request logging
|
||||
func GinLogger() gin.HandlerFunc {
|
||||
return func(c *gin.Context) {
|
||||
start := time.Now()
|
||||
path := c.Request.URL.Path
|
||||
raw := c.Request.URL.RawQuery
|
||||
|
||||
// Process request
|
||||
c.Next()
|
||||
|
||||
// Log after request
|
||||
end := time.Now()
|
||||
latency := end.Sub(start)
|
||||
|
||||
if raw != "" {
|
||||
path = path + "?" + raw
|
||||
}
|
||||
|
||||
msg := "Request"
|
||||
if len(c.Errors) > 0 {
|
||||
msg = c.Errors.String()
|
||||
}
|
||||
|
||||
event := log.Info()
|
||||
statusCode := c.Writer.Status()
|
||||
|
||||
if statusCode >= 400 && statusCode < 500 {
|
||||
event = log.Warn()
|
||||
} else if statusCode >= 500 {
|
||||
event = log.Error()
|
||||
}
|
||||
|
||||
event.
|
||||
Str("method", c.Request.Method).
|
||||
Str("path", path).
|
||||
Int("status", statusCode).
|
||||
Str("ip", c.ClientIP()).
|
||||
Dur("latency", latency).
|
||||
Str("user-agent", c.Request.UserAgent()).
|
||||
Msg(msg)
|
||||
}
|
||||
}
|
||||
|
||||
// GinRecovery returns a Gin middleware for panic recovery
|
||||
func GinRecovery() gin.HandlerFunc {
|
||||
return func(c *gin.Context) {
|
||||
defer func() {
|
||||
if err := recover(); err != nil {
|
||||
log.Error().
|
||||
Interface("error", err).
|
||||
Str("path", c.Request.URL.Path).
|
||||
Str("method", c.Request.Method).
|
||||
Msg("Panic recovered")
|
||||
|
||||
c.AbortWithStatusJSON(500, gin.H{
|
||||
"error": "Internal server error",
|
||||
})
|
||||
}
|
||||
}()
|
||||
|
||||
c.Next()
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user