gou

package module
v0.0.0-...-e7d0810 Latest Latest
Warning

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

Go to latest
Published: Oct 19, 2021 License: MIT Imports: 24 Imported by: 488

README

gou - Go Utilities

Go Utilities (logging, json)

JsonHelper

A Go Json Helper, focused on Type coercion, and json path query.

	package main
	import . "github.com/araddon/gou"
	import . "github.com/araddon/gou/goutest"
	import "testing"


	func TestJsonHelper() {

		var jsonData := []byte(`{
			"name":"aaron",
			"nullstring":null,
			"ints":[1,2,3,4],
			"int":1,
			"intstr":"1",
			"int64":1234567890,
			"MaxSize" : 1048576,
			"strings":["string1"],
			"stringscsv":"string1,string2",
			"nested":{
				"nest":"string2",
				"strings":["string1"],
				"int":2,
				"list":["value"],
				"nest2":{
					"test":"good"
				}
			},
			"nested2":[
				{"sub":2}
			],
			"period.name":"value"
		}`

		jh := NewJsonHelper(jsonData)

		// String method
		Assert(jh.String("name") == "aaron", t, "should get 'aaron' %s", jh.String("name"))
		// Int Method
		Assert(jh.Int("int") == 1, t, "get int ")
		// Selecting items from an array
		Assert(jh.Int("ints[0]") == 1, t, "get int from array %d", jh.Int("ints[0]"))
		Assert(jh.Int("ints[2]") == 3, t, "get int from array %d", jh.Int("ints[0]"))
		// Getting arrays
		Assert(len(jh.Ints("ints")) == 4, t, "get int array %v", jh.Ints("ints"))
		// Type coercion to Int64
		Assert(jh.Int64("int64") == 1234567890, t, "get int")
		Assert(jh.Int("nested.int") == 2, t, "get int")

		// Path based selection
		Assert(jh.String("nested.nest") == "string2", t, "should get string %s", jh.String("nested.nest"))
		Assert(jh.String("nested.nest2.test") == "good", t, "should get string %s", jh.String("nested.nest2.test"))
		Assert(jh.String("nested.list[0]") == "value", t, "get string from array")
		Assert(jh.Int("nested2[0].sub") == 2, t, "get int from obj in array %d", jh.Int("nested2[0].sub"))

		// casing?
		Assert(jh.Int("MaxSize") == 1048576, t, "get int, test capitalization? ")
		sl := jh.Strings("strings")
		Assert(len(sl) == 1 && sl[0] == "string1", t, "get strings ")
		sl = jh.Strings("stringscsv")
		Assert(len(sl) == 2 && sl[0] == "string1", t, "get strings ")

		// Safe gets
		i64, ok := jh.Int64Safe("int64")
		Assert(ok, t, "int64safe ok")
		Assert(i64 == 1234567890, t, "int64safe value")

		i, ok := jh.IntSafe("int")
		Assert(ok, t, "intsafe ok")
		Assert(i == 1, t, "intsafe value")

		l := jh.List("nested2")
		Assert(len(l) == 1, t, "get list")

		jhm := jh.Helpers("nested2")
		Assert(len(jhm) == 1, t, "get list of helpers")
		Assert(jhm[0].Int("sub") == 2, t, "Should get list of helpers")

		// Now lets test xpath type syntax
		Assert(jh.Int("/MaxSize") == 1048576, t, "get int, test capitalization? ")
		Assert(jh.String("/nested/nest") == "string2", t, "should get string %s", jh.String("/nested/nest"))
		Assert(jh.String("/nested/list[0]") == "value", t, "get string from array")
		// note this one has period in name
		Assert(jh.String("/period.name") == "value", t, "test period in name ")
	}

Logging

Yet Another Go Logger, configureable logging.

	package main
	import "github.com/araddon/gou"
	import "flag"

	var logLevel *string = flag.String("logging", "debug", "Which log level: [debug,info,warn,error,fatal]")

	func main() {

		flag.Parse()
		gou.SetupLogging(*logLevel)

		// logging methods
		gou.Debug("hello", thing, " more ", stuff)

		gou.Error("hello")

		gou.Errorf("hello %v", thing)
	}

License

MIT License

Documentation

Index

Constants

View Source
const (
	MaxInt  = 1<<(BitsPerWord-1) - 1 // either 1<<31 - 1 or 1<<63 - 1
	MinInt  = -MaxInt - 1            // either -1 << 31 or -1 << 63
	MaxUint = 1<<BitsPerWord - 1     // either 1<<32 - 1 or 1<<64 - 1
)

Implementation-specific integer limit values.

View Source
const (
	NOLOGGING = -1
	FATAL     = 0
	ERROR     = 1
	WARN      = 2
	INFO      = 3
	DEBUG     = 4
)
View Source
const BitsPerWord = bitsPerWord // either 32 or 64

Implementation-specific size of int and uint in bits.

Variables

View Source
var (
	LogLevel    int = ERROR
	EMPTY       struct{}
	ErrLogLevel int = ERROR

	LogColor = map[int]string{FATAL: "\033[0m\033[37m",
		ERROR: "\033[0m\033[31m",
		WARN:  "\033[0m\033[33m",
		INFO:  "\033[0m\033[35m",
		DEBUG: "\033[0m\033[34m"}
	LogPrefix = map[int]string{
		FATAL: "[FATAL] ",
		ERROR: "[ERROR] ",
		WARN:  "[WARN] ",
		INFO:  "[INFO] ",
		DEBUG: "[DEBUG] ",
	}

	LogLevelWords map[string]int = map[string]int{"fatal": 0, "error": 1, "warn": 2, "info": 3, "debug": 4, "none": -1}
)

Functions

func CloseEnuf

func CloseEnuf(a, b float64) bool

take two floats, compare, need to be within 2%

func CloseInt

func CloseInt(a, b int) bool

take two ints, compare, need to be within 5%

func CoerceFloat

func CoerceFloat(v interface{}) (float64, error)

func CoerceFloatShort

func CoerceFloatShort(v interface{}) float64

func CoerceFloats

func CoerceFloats(v interface{}) []float64

func CoerceInt

func CoerceInt(v interface{}) (int, error)

func CoerceInt64

func CoerceInt64(v interface{}) (int64, error)

func CoerceInt64Short

func CoerceInt64Short(v interface{}) int64

func CoerceIntShort

func CoerceIntShort(v interface{}) int

func CoerceInts

func CoerceInts(v interface{}) []int

func CoerceString

func CoerceString(v interface{}) (string, error)

Coerce types (string,int,int64, float, []byte) into String type

func CoerceStringShort

func CoerceStringShort(v interface{}) string

Coerce type to string, returning zero length string if error or nil

func CoerceStrings

func CoerceStrings(v interface{}) []string

CoerceStrings Coerce type to strings, will split on comma by default.

func CoerceUint

func CoerceUint(v interface{}) (uint64, error)

Coerce a val(interface{}) into a Uint64

func CoerceUintShort

func CoerceUintShort(v interface{}) uint64

Coerce a Val(interface{}) into Uint64

func Debug

func Debug(v ...interface{})

Log at debug level

func DebugCtx

func DebugCtx(ctx context.Context, format string, v ...interface{})

Debug log formatted context writer

func DebugT

func DebugT(lineCt int)

func Debugf

func Debugf(format string, v ...interface{})

Debug log formatted

func DeleteJson

func DeleteJson(url, body string) (ret string, err error, resp *http.Response)

issues http delete an application/json to url with body

func DiscardStandardLogger

func DiscardStandardLogger()

Setup default log output to go to a dev/null

log.SetOutput(new(DevNull))

func DoLog

func DoLog(depth, logLvl int, msg string)

Low level log with depth , level, message and logger

func DoLogFields

func DoLogFields(depth, logLvl int, msg string, fields map[string]interface{})

DoLogFields allows the inclusion of additional context for logrus logs file and line number are included in the fields by default

func Error

func Error(v ...interface{})

Log at error level

func ErrorCtx

func ErrorCtx(ctx context.Context, format string, v ...interface{})

Error log formatted context writer

func Errorf

func Errorf(format string, v ...interface{})

Error log formatted

func EscapeNewlines

func EscapeNewlines(str string) string

Replace standard newline characters with escaped newlines so long msgs will remain one line.

func Fetch

func Fetch(url string) (ret []byte, err error)

Simple Fetch Wrapper, given a url it returns bytes

func FetchResp

func FetchResp(url string) (ret []byte, err error, resp *http.Response)

Simple Fetch Wrapper, given a url it returns bytes and response

func FromContext

func FromContext(ctx context.Context) string

FromContext extracts the Log Context prefix from context

func GetErrLogger

func GetErrLogger() *log.Logger

func GetLogger

func GetLogger() *log.Logger

func Info

func Info(v ...interface{})

Log at info level

func InfoCtx

func InfoCtx(ctx context.Context, format string, v ...interface{})

Info log formatted context writer

func InfoT

func InfoT(lineCt int)

Info Trace

func Infof

func Infof(format string, v ...interface{})

info log formatted

func IsJson

func IsJson(by []byte) bool

Determines if the bytes is a json array, only looks at prefix

not parsing the entire thing

func IsJsonArray

func IsJsonArray(by []byte) bool

Determines if the bytes is a json array, only looks at prefix

not parsing the entire thing

func IsJsonObject

func IsJsonObject(by []byte) bool

func IsTerminal

func IsTerminal() bool

Determine is this process is running in a Terminal or not?

func JsonString

func JsonString(v interface{}) string

func Log

func Log(logLvl int, v ...interface{})

Log to logger if setup

Log(ERROR, "message")

func LogCtx

func LogCtx(ctx context.Context, depth, logLvl int, format string, v ...interface{})

LogCtx Low level log with depth , level, message and logger

func LogD

func LogD(depth int, logLvl int, v ...interface{})

When you want to use the log short filename flag, and want to use the lower level logging functions (say from an *Assert* type function) you need to modify the stack depth:

    func init() {}
	       SetLogger(log.New(os.Stderr, "", log.Ltime|log.Lshortfile|log.Lmicroseconds), lvl)
    }

    func assert(t *testing.T, myData) {
        // we want log line to show line that called this assert, not this line
        LogD(5, DEBUG, v...)
    }

func LogErrorf

func LogErrorf(format string, v ...interface{}) error

Log this error, and return error object

func LogFieldsf

func LogFieldsf(logLvl int, fields map[string]interface{}, format string, v ...interface{})

func LogLevelSet

func LogLevelSet(levelWord string)

sets the log level from a string

func LogP

func LogP(logLvl int, prefix string, v ...interface{})

Log to logger if setup

LogP(ERROR, "prefix", "message", anyItems, youWant)

func LogPf

func LogPf(logLvl int, prefix string, format string, v ...interface{})

Log to logger if setup with a prefix

LogPf(ERROR, "prefix", "formatString %s %v", anyItems, youWant)

func LogThrottle

func LogThrottle(logLvl, limit int, format string, v ...interface{})

LogThrottleD logging based on @format as a key, such that key would never occur more than @limit times per hour

LogThrottle(u.ERROR, 1, "message %s", varx)

func LogThrottleCtx

func LogThrottleCtx(ctx context.Context, logLvl, limit int, format string, v ...interface{})

LogThrottleCtx log formatted context writer, limits to @limit/hour.

func LogThrottleD

func LogThrottleD(depth, logLvl, limit int, format string, v ...interface{})

LogThrottleD Throttle logging based on @format as a key, such that key would never occur more than @limit times per hour

LogThrottleD(5, u.ERROR, 1, "message %s", varx)

func LogThrottleKey

func LogThrottleKey(logLvl, limit int, key, format string, v ...interface{})

Throttle logging based on key, such that key would never occur more than @limit times per hour

LogThrottleKey(u.ERROR, 1,"error_that_happens_a_lot" "message %s", varx)

func LogThrottleKeyCtx

func LogThrottleKeyCtx(ctx context.Context, logLvl, limit int, key, format string, v ...interface{})

LogThrottleKeyCtx log formatted context writer

func LogTraceDf

func LogTraceDf(logLvl, lineCt int, format string, v ...interface{})

Log to logger if setup, grab a stack trace and add that as well

u.LogTracef(u.ERROR, "message %s", varx)

func LogTracef

func LogTracef(logLvl int, format string, v ...interface{})

Log to logger if setup, grab a stack trace and add that as well

u.LogTracef(u.ERROR, "message %s", varx)

func Logf

func Logf(logLvl int, format string, v ...interface{})

Logf to logger if setup

Logf(ERROR, "message %d", 20)

func MakeJsonList

func MakeJsonList(b []byte) []byte

Convert a slice of bytes into an array by ensuring it is wrapped

with []

func NewContext

func NewContext(ctx context.Context, msg string) context.Context

NewContext returns a new Context carrying contextual log message that gets prefixed to log statements.

func NewContextWrap

func NewContextWrap(ctx context.Context, msg string) context.Context

NewContextWrap returns a new Context carrying contextual log message that gets prefixed to log statements.

func NewUid

func NewUid() uint64

Create a new uint64 unique id

func PostForm

func PostForm(url, body string) (ret string, err error, resp *http.Response)

posts a www-form encoded form to url with body

func PostJson

func PostJson(postUrl string, data interface{}) (ret string, err error, resp *http.Response)

