Categorygithub.com/insaneadinesia/gobang/logger
package
1.2.0
Repository: https://github.com/insaneadinesia/gobang.git
Documentation: pkg.go.dev

# Packages

No description provided by the author

# README

Gobang - Logger

Gobang - Logger is a structured logging library for Go services. It leverages Zap as its core logging mechanism and adds several additional features to enhance logging capabilities.

Table of Contents

Installation

To install Gobang - Logger, use the following command:

go get -u insaneadinesia/gobang/logger

Features

  • Formatted Logs: Ensures that logs are consistently formatted for easier parsing and analysis.
  • Masking Data: Allows sensitive data (e.g., passwords, tokens) to be masked in logs.
  • Additional Trace & Span ID Information: Includes trace and span IDs for better distributed tracing support.

Quick Start

Here's a simple example to get you started with Gobang - Logger:

logger.NewLogger(logger.Option{
	IsEnable:            true,
	EnableStackTrace:    true,
	EnableMaskingFields: true,
	MaskingFields: []string{
	  "password",
      "token",
      "anything-you-want-to-mask"
	},
})

ctxLogger := logger.Context{
	ServiceName:    "service-name",
	ServiceVersion: "service-version",
	ServicePort:    9000,
	ReqMethod:      "req-method",
	ReqURI:         "req-path",
}

ctx = logger.InjectCtx(ctx, ctxLogger)

logger.Log.Info(ctx, "title", "replace-with-anything")

For more detailed examples, see the example directory.

Log Structured

Common Log

Common logs are used for logging errors, request responses to outbound services, or additional messages to aid in issue tracing. They can be placed anywhere in your code.

{
  "level": "info",
  "xtime": "2025-02-26 11:30:20.582",
  "message": "Request Header",
  "trace.id": "b03b1bba60aa5e3e8c2ee0ce141b0ad8",
  "span.id": "e4fa02ad9b3bde14",
  "app_name": "Logger Service",
  "app_version": "v1.0.0",
  "app_port": 9000,
  "app_tag": "",
  "app_method": "POST",
  "app_uri": "/test",
  "message_0": "test-message-1",
  "message_1": "test-message-2"
}

TDR Log

TDR (Transaction Data Record) logs are used for logging request and response data served by your service. They are typically placed in request-response middleware.

{
  "level": "info",
  "xtime": "2025-02-26 12:13:21.103",
  "message": "TDR",
  "trace.id": "b03b1bba60aa5e3e8c2ee0ce141b0ad8",
  "span.id": "e4fa02ad9b3bde14",
  "app_name": "Logger Service",
  "app_version": "v1.0.0",
  "app_port": 9000,
  "app_tag": "",
  "app_method": "POST",
  "app_uri": "/test",
  "app_response_code": 200,
  "app_exec_time": "29.283917ms",
  "app_request_header": {
    "Accept": [
      "*/*"
    ],
    "Accept-Encoding": [
      "gzip, deflate, br"
    ],
    "Connection": [
      "keep-alive"
    ],
    "Content-Length": [
      "229"
    ],
    "Content-Type": [
      "application/json"
    ],
    "Postman-Token": [
      "960b60ab-444a-4d01-a8b6-e3b74eff2524"
    ],
    "User-Agent": [
      "PostmanRuntime/7.43.0"
    ]
  },
  "app_request": {
    "amount": 100000,
    "recipient": {
      "account_no": "1234567890",
      "bank_code": "014",
      "bank_name": "bca"
    },
    "username": "mamatosai",
    "wallet_uuid": "48366b95-3320-464b-8f97-fa27a75fb0be"
  },
  "app_response": {
    "message": "Request Successfully Processed"
  }
}

Comparation & Explanation

KeyCommon LogTDR LogDescription
levelThe level of the log. Possible value: debug, info, warn, error, fatal or panic.
x-timeDate and time when the log was created.
trace.idTrace ID extracted from the traceparent context.
span.idSpan ID extracted from the traceparent context.
app_nameName of the service.
app_versionVersion of the service.
app_portPort number on which the service is running.
app_methodHTTP Request Method.
app_uriHTTP Request URI.
app_response_codeHTTP response status code.
app_exec_timeDuration of executed process.
app_request_headerHTTP Request headers.
app_requestHTTP Request body.
app_responseHTTP Request body.
messageTitleor Message of the log. The log should contain at least one message.
message_1Additional message or information.
message_2Additional message or information.
message_nAdditional message or information.