eraspacelog

package module
v1.2.0 Latest Latest
Warning

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

Go to latest
Published: Nov 7, 2023 License: MIT Imports: 17 Imported by: 0

README

go-eraspacelog

Getting Started

eraspacelog is a structured logger for Go (golang), completely API compatible with the standard library logger.

Dependency

Installation

Go Version 1.16+

go get github.com/erajayatech/go-eraspacelog

Setup Environment

  • Set the following environment variables:
  • MODE=<your_application_mode>
    • Example : prod

How To Use

  • First you need to import go-eraspacelog package for using eraspacelog, one simplest example likes the follow
package main

import (
  "github.com/erajayatech/go-eraspacelog"
)

func main() {
 ...
 eraspacelog.SetupLogger(helper.GetEnv("MODE"))

// your code goes here
}

SetupLogger() need paramater mode for determine formater to print into your terminal.

Example local mode

with local mode you'll see nicely color-coded Colored

Example development mode

with development mode you'll see json raw

Set auth-header and request-header

  • Before implementation logger to every single function on your application, you must set the auth-header and request-header into a middleware.
Example set auth-header

to set auth-header, just call SetAuthHeaderInfoToContext()

package middleware

import (
  "github.com/erajayatech/go-eraspacelog"
)

func (middleware *Middleware) HeaderValidatorMiddleware() gin.HandlerFunc {
	return func(context *gin.Context) {
		source := context.Request.Header.Get("Source")
		
		eraspacelog.SetAuthHeaderInfoToContext(context, eraspacelog.AuthHeaderInfo{
			"source":        source,
		})
	}
}

// your code goes here
Example set request-header

to set request-header, just call SetAuthHeaderInfoToContext()

package middleware

import (
	"github.com/erajayatech/go-eraspacelog"
	"github.com/gin-gonic/gin"
)

func (middleware *Middleware) TraceMiddleware() gin.HandlerFunc {
	return func(context *gin.Context) {
		var traceID = helper.GenerateUUID()

		context.Header("X-Trace-Id", traceID)
		context.Set("X-Trace-Id", traceID)
		context.Set("traceID", traceID)

		eraspacelog.SetRequestHeaderInfoToContext(context, eraspacelog.RequestHeaderInfo{
			"request_id": traceID,
			"path":       fmt.Sprintf("%s %s", context.Request.Method, context.Request.URL.Path),
		})

		context.Next()
	}
}

// your code goes here

Documentation

Index

Constants

View Source
const FileKey = "file"

FileKey holds the file field

View Source
const FunctionKey = "function"

FunctionKey holds the function field

View Source
const LineKey = "line"

LineKey holds the line field

View Source
const PackageKey = "package"

PackageKey holds the package field

Variables

This section is empty.

Functions

func Dump added in v1.2.0

func Dump(i interface{}) string

func GetEnv

func GetEnv(key string, defaultValue interface{}) string

func JSONMarshal added in v1.2.0

func JSONMarshal(data interface{}) ([]byte, error)

func OtelSeverityText added in v1.2.0

func OtelSeverityText(lv logrus.Level) string

OtelSeverityText convert logrus level to otel severityText ref to https://github.com/open-telemetry/opentelemetry-specification/blob/main/specification/logs/data-model.md#severity-fields

func SetAuthHeaderInfoToContext added in v1.2.0

func SetAuthHeaderInfoToContext(ginContext *gin.Context, authHeaderInfo AuthHeaderInfo)

func SetRequestHeaderInfoToContext added in v1.2.0

func SetRequestHeaderInfoToContext(ginContext *gin.Context, requestHeaderInfo RequestHeaderInfo)

func SetupLogger added in v1.2.0

func SetupLogger(env string)

func ToByte added in v1.2.0

func ToByte(i interface{}) []byte

func WithContext added in v1.2.0

func WithContext(ctx context.Context, field map[string]interface{}) *logrus.Entry

Types

type AuthHeaderInfo added in v1.2.0

type AuthHeaderInfo map[string]interface{}

func GetAuthHeaderInfoFromContext added in v1.2.0

func GetAuthHeaderInfoFromContext(context context.Context) *AuthHeaderInfo

type Config

type Config struct {
	// contains filtered or unexported fields
}

type Fields added in v1.2.0

type Fields = logrus.Fields

type Formatter added in v1.2.0

type Formatter struct {
	ChildFormatter logrus.Formatter
	// When true, line number will be tagged to fields as well
	Line bool
	// When true, package name will be tagged to fields as well
	Package bool
	// When true, file name will be tagged to fields as well
	File bool
	// When true, only base name of the file will be tagged to fields
	BaseNameOnly bool
}

Formatter decorates log entries with function name and package name (optional) and line number (optional)

func (*Formatter) Format added in v1.2.0

func (f *Formatter) Format(entry *logrus.Entry) ([]byte, error)

Format the current log entry by adding the function name and line number of the caller.

type ILogger

type ILogger interface {
	// log message
	Log(logType string, messages interface{})

	// log message with trace
	LogWithTrace(logType string, traceID string, messages map[string]interface{})

	// Log print
	Print(logType string, messages interface{})
}

func New

func New() ILogger

type OtelTraceHook added in v1.2.0

type OtelTraceHook struct {
	// contains filtered or unexported fields
}

func NewOtelTraceHook added in v1.2.0

func NewOtelTraceHook(cfg *TraceHookConfig) *OtelTraceHook

func (*OtelTraceHook) Fire added in v1.2.0

func (h *OtelTraceHook) Fire(entry *logrus.Entry) error

func (*OtelTraceHook) Levels added in v1.2.0

func (h *OtelTraceHook) Levels() []logrus.Level

type RequestHeaderInfo added in v1.2.0

type RequestHeaderInfo map[string]interface{}

func GetRequestHeaderInfoFromContext added in v1.2.0

func GetRequestHeaderInfoFromContext(ctx context.Context) *RequestHeaderInfo

func (*RequestHeaderInfo) ToString added in v1.2.0

func (requestHeaderInfo *RequestHeaderInfo) ToString() string

type Service

type Service struct {
	// contains filtered or unexported fields
}

func (*Service) Log

func (service *Service) Log(logType string, messages interface{})

---------------- logger message

func (*Service) LogWithTrace

func (service *Service) LogWithTrace(logType string, traceID string, messages map[string]interface{})

--------------- loggger with trace id

func (*Service) Print

func (service *Service) Print(logType string, messages interface{})

---------------- logger print default by golang

type TraceHookConfig added in v1.2.0

type TraceHookConfig struct {
	RecordStackTraceInSpan bool
	EnableLevels           []logrus.Level
	ErrorSpanLevel         logrus.Level
}

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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