welog package - github.com/christiandoxa/welog - Go Packages

welog

package module
v1.1.7 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Dec 1, 2025 License: MIT Imports: 15 Imported by: 0

README

Welog

Welog is a structured logging library for Go applications, integrating with ElasticSearch and powered by Logrus.
It provides detailed request/response logging for popular Go web frameworks such as Fiber and Gin, and supports both server-side and client-side HTTP logging.


Features

  • 📦 Plug-and-play middleware for Fiber and Gin
  • 📝 Structured JSON logs via Logrus
  • 🔍 Detailed request/response tracing with latency and metadata
  • 🔗 ElasticSearch integration for centralized log storage
  • 🎯 Context-aware logging for handlers
  • 🔄 Client request logging for outbound HTTP calls

Installation

go get github.com/christiandoxa/welog

Quick Start

package main

import (
    "github.com/christiandoxa/welog"
    "github.com/gofiber/fiber/v2"
)

func main() {
    // 1. Configure ElasticSearch connection
	welog.SetConfig(welog.Config{
        ElasticIndex:    "my-logs",
        ElasticURL:      "http://localhost:9200",
        ElasticUsername: "elastic",
        ElasticPassword: "changeme",
    })

    // 2. Create Fiber app and attach Welog middleware
    app := fiber.New()
    app.Use(welog.NewFiber(fiber.Config{}))

    // Example route
    app.Get("/", func(c *fiber.Ctx) error {
        return c.JSON(fiber.Map{"message": "hello world"})
    })

    app.Listen(":3000")
}

Configuration

Welog exposes welog.Config to store ElasticSearch connection details:

type Config struct {
    ElasticIndex    string
    ElasticURL      string
    ElasticUsername string
    ElasticPassword string
}

Set it at application startup using:

welog.SetConfig(welog.Config{
    ElasticIndex:    "my-logs",
    ElasticURL:      "http://localhost:9200",
    ElasticUsername: "elastic",
    ElasticPassword: "changeme",
})
Environment Variables Used
Variable Description
ELASTIC_INDEX__ ElasticSearch index name
ELASTIC_URL__ ElasticSearch base URL
ELASTIC_USERNAME__ ElasticSearch username
ELASTIC_PASSWORD__ ElasticSearch password

Middleware Setup

Fiber
app := fiber.New()
app.Use(welog.NewFiber(fiber.Config{}))
Gin
router := gin.Default()
router.Use(welog.NewGin())

Client Request Logging

Welog supports logging outbound HTTP requests from within your application.

Fiber Example
import (
    "time"
    "net/http"
    "github.com/christiandoxa/welog/pkg/model"
)

reqModel := model.TargetRequest{
    URL:         "https://example.com/api",
    Method:      "GET",
    ContentType: "application/json",
    Header:      map[string]interface{}{"Authorization": "Bearer token"},
    Body:        []byte(`{"param":"value"}`),
    Timestamp:   time.Now(),
}

resModel := model.TargetResponse{
    Header:  map[string]interface{}{"Content-Type": "application/json"},
    Body:    []byte(`{"status":"ok"}`),
    Status:  http.StatusOK,
    Latency: 200 * time.Millisecond,
}

welog.LogFiberClient(c, reqModel, resModel)
Gin Example
welog.LogGinClient(c, reqModel, resModel)

Logging Inside Handlers

Welog stores a contextual *logrus.Entry inside the request context.

Fiber
c.Locals("logger").(*logrus.Entry).Error("Something went wrong")
Gin
c.MustGet("logger").(*logrus.Entry).Error("Something went wrong")

Sample Output

Example JSON log entry generated by logFiber:

{
  "level": "info",
  "requestAgent": "PostmanRuntime/7.31.1",
  "requestBody": {"foo": "bar"},
  "requestMethod": "POST",
  "requestUrl": "http://localhost/api/v1/resource",
  "requestTimestamp": "2024-09-25T12:34:56.789Z",
  "responseStatus": 200,
  "responseLatency": "150ms",
  "responseTimestamp": "2024-09-25T12:34:56.939Z",
  "target": [
    {
      "targetRequestMethod": "GET",
      "targetRequestURL": "https://example.com/api/data",
      "targetResponseStatus": 200,
      "targetResponseLatency": "200ms"
    }
  ]
}

Contributing

Contributions are welcome! Please open an issue or submit a pull request.


License

MIT License — see LICENSE for details.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func LogFiberClient

func LogFiberClient(
	c *fiber.Ctx,
	req model.TargetRequest,
	res model.TargetResponse,
)

LogFiberClient logs a custom client request and response for Fiber.

func LogGinClient added in v1.0.2

func LogGinClient(
	c *gin.Context,
	req model.TargetRequest,
	res model.TargetResponse,
)

LogGinClient logs a custom client request and response for Gin.

func NewFiber

func NewFiber(fiberConfig fiber.Config) fiber.Handler

NewFiber creates a new Fiber middleware that logs requests and responses.

func NewGin added in v1.0.2

func NewGin() gin.HandlerFunc

NewGin creates a new Gin middleware that logs requests and responses.

func SetConfig added in v1.0.5

func SetConfig(config Config)

Types

type Config added in v1.0.3

type Config struct {
	ElasticIndex    string
	ElasticURL      string
	ElasticUsername string
	ElasticPassword string
}

Directories

Path Synopsis
pkg
constant/envkey
Package envkey defines environment variable keys used for configuring the application's connection to ElasticSearch.
Package envkey defines environment variable keys used for configuring the application's connection to ElasticSearch.
constant/generalkey
Package generalkey defines common keys used within the application's context for logging and request handling.
Package generalkey defines common keys used within the application's context for logging and request handling.
infrastructure/logger
Package logger provides a logging utility that integrates with ElasticSearch and uses the logrus package for structured logging.
Package logger provides a logging utility that integrates with ElasticSearch and uses the logrus package for structured logging.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL