paycloudgohelper

package module
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: Mar 7, 2025 License: MIT Imports: 11 Imported by: 0

README

Go Helper

logo

Introduction

This repo for Golang Helper services.

⚙️ Installation

via go modules
go get bitbucket.org/paycloudid/paycloudgohelper

🎯 Features

⚡️ Usage

📖 Logger Wrapper

rs/zerolog is a logger package for Go which follows uber/zap's JSON output format with calls chained for ease of logging.

This package is just a wrapper on initialization of zerolog logger instance which stores and propagates through context.Context.

Usage

Example-1: Initialization and propagation of logger through context variable
package main

import (
	"context"
	"os"

	zlog "bitbucket.org/paycloudid/paycloudgohelper"
)

func main() {
	ctx, logger := zlog.NewLogger(context.Background(), os.Stdout, zlog.LevelDebug)

	logger.Info().Msg("Message from main")

	greet(ctx)
}

func greet(ctx context.Context) {
	logger := zlog.FromContext(ctx)

	logger.Info().Msg("Message from greet")
}

This outputs logs as follows:

{"log-level":"info","timestamp":"2023-03-10T15:32:07+05:30","caller":"main.go:13","log-message":"Message from main"}
{"log-level":"info","timestamp":"2023-03-10T15:32:07+05:30","caller":"main.go:21","log-message":"Message from greet"}
Example-2: Using zerolog's With() to create child logger with added extra fields.

In following example, With adds {"flow": "example-manager"} to child logger which is then stored in Manager instance.

package main

import (
	"context"
	"os"

	zlog "bitbucket.org/paycloudid/paycloudgohelper"
)

func main() {
	ctx, logger := zlog.NewLogger(context.Background(), os.Stdout, zlog.LevelDebug)

	logger.Info().Msg("Message from main")

	manager := NewManager(ctx)
	manager.Greet()
}

func (m Manager) Greet() {
	m.logger.Info().Msg("Message from Greet")
}

type Manager struct {
	logger zlog.Logger
}

func NewManager(ctx context.Context) *Manager {
	logger := zlog.FromContext(ctx).With().
		Str("flow", "example-manager").Logger()

	return &Manager{
		logger: zlog.FromRawLogger(logger),
	}
}

This outputs logs as follows:

{"log-level":"info","timestamp":"2023-03-10T15:41:33+05:30","caller":"main.go:13","log-message":"Message from main"}
{"log-level":"info","flow":"example-manager","timestamp":"2023-03-10T15:41:33+05:30","caller":"main.go:20","log-message":"Message from greet"}

Note: The zlog.FromRawLogger creates zlog.Logger from zerolog.Logger avoiding the need for direct import of zerolog.

📖 Logger Sentry

import (
     ...
     bitbucket.org/paycloudid/paycloudgohelper
     ...
)

func main(){
     ...
      paycloudgohelper.InitSentry("this is url sentry", "environment (develop, staging , production)", "tags release (1.0.0 , 1.0.12)", debug (true,false))
     ...

     ...
     // Send Error
     if err != nil {
		paycloudgohelper.SendSentryError(err, "saas-be-research-manager", "User", "divide")
          // 1. Error
          // 2. Service
          // 3. Module
          // 4. Function
	}
     ...

     // Send Message
     ...
     paycloudgohelper.SendSentryMessage("this is sample message", "saas-be-research-manager", "User", "divide")
          // 1. Message
          // 2. Service
          // 3. Module
          // 4. Function
     ...

     // Send Event
     ...
     paycloudgohelper.SendSentryEvent(*sentry.Event)
          // 1. Sentry Event
          // 2. Service
          // 3. Module
          // 4. Function
     ...
}

  • Result
- logger error
+ logger warning
! logger success
# logger info

📖 Data

Documentation

Index

Constants

This section is empty.

Variables

View Source
var Validates *validator.Validate

Functions

func Bind

func Bind(form interface{}, structs interface{}) interface{}

func ClientIP

func ClientIP(c echo.Context) string

func DateNil

func DateNil(dt interface{}) interface{}

func DateParse

func DateParse(d interface{}, format string) *time.Time

func ErrorContext

func ErrorContext(message interface{}, c echo.Context) (err error)

func InArray

func InArray(val interface{}, arrays interface{}) bool

func InitSentry

func InitSentry(Dsn, Environment, Release string, Debug bool) *sentry.Client

func JsonDecode

func JsonDecode(data interface{}) (maps map[string]interface{}, err error)

func JsonEncode

func JsonEncode(data interface{}) string

func JsonEncodeBeautify

func JsonEncodeBeautify(data interface{}) string

func LoggerDebug

func LoggerDebug(msg interface{})

LoggerDebug this is for logger debug level

func LoggerError

func LoggerError(err error)

LoggerError this is for logger error level

func LoggerInfo

func LoggerInfo(message interface{})

LoggerInfo this is for logger info level

func LoggerSuccess

func LoggerSuccess(message interface{})

LoggerSuccess this is for logger success level

func LoggerWarning

func LoggerWarning(message interface{})

LoggerWarning this is for logger warning level

func NotFoundContext

func NotFoundContext(message interface{}, data interface{}, c echo.Context) (err error)

func RPCJSONResponse

func RPCJSONResponse(status string, message interface{}, data interface{}) string

func RandStringBytes

func RandStringBytes(n int) string

func ResponseContext

func ResponseContext(code int, message interface{}, data interface{}, c echo.Context) error

func SendSentryError

func SendSentryError(err error, service, module, function string)

func SendSentryEvent

func SendSentryEvent(event *sentry.Event, service, module, function string)

func SendSentryMessage

func SendSentryMessage(message string, service, module, function string)

func Success

func Success(message string, data interface{}, c echo.Context) map[string]interface{}

func SuccessContext

func SuccessContext(message interface{}, data interface{}, c echo.Context) (err error)

V1 Version 1 snake_case

func TimeoutContext

func TimeoutContext(message interface{}, c echo.Context) (err error)

func UUID

func UUID() string

func V2ErrorContext

func V2ErrorContext(message interface{}, c echo.Context) (err error)

func V2NotFoundContext

func V2NotFoundContext(message interface{}, data interface{}, c echo.Context) (err error)

func V2RPCJSONResponse

func V2RPCJSONResponse(status string, message interface{}, data interface{}) string

func V2ResponseContext

func V2ResponseContext(code int, message interface{}, data interface{}, c echo.Context) error

func V2Success

func V2Success(message string, data interface{}, c echo.Context) map[string]interface{}

func V2SuccessContext

func V2SuccessContext(message interface{}, data interface{}, c echo.Context) (err error)

V2 Version 2 CamlCase

func V2TimeoutContext

func V2TimeoutContext(message interface{}, c echo.Context) (err error)

func V2ValidationContext

func V2ValidationContext(message interface{}, data interface{}, c echo.Context) (err error)

func V2ValidationResp

func V2ValidationResp(message interface{}, data interface{}) map[string]interface{}

func Validate

func Validate(c echo.Context, i interface{}) (interface{}, error)

func ValidateRPC

func ValidateRPC(i interface{}) (interface{}, error)

func ValidationContext

func ValidationContext(message interface{}, data interface{}, c echo.Context) (err error)

func ValidationResp

func ValidationResp(message interface{}, data interface{}) map[string]interface{}

Types

type JSONResponse

type JSONResponse struct {
	Code    int         `json:"status_code"`
	Status  string      `json:"status"`
	Message interface{} `json:"message"`
	Data    interface{} `json:"data"`
}

func (*JSONResponse) Error

func (response *JSONResponse) Error(message interface{}, data interface{})

func (*JSONResponse) Success

func (response *JSONResponse) Success(message interface{}, data interface{})

func (*JSONResponse) Validation

func (response *JSONResponse) Validation(message interface{}, data interface{})

type Response

type Response struct {
	Code    int         `json:"status_code"`
	Status  string      `json:"status"`
	Message interface{} `json:"message"`
	Data    interface{} `json:"data"`
}

func (*Response) Error

func (response *Response) Error(code int, message string, data interface{})

func (*Response) Success

func (response *Response) Success(code int, message string, data interface{})

type SentryData

type SentryData struct {
	Dsn             string
	Environment     string
	Release         string
	Debug           bool
	TraceSampleRate float64
	Event           *sentry.Event
}
var (
	Sentries     *SentryData
	SentryClient *sentry.Client
)

func NewSentryData

func NewSentryData() *SentryData

type V2JSONResponse

type V2JSONResponse struct {
	Code    int         `json:"StatusCode"`
	Status  string      `json:"Status"`
	Message interface{} `json:"Message"`
	Data    interface{} `json:"Data"`
}

func (*V2JSONResponse) V2Error

func (response *V2JSONResponse) V2Error(message interface{}, data interface{})

func (*V2JSONResponse) V2Success

func (response *V2JSONResponse) V2Success(message interface{}, data interface{})

func (*V2JSONResponse) V2Validation

func (response *V2JSONResponse) V2Validation(message interface{}, data interface{})

type V2Response

type V2Response struct {
	Code    int         `json:"StatusCode"`
	Status  string      `json:"Status"`
	Message interface{} `json:"Message"`
	Data    interface{} `json:"Data"`
}

func (*V2Response) V2Error

func (response *V2Response) V2Error(code int, message string, data interface{})

func (*V2Response) V2Success

func (response *V2Response) V2Success(code int, message string, data interface{})

Directories

Path Synopsis

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL
JackTT - Gopher 🇻🇳