welogv7

package module
v1.0.2 Latest Latest
Warning

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

Go to latest
Published: Oct 4, 2024 License: MIT Imports: 14 Imported by: 0

README

Welog V7

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.

Installation

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

go get github.com/christiandoxa/welog-v7

Configuration

Welog uses a configuration struct to set up ElasticSearch connection parameters. The Config struct allows you to define the ElasticSearch URL, username, password, and index name:

type Config struct {
    ElasticIndex    string
    ElasticURL      string
    ElasticUsername string
    ElasticPassword string
}
Setting Configuration with SetConfig

The SetConfig function is used to set ElasticSearch connection parameters via environment variables. Ensure you call this function at the start of your application.

Example Usage

Call SetConfig with your configuration details before initializing the middleware:

config := welog.Config{
    ElasticIndex:    "your-index",
    ElasticURL:      "http://localhost:9200",
    ElasticUsername: "your-username",
    ElasticPassword: "your-password",
}

welog.SetConfig(config)

By calling SetConfig, you ensure that the logging library is properly configured to connect to your ElasticSearch instance, allowing detailed request and response logging to function as expected.

Usage

Middleware Setup in Fiber

To use the welog-v7 middleware in a Fiber application, set up the middleware with the configuration as follows:

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

To use the welog-v7 middleware in a Gin application, set up the middleware with the configuration 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-v7 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\"}",
  "responseHeader": {
    "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": "https://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, which is useful for debugging and monitoring the application.

Contributing

Contributions to welog-v7 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

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(fiberConfig fiber.Config) fiber.Handler

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

func NewGin

func NewGin() gin.HandlerFunc

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

func SetConfig

func SetConfig(config Config)

Types

type Config

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