posts an application/json to url with body ie: type = application/json

func PrettyStack

func PrettyStack(lineCt int) string

PrettyStack is a helper to pull stack-trace, prettify it.

func PutJson

func PutJson(url, body string) (ret string, err error, resp *http.Response)

issues http put an application/json to url with optional body

func SetColorIfTerminal

func SetColorIfTerminal()

Setup colorized output if this is a terminal

func SetColorOutput

func SetColorOutput()

Setup colorized output

func SetCustomLogger

func SetCustomLogger(cl LoggerCustom)

SetCustomLogger sets the logger to a custom logger

func SetErrLogger

func SetErrLogger(l *log.Logger, logLevel string)

you can set a logger, and log level. this is for errors, and assumes you are logging to Stderr (seperate from stdout above), allowing you to seperate debug&info logging from errors

	gou.SetLogger(log.New(os.Stderr, "", log.LstdFlags), "debug")

 loglevls:   debug, info, warn, error, fatal

func SetEscapeNewlines

func SetEscapeNewlines(en bool)

Set whether to escape newline characters in log messages

func SetLogger

func SetLogger(l *log.Logger, logLevel string)

you can set a logger, and log level,most common usage is:

	gou.SetLogger(log.New(os.Stdout, "", log.LstdFlags), "debug")

 loglevls:   debug, info, warn, error, fatal

Note, that you can also set a separate Error Log Level

func SetStopper

func SetStopper(f func())

Use this in combo with StopCheck() for test functions that must start processes such as

func SetupLogging

func SetupLogging(lvl string)

Setup default logging to Stderr, equivalent to:

gou.SetLogger(log.New(os.Stderr, "", log.Ltime|log.Lshortfile), "debug")

func SetupLoggingFile

func SetupLoggingFile(f *os.File, lvl string)

SetupLoggingFile writes logs to the file object parameter.

func SetupLoggingLong

func SetupLoggingLong(lvl string)

Setup default logging to Stderr, equivalent to:

gou.SetLogger(log.New(os.Stderr, "", log.LstdFlags|log.Lshortfile|log.Lmicroseconds), level)

func StartTest

func StartTest()

func StopCheck

func StopCheck()

func WaitFor

func WaitFor(check func() bool, timeoutSecs int)

Wait for condition (defined by func) to be true this is mostly for testing, but a utility to create a ticker checking back every 100 ms to see if something (the supplied check func) is done

WaitFor(func() bool {
   return ctr.Ct == 0
},10)

timeout (in seconds) is the last arg

func Warn

func Warn(v ...interface{})

Log at warn level

func WarnCtx

func WarnCtx(ctx context.Context, format string, v ...interface{})

Warn log formatted context writer

func WarnT

func WarnT(lineCt int)

Warn Trace

func Warnf

func Warnf(format string, v ...interface{})

Warn log formatted

Types

type DevNull

type DevNull struct{}

http://play.golang.org/p/5LIA41Iqfp Dummy discard, satisfies io.Writer without importing io or os.

func (DevNull) Write

func (DevNull) Write(p []byte) (int, error)

type JsonHelper

type JsonHelper map[string]interface{}

A wrapper around a map[string]interface{} to facilitate coercion of json data to what you want

allows usage such as this

