{
  "slug": "Adding-Zap-Logger-to-Your-Go-Project--A-Practical-Guide----52091fc2db7d",
  "title": "Adding Zap Logger to Your Go Project: A Practical Guide⚡ ️",
  "subtitle": "Logging is the backbone of observability in any production-grade system. If you’re building scalable, maintainable Go applications, Uber’s…",
  "excerpt": "Logging is the backbone of observability in any production-grade system. If you’re building scalable, maintainable Go applications, Uber’s…",
  "date": "2025-10-06",
  "tags": [
    "Go"
  ],
  "readingTime": "2 min",
  "url": "https://medium.com/@mobinshaterian/adding-zap-logger-to-your-go-project-a-practical-guide-%EF%B8%8F-52091fc2db7d",
  "hero": "https://cdn-images-1.medium.com/max/800/1*9NXIaQR7jUssX8O1s-6lGg.jpeg",
  "content": [
    {
      "type": "heading",
      "level": 2,
      "text": "Adding Zap Logger to Your Go Project: A Practical Guide⚡ ️"
    },
    {
      "type": "paragraph",
      "html": "Logging is the backbone of observability in any production-grade system. If you’re building scalable, maintainable Go applications, Uber’s Zap is a high-performance, structured logging library that fits right in."
    },
    {
      "type": "paragraph",
      "html": "This article walks you through integrating Zap into your Go project — from setup to idiomatic usage."
    },
    {
      "type": "heading",
      "level": 2,
      "text": "🚀 Why Zap?"
    },
    {
      "type": "paragraph",
      "html": "Zap is designed for speed and structure. Unlike traditional loggers that format strings, Zap encodes logs as structured fields. This makes it ideal for:"
    },
    {
      "type": "list",
      "ordered": false,
      "items": [
        "High-throughput services",
        "JSON-based log pipelines",
        "Real-time debugging and monitoring"
      ]
    },
    {
      "type": "image",
      "src": "https://cdn-images-1.medium.com/max/800/1*9NXIaQR7jUssX8O1s-6lGg.jpeg",
      "alt": "Adding Zap Logger to Your Go Project: A Practical Guide⚡ ️",
      "caption": "",
      "width": 1024,
      "height": 1024
    },
    {
      "type": "heading",
      "level": 2,
      "text": "Go-simple project"
    },
    {
      "type": "paragraph",
      "html": "Building on our previous article, we plan to integrate the Zap logger into our simple Golang project structure in order to provide more efficient and structured logging."
    },
    {
      "type": "heading",
      "level": 2,
      "text": "🛠️ Step 1: Install Zap"
    },
    {
      "type": "paragraph",
      "html": "Add Zap to your project using <code>go get</code>:"
    },
    {
      "type": "code",
      "lang": "bash",
      "code": "go get go.uber.org/zap"
    },
    {
      "type": "heading",
      "level": 2,
      "text": "📦 Step 2: Create a Logger Module"
    },
    {
      "type": "paragraph",
      "html": "Encapsulate logger setup in a reusable package. For example:"
    },
    {
      "type": "paragraph",
      "html": "<code>internal/pkg/logger/logger.go</code>"
    },
    {
      "type": "code",
      "lang": "go",
      "code": "package logger\nimport (\n\"go.uber.org/zap\"\n\"go.uber.org/fx\"\n)\nfunc NewLogger() (*zap.Logger, error) {\n    cfg := zap.NewProductionConfig()\n    cfg.OutputPaths = []string{\n        \"stdout\"\n    }\n    return cfg.Build()\n}\nvar Module = fx.Provide(NewLogger)"
    },
    {
      "type": "heading",
      "level": 2,
      "text": "🧩 Step 3: Wire Logger into Your App"
    },
    {
      "type": "paragraph",
      "html": "In your <code>main.go</code>, include the logger module:"
    },
    {
      "type": "code",
      "lang": "go",
      "code": "package main\nimport (\n\"go.uber.org/fx\"\n\"your_project/internal/pkg/logger\"\n)\nfunc main() {\n    app := fx.New(logger.Module, // other modules...\n    )\n    app.Run()\n}"
    },
    {
      "type": "paragraph",
      "html": "Replace <code>your_project</code> with your actual module name."
    },
    {
      "type": "heading",
      "level": 2,
      "text": "🧪 Step 4: Inject Logger into Services"
    },
    {
      "type": "paragraph",
      "html": "Use constructor injection to pass the logger:"
    },
    {
      "type": "code",
      "lang": "go",
      "code": "type ProductService struct {\n    log *zap.Logger\n}\nfunc NewProductService(log *zap.Logger) *ProductService {\n    log.Info(\"ProductService initialized\")\n    return &ProductService{\n        log: log\n    }\n}"
    },
    {
      "type": "paragraph",
      "html": "This ensures structured logging is available throughout your service layer."
    },
    {
      "type": "heading",
      "level": 2,
      "text": "🧼 Step 5: Graceful Shutdown"
    },
    {
      "type": "paragraph",
      "html": "Flush logs on shutdown using FX lifecycle hooks:"
    },
    {
      "type": "code",
      "lang": "go",
      "code": "func RegisterLoggerLifecycle(lc fx.Lifecycle, log *zap.Logger) {\n    lc.Append(fx.Hook{\n        OnStop: func(ctx context.Context) error {\n            return log.Sync()\n        },\n    })\n}\nvar Module = fx.Options(\nfx.Provide(NewLogger),\nfx.Invoke(RegisterLoggerLifecycle),)"
    },
    {
      "type": "heading",
      "level": 2,
      "text": "🧠 Best Practices"
    },
    {
      "type": "list",
      "ordered": false,
      "items": [
        "Use structured fields: <code>log.Info(\"User created\", zap.String(\"email\", user.Email))</code>",
        "Avoid global loggers — inject via DI",
        "Use <code>zap.SugaredLogger</code> for ergonomic logging if needed",
        "Always <code>Sync()</code> on shutdown to flush buffers"
      ]
    },
    {
      "type": "heading",
      "level": 2,
      "text": "log example"
    },
    {
      "type": "code",
      "lang": "json",
      "code": "{\n  \"level\": \"info\",\n  \"ts\": 1759755003.9162495,\n  \"caller\": \"service/service.go:34\",\n  \"msg\": \"Product created\",\n  \"id\": 8\n}"
    },
    {
      "type": "heading",
      "level": 2,
      "text": "Git Commit"
    },
    {
      "type": "heading",
      "level": 2,
      "text": "✅ Conclusion"
    },
    {
      "type": "paragraph",
      "html": "Integrating Zap into your Go project gives you powerful, structured logging with minimal overhead. Whether you’re building microservices, APIs, or CLI tools, Zap helps you stay observant, performant, and production-ready."
    },
    {
      "type": "paragraph",
      "html": "Want help wiring Zap into your own project structure or adding contextual fields like request IDs or user agents? I’d be happy to walk you through it."
    }
  ]
}
