setup the project structure

This commit is contained in:
FabricSoul 2025-07-16 01:06:39 -04:00
parent 279efc8817
commit ed3854f542
10 changed files with 257 additions and 80 deletions

46
internal/logger/logger.go Normal file
View file

@ -0,0 +1,46 @@
// Package logger provides a configurable constructor for the application's zap logger.
// It supports distinct configurations for development and production environments.
package logger
import (
"go.uber.org/zap"
"go.uber.org/zap/zapcore"
)
// New creates a new zap.Logger tailored for either a development or production environment.
//
// In development (isDev = true), it returns a human-readable, colored, debug-level logger.
//
// In production (isDev = false), it returns a structured, JSON-formatted, info-level logger
// that is optimized for performance and machine parsing.
func New(isDev bool) (*zap.Logger, error) {
var config zap.Config
var err error
if isDev {
// Development configuration:
// - Human-readable, console-friendly output
// - Logs at Debug level and above
// - Includes caller's file and line number
// - Adds color to log levels for easy scanning
config = zap.NewDevelopmentConfig()
config.EncoderConfig.EncodeLevel = zapcore.CapitalColorLevelEncoder
} else {
// Production configuration:
// - Structured JSON output for machine parsing
// - Logs at Info level and above
// - Highly performant
// - Uses ISO8601 timestamp format for consistency across services
config = zap.NewProductionConfig()
config.EncoderConfig.TimeKey = "timestamp"
config.EncoderConfig.EncodeTime = zapcore.ISO8601TimeEncoder
}
// Build the logger from the selected configuration.
logger, err := config.Build()
if err != nil {
return nil, err
}
return logger, nil
}

View file

@ -0,0 +1,51 @@
package middleware
import (
"time"
"github.com/labstack/echo/v4"
"go.uber.org/zap"
)
// ZapRequestLogger is a middleware that logs incoming HTTP requests using a zap.Logger.
// It logs the method, URI, status, latency, remote IP, and response size.
func ZapRequestLogger(log *zap.Logger) echo.MiddlewareFunc {
return func(next echo.HandlerFunc) echo.HandlerFunc {
return func(c echo.Context) error {
start := time.Now()
// Run the next handler in the chain
err := next(c)
if err != nil {
// Let Echo's central error handler deal with it
c.Error(err)
}
// After the handler has run, we can log the details
req := c.Request()
res := c.Response()
stop := time.Now()
// These are our structured logging fields
fields := []zap.Field{
zap.String("method", req.Method),
zap.String("uri", req.RequestURI),
zap.Int("status", res.Status),
zap.String("remote_ip", c.RealIP()),
zap.Duration("latency", stop.Sub(start)),
zap.Int64("size", res.Size),
}
// Differentiate between server errors and client errors
if res.Status >= 500 {
log.Error("Server Error", fields...)
} else if res.Status >= 400 {
log.Warn("Client Error", fields...)
} else {
log.Info("Request Handled", fields...)
}
return nil
}
}
}