jh := NewJsonHelper([]byte(`{
	"name":"string",
	"ints":[1,5,9,11],
	"int":1,
	"int64":1234567890,
	"MaxSize" : 1048576,
	"strings":["string1"],
	"nested":{
		"nest":"string2",
		"strings":["string1"],
		"int":2,
		"list":["value"],
		"nest2":{
			"test":"good"
		}
	},
	"nested2":[
		{"sub":5}
	]
}`)

i := jh.Int("nested.int")  // 2
i2 := jh.Int("ints[1]")    // 5   array position 1 from [1,5,9,11]
s := jh.String("nested.nest")  // "string2"

func JsonHelperHttp

func JsonHelperHttp(method, urlStr string, data interface{}) (JsonHelper, error)

Simple Fetch Wrapper, given a url it returns Helper, error Sends as type application/json, interprets whatever datatype is sent in appropriately

func NewJsonHelper

func NewJsonHelper(b []byte) JsonHelper

func NewJsonHelperFromResp

func NewJsonHelperFromResp(resp *http.Response) (JsonHelper, error)

Make a JsonHelper from http response. This will automatically close the response body

func NewJsonHelperMapString

func NewJsonHelperMapString(m map[string]string) JsonHelper

func NewJsonHelperReader

func NewJsonHelperReader(r io.Reader) (jh JsonHelper, err error)

func NewJsonHelpers

func NewJsonHelpers(b []byte) []JsonHelper

func (JsonHelper) Bool

func (j JsonHelper) Bool(n string) bool

func (JsonHelper) BoolSafe

func (j JsonHelper) BoolSafe(n string) (val bool, ok bool)

func (JsonHelper) Float64

func (j JsonHelper) Float64(n string) float64

func (JsonHelper) Float64Safe

func (j JsonHelper) Float64Safe(n string) (float64, bool)

func (JsonHelper) Get

func (j JsonHelper) Get(n string) interface{}

Get the key (or keypath) value as interface, mostly used internally through String, etc methods

jh.Get("name.subname")
jh.Get("name/subname")
jh.Get("name.arrayname[1]")
jh.Get("name.arrayname[]")

func (*JsonHelper) GobDecode

func (j *JsonHelper) GobDecode(data []byte) error

GobDecode overwrites the receiver, which must be a pointer, with the value represented by the byte slice, which was written by GobEncode, usually for the same concrete type. GobDecode([]byte) error

func (*JsonHelper) GobEncode

func (j *JsonHelper) GobEncode() ([]byte, error)

func (JsonHelper) HasKey

func (j JsonHelper) HasKey(name string) bool

func (JsonHelper) Helper

func (j JsonHelper) Helper(n string) JsonHelper

Get a Helper from a string path

func (JsonHelper) Helpers

func (j JsonHelper) Helpers(n string) []JsonHelper

Get list of Helpers at given name. Trys to coerce into proper Helper type

func (JsonHelper) Int

func (j JsonHelper) Int(n string) int

func (JsonHelper) Int64

func (j JsonHelper) Int64(n string) int64

func (JsonHelper) Int64Safe

func (j JsonHelper) Int64Safe(n string) (int64, bool)

func (JsonHelper) IntSafe

func (j JsonHelper) IntSafe(n string) (int, bool)

func (JsonHelper) Ints

func (j JsonHelper) Ints(n string) []int

func (JsonHelper) Keys

func (j JsonHelper) Keys() []string

func (JsonHelper) List

func (j JsonHelper) List(n string) []interface{}

Gets slice of interface{}

func (JsonHelper) Map

func (j JsonHelper) Map(n string) map[string]interface{}

func (JsonHelper) MapSafe

func (j JsonHelper) MapSafe(n string) (map[string]interface{}, bool)

func (JsonHelper) PrettyJson

func (j JsonHelper) PrettyJson() []byte

func (JsonHelper) String

func (j JsonHelper) String(n string) string

func (JsonHelper) StringSafe

func (j JsonHelper) StringSafe(n string) (string, bool)

func (JsonHelper) Strings

func (j JsonHelper) Strings(n string) []string

func (JsonHelper) Uint64

func (j JsonHelper) Uint64(n string) uint64

func (JsonHelper) Uint64Safe

func (j JsonHelper) Uint64Safe(n string) (uint64, bool)

type JsonRawWriter

type JsonRawWriter struct {
	bytes.Buffer
}

func (*JsonRawWriter) MarshalJSON

func (m *JsonRawWriter) MarshalJSON() ([]byte, error)

func (*JsonRawWriter) Raw

func (m *JsonRawWriter) Raw() json.RawMessage

type LoggerCustom

type LoggerCustom interface {
	Log(depth, logLevel int, msg string, fields map[string]interface{})
}

LoggerCustom defines custom interface for logger implementation

func GetCustomLogger

func GetCustomLogger() LoggerCustom

GetCustomLogger returns the custom logger if initialized

type Throttler

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

func NewThrottler

func NewThrottler(max int, per time.Duration) *Throttler

new Throttler that will tell you to limit or not based on given @max events @per duration

func (*Throttler) Throttle

func (r *Throttler) Throttle() (bool, int32)

Should we limit this because we are above rate? Returns a bool of whether to throttle the message, and a count of previous log messages throttled since last log message.

func (*Throttler) ThrottleAdd

func (r *Throttler) ThrottleAdd(ct int32) (bool, int32)

Should we limit this because we are above rate? Returns a bool of whether to throttle the message, and a count of previous log messages throttled since last log message.

func (*Throttler) ThrottleCount

func (r *Throttler) ThrottleCount() int32

type Uid

type Uid uint64

uid is a 64 bit int uid

func (*Uid) String

func (u *Uid) String() string

Jump to

Keyboard shortcuts

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