welog

package module
v1.0.2 Latest Latest
Warning

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

Go to latest
Published: Sep 25, 2024 License: MIT Imports: 12 Imported by: 0

README

Welog

Welog is a logging library designed for Go applications, integrating with ElasticSearch and utilizing logrus for structured logging. It supports log management for Go applications running on popular web frameworks like Fiber and Gin, providing detailed request and response logging.

Requirements

Before using this library, ensure you have the following environment variables set up in your .env file:

ELASTIC_URL=http://127.0.0.1:9200
ELASTIC_USERNAME=elastic
ELASTIC_PASSWORD=changeme
ELASTIC_INDEX=welog

Installation

To install welog, add the library to your Go project by running:

go get github.com/christiandoxa/welog

Additionally, install godotenv to manage environment variables:

go get github.com/joho/godotenv

Usage

Middleware Setup in Fiber

To use the welog middleware in a Fiber application, add the middleware as follows:

app := fiber.New()
app.Use(welog.NewFiber(fiber.Config{}))
Middleware Setup in Gin

To use the welog middleware in a Gin application, set up the middleware as follows:

router := gin.Default()
router.Use(welog.NewGin())
Logging Client Requests
Logging Client Requests in Fiber

When using a custom Fiber client, you can log client requests with welog using the following method:

welog.LogFiberClient(
    c,
    requestURL,
    requestMethod,
    requestContentType,
    requestHeader,
    requestBody,
    responseHeader,
    responseBody,
    responseStatus,
    requestTime,
    responseLatency,
)
  • c: The Fiber context.
  • Other parameters: Include details of the request and response such as URL, method, headers, body, status, and timing.
Logging Client Requests in Gin

For custom logging of client requests within Gin, use the LogGinClient method:

welog.LogGinClient(
    c,
    requestURL,
    requestMethod,
    requestContentType,
    requestHeader,
    requestBody,
    responseHeader,
    responseBody,
    responseStatus,
    requestTime,
    responseLatency,
)
  • c: The Gin context.
  • Other parameters: Include details of the request and response such as URL, method, headers, body, status, and timing.
Logging Outside of Handlers

If you need to log errors or other information outside of a Fiber or Gin handler, you can directly use the logger.Logger() instance:

logger.Logger().Fatal(err)
Logging Inside Handlers in Fiber

When logging within a Fiber handler, use the logger instance stored in the Fiber context to ensure consistent and contextual logging:

c.Locals("logger").(*logrus.Entry).Error(err)
Logging Inside Handlers in Gin

When logging within a Gin handler, use the logger instance stored in the Gin context to ensure consistent and contextual logging:

c.MustGet("logger").(*logrus.Entry).Error(err)

Sample Output Logging

Below is a sample output log generated by logFiber and LogFiberClient functions:

{
  "level": "info",
  "msg": "",
  "requestAgent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.124 Safari/537.36",
  "requestBody": {
    "key1": "value1",
    "key2": "value2"
  },
  "requestBodyString": "{\"key1\": \"value1\", \"key2\": \"value2\"}",
  "requestContentType": "application/json",
  "requestHeader": {
    "Content-Type": "application/json",
    "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64)"
  },
  "requestHostName": "localhost",
  "requestId": "123456",
  "requestIp": "192.168.1.1",
  "requestMethod": "POST",
  "requestProtocol": "HTTP/1.1",
  "requestTimestamp": "2024-09-25T12:34:56.789Z",
  "requestUrl": "http://localhost/api/v1/resource",
  "responseBody": {
    "responseKey1": "responseValue1"
  },
  "responseBodyString": "{\"responseKey1\": \"responseValue1\"}",
  "responseHeaderString": "Content-Type: application/json; charset=utf-8",
  "responseLatency": "150ms",
  "responseStatus": 200,
  "responseTimestamp": "2024-09-25T12:34:56.939Z",
  "responseUser": "unknown",
  "target": [
    {
      "targetRequestBody": {
        "param1": "value1"
      },
      "targetRequestBodyString": "{\"param1\": \"value1\"}",
      "targetRequestContentType": "application/json",
      "targetRequestHeader": {
        "Authorization": "Bearer some_token"
      },
      "targetRequestMethod": "GET",
      "targetRequestTimestamp": "2024-09-25T12:34:56.789Z",
      "targetRequestURL": "http://example.com/api/data",
      "targetResponseBody": {
        "dataKey": "dataValue"
      },
      "targetResponseBodyString": "{\"dataKey\": \"dataValue\"}",
      "targetResponseHeader": {
        "Content-Length": "123"
      },
      "targetResponseLatency": "200ms",
      "targetResponseStatus": 200,
      "targetResponseTimestamp": "2024-09-25T12:34:56.989Z"
    }
  ]
}

This log provides detailed information about the request and response including headers, body, latency, status, and additional metadata, useful for debugging and monitoring the application.

Contributing

Contributions to welog are welcome! If you have suggestions, bug reports, or want to contribute code, please create a pull request or open an issue on GitHub.

License

This project is licensed under the MIT License - see the LICENSE file for details.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func LogFiberClient

func LogFiberClient(
	c *fiber.Ctx,
	requestURL string,
	requestMethod string,
	requestContentType string,
	requestHeader map[string]interface{},
	requestBody []byte,
	responseHeader map[string]interface{},
	responseBody []byte,
	responseStatus int,
	requestTime time.Time,
	responseLatency time.Duration,
)

LogFiberClient logs a custom client request and response for Fiber.

func LogGinClient added in v1.0.2

func LogGinClient(
	c *gin.Context,
	requestURL string,
	requestMethod string,
	requestContentType string,
	requestHeader map[string]interface{},
	requestBody []byte,
	responseHeader map[string]interface{},
	responseBody []byte,
	responseStatus int,
	requestTime time.Time,
	responseLatency time.Duration,
)

LogGinClient logs a custom client request and response for Gin.

func NewFiber

func NewFiber(config 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.

Types

This section is empty.

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