golog

package module
v1.4.3 Latest Latest
Warning

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

Go to latest
Published: Aug 3, 2023 License: MIT Imports: 7 Imported by: 1

README

go-log Tests codecov

Docs live at mathbalduino.com.br/go-log


Matheus Leonel Balduino

Everywhere, under @mathbalduino

Documentation

Index

Constants

View Source
const (
	// LvlTrace is a flag that if used will enable
	// Trace logs (via Logger Trace method)
	LvlTrace uint64 = 1 << iota

	// LvlDebug is a flag that if used will enable
	// Debug logs (via Logger Debug method)
	LvlDebug

	// LvlInfo is a flag that if used will enable
	// Info logs (via Logger Info method)
	LvlInfo

	// LvlWarn is a flag that if used will enable
	// Warn logs (via Logger Warn method)
	LvlWarn

	// LvlError is a flag that if used will enable
	// Error logs (via Logger Error method)
	LvlError

	// LvlFatal is a flag that if used will enable
	// Fatal logs (via Logger Fatal method)
	LvlFatal
)
View Source
const (
	// LvlProduction will enable Info, Warn, Error and Fatal logs
	// (excluding only Debug and Trace)
	LvlProduction = LvlInfo | LvlWarn | LvlError | LvlFatal

	// LvlDefaults will enable Debug and all LvlProduction logs
	// (excluding only Trace)
	LvlDefaults = LvlDebug | LvlProduction

	// LvlAll will enable all logs
	LvlAll = LvlTrace | LvlDefaults
)
View Source
const DefaultErrorKey = "error"

DefaultErrorKey is the key used to store the errors returned by the DefaultErrorParser, inside the fields map

View Source
const DefaultLvlKey = "lvl"

DefaultLvlKey is the key used to store the level of the log, inside the fields maps

View Source
const DefaultMsgKey = "msg"

DefaultMsgKey is the key used to store the message of the log, inside the fields maps

Variables

View Source
var ErrLvlMsgSameKey = fmt.Errorf("the 'level' and 'message' keys cannot be equal")

ErrLvlMsgSameKey is an error token used to represent the situation where the given Configuration struct contains the 'LvlFieldName' and 'MsgFieldName' fields with the same value

View Source
var ErrNilChan = fmt.Errorf("the 'channel' argument cannot be nil")

ErrNilChan is thrown by AsyncHandleLog when it receives a nil channel via the function argument

View Source
var ErrNilCtx = fmt.Errorf("the 'context.Context' argument cannot be nil")

ErrNilCtx is thrown by AsyncHandleLog when it receives a nil context via the function argument

View Source
var ErrNilErrorParser = fmt.Errorf("the 'ErrorParser' cannot be nil")

ErrNilErrorParser is an error token used to represent that the given ErrorParser is nil and if it's accepted, a nil pointer can happen

View Source
var ErrNilWaitGroup = fmt.Errorf("the 'WaitGroup' argument cannot be nil")

ErrNilWaitGroup is thrown by AsyncHandleLog when it receives a nil WaitGroup via the function argument

Functions

func AsyncHandleLog

func AsyncHandleLog(ctx context.Context, c <-chan Log, wg WaitGroup) error

AsyncHandleLog will wait on the given send-only channel, and forwarding any received Log to the internal "handleLog" function.

Note that this function must be used to implement custom async strategies, since it's the only way to access the internal "handleLog" function

func BoldRedString

func BoldRedString(msg string) string

BoldRedString will wrap the given string between the bold red and reset ANSI codes.

Terminals with ANSI code support will print the string to the screen using bold red as the font color

func ColorizeStrByLvl

func ColorizeStrByLvl(lvl uint64, msg string) string

ColorizeStrByLvl will colorize the log msg using the ANSI color code associated with the log level

func CyanString

func CyanString(msg string) string

CyanString will wrap the given string between the cyan and reset ANSI codes.

Terminals with ANSI code support will print the string to the screen using cyan as the font color

func DarkGreyString

func DarkGreyString(msg string) string

DarkGreyString will wrap the given string between the dark grey and reset ANSI codes.

Terminals with ANSI code support will print the string to the screen using dark grey as the font color

func Debug

func Debug(msg string, adHocFields ...LogFields)

Debug will create a new log with the Debug level, if enabled, applying the given adHocFields.

func Error

func Error(msg string, adHocFields ...LogFields)

Error will create a new log with the Error level, if enabled, applying the given adHocFields.

func ErrorFrom

func ErrorFrom(e error, adHocFields ...LogFields)

ErrorFrom will create a new log with the Error level, if enabled, using the given error and adHocFields.

func Fatal

func Fatal(msg string, adHocFields ...LogFields)

Fatal will create a new log with the Fatal level, if enabled, applying the given adHocFields.

Remember that this method will not call "panic"

func FatalFrom

func FatalFrom(e error, adHocFields ...LogFields)

FatalFrom will create a new log with the Fatal level, if enabled, using the given error and adHocFields.

Remember that this method will not call "panic"

func Info

func Info(msg string, adHocFields ...LogFields)

Info will create a new log with the Info level, if enabled, applying the given adHocFields.

func LightGreyString

func LightGreyString(msg string) string

LightGreyString will wrap the given string between the light grey and reset ANSI codes.

Terminals with ANSI code support will print the string to the screen using light grey as the font color

func LvlToString

func LvlToString(lvl uint64) string

LvlToString will take the given log level and return the string that represents it

Note that this function can only translate default log levels

func OutputAnsiToStdout

func OutputAnsiToStdout(lvl uint64, msg string, _ LogFields)

OutputAnsiToStdout will take some log and write it to the os.Stdout, using ANSI codes to colorize it accordingly to the log level

func OutputPanicOnFatal

func OutputPanicOnFatal(lvl uint64, msg string, fields LogFields)

OutputPanicOnFatal will call "panic" if the lvl of the given log is LvlFatal, using the "error" interface inside the fields as argument, if present. Otherwise, the log msg string will be used to create a new error (using fmt.Errorf)

func RedString

func RedString(msg string) string

RedString will wrap the given string between the red and reset ANSI codes.

Terminals with ANSI code support will print the string to the screen using red as the font color

func Trace

func Trace(msg string, adHocFields ...LogFields)

Trace will create a new log with the Trace level, if enabled, applying the given adHocFields.

func Warn

func Warn(msg string, adHocFields ...LogFields)

Warn will create a new log with the Warn level, if enabled, applying the given adHocFields.

func YellowString

func YellowString(msg string) string

YellowString will wrap the given string between the yellow and reset ANSI codes.

Terminals with ANSI code support will print the string to the screen using yellow as the font color

Types

type AsyncScheduler

type AsyncScheduler interface {
	// NextChannel must return a valid, non-nil,
	// receive-only channel
	NextChannel() chan<- Log

	// Shutdown must send a signal (and wait response)
	// to the running go routines, exiting them
	Shutdown()
}

AsyncScheduler is used as a source of channels that are used to send new Logs to worker goroutines, handling Logs in an asynchronous way

func DefaultAsyncScheduler

func DefaultAsyncScheduler(nGoRoutines uint64, chanCap uint64) AsyncScheduler

DefaultAsyncScheduler will create one channel by goroutine, with the given capacity, and setup a goroutine that will handle newly created Logs.

Note that if nGoRoutines is zero, nothing happens and the returned AsyncScheduler will be nil

type Configuration

type Configuration struct {
	// AsyncScheduler will be used to distinguish
	// between a sync (if nil) or async (if not nil)
	// approach
	AsyncScheduler AsyncScheduler

	// LvlFieldName is used to customize the name of
	// the key that represents the level of the Log
	//
	// Note that it cannot be equal to the MsgFieldName
	LvlFieldName string

	// MsgFieldName is used to customize the name of
	// the key that represents the message of the Log
	//
	// Note that it cannot be equal to the LvlFieldName
	MsgFieldName string

	// LvlsEnabled is an integer that represents which
	// Log levels are enabled.
	//
	// Note that it is intended to be used as a combination
	// ("or" bitwise operation) of log levels
	LvlsEnabled uint64

	// ErrorParser is a function that takes an error and
	// return a msg string and an optional collection of fields
	// (used by ErrorFrom and FatalFrom methods)
	ErrorParser func(error) (string, LogFields)
}

Configuration is a struct that holds global src configurations

func DefaultConfig

func DefaultConfig() Configuration

DefaultConfig creates a default Logger configuration, with a synchronous approach (nil AsyncScheduler), omitting only Trace logs, using "lvl" and "msg" as LvlFieldName and MsgFieldName, respectively, and extracting only the error message via 'ErrorParser'

type Hooks

type Hooks = map[string]func(Log) interface{}

Hooks is just an alias to a map that represents dynamic fields of the log (values are evaluated by calling the function)

type Log

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

Log is a struct that represents a newly created log

It is intended to be internal only, exported just to allow outside type reference

func (Log) Field

func (l Log) Field(key string) interface{}

Field will return the value associated with the given key

Note that if this method is called in pre hooks, the adHoc and post fields aren't ready yet, so the returned value may be overridden by them. If called in post hook N, it can only see the result of the application of the N-1 previous post hooks. The returned value can be overridden by the N+1 next post hooks

Pay attention

type LogFields

type LogFields = map[string]interface{}

LogFields is just an alias to a map that represents the fields of the log

func DefaultErrorParser

func DefaultErrorParser(err error) (string, LogFields)

DefaultErrorParser will return a tuple containing the error string and the following map: { "error": err }

type Logger

type Logger interface {
	// Field acts as a getter for base fields.
	// Note that this method will ignore preHooks/postHooks
	Field(key string) interface{}

	// Fields will append the given LogFields to the Logger
	// base fields, overriding old base fields (if there's a
	// key clash) and returning a new Logger instance
	Fields(fields LogFields) Logger

	// RawFields will set the given LogFields directly to the
	// Logger base fields, discarding all the previous base
	// fields and returning a new Logger instance
	RawFields(fields LogFields) Logger

	// PreHooks will append the given Hooks to the Logger
	// preHooks, overriding old preHooks (if there's a key
	// clash) and returning a new Logger instance
	PreHooks(hooks Hooks) Logger

	// RawPreHooks will set the given Hooks to the Logger
	// preHooks, discarding all the previous preHooks and
	// returning a new Logger instance
	RawPreHooks(hooks Hooks) Logger

	// PostHooks will append the given Hooks to the Logger
	// postHooks, overriding old postHooks (if there's a key
	// clash) and returning a new Logger instance
	PostHooks(hooks Hooks) Logger

	// RawPostHooks will set the given Hooks to the Logger
	// postHooks, discarding all the previous postHooks and
	// returning a new Logger instance
	RawPostHooks(hooks Hooks) Logger

	// Outputs will append the given Output params (including
	// the variadic ones), in the given order, to the Logger
	// outputs and return a new Logger instance
	Outputs(output Output, outputs ...Output) Logger

	// RawOutputs will set the given Output params (including
	// the variadic ones), in the given order, to the Logger
	// outputs, discarding all the old values and returning a
	// new Logger instance
	RawOutputs(output Output, outputs ...Output) Logger

	// Trace will create a new log with the Trace level, if enabled,
	// applying the given adHocFields.
	Trace(msg string, adHocFields ...LogFields)

	// Debug will create a new log with the Debug level, if enabled,
	// applying the given adHocFields.
	Debug(msg string, adHocFields ...LogFields)

	// Info will create a new log with the Info level, if enabled,
	// applying the given adHocFields.
	Info(msg string, adHocFields ...LogFields)

	// Warn will create a new log with the Warn level, if enabled,
	// applying the given adHocFields.
	Warn(msg string, adHocFields ...LogFields)

	// Error will create a new log with the Error level, if enabled,
	// applying the given adHocFields.
	Error(msg string, adHocFields ...LogFields)

	// Fatal will create a new log with the Fatal level, if enabled,
	// applying the given adHocFields.
	//
	// Remember that this method will not call "panic"
	Fatal(msg string, adHocFields ...LogFields)

	// ErrorFrom will create a new log with the Error level, if enabled,
	// using the given error and adHocFields.
	ErrorFrom(e error, adHocFields ...LogFields)

	// FatalFrom will create a new log with the Fatal level, if enabled,
	// using the given error and adHocFields.
	//
	// Remember that this method will not call "panic"
	FatalFrom(e error, adHocFields ...LogFields)

	// Log is the base method that handles new logs creation, being used
	// by all other log methods (Trace, Debug, Warn, ...). If there's the
	// need to create custom log levels, you must call this method.
	Log(lvl uint64, msg string, adHocFields []LogFields)

	// Configuration will set the given Configuration as the new one.
	// Note that every Logger created before the one that called this
	// method will point to the new Configuration
	Configuration(c Configuration)
}

func New

func New(config Configuration) Logger

New creates a new Logger instance, validating the given configuration

Note that if the given configuration is invalid, panic will be called with the proper errors

func NewDefault

func NewDefault() Logger

NewDefault will create a basic sync Logger instance that outputs newly created logs to the terminal (using ANSI codes), using 'lvl' and 'msg' as level and message keys and enabling only the default log levels

type Output

type Output = func(lvl uint64, msg string, fields LogFields)

Output is just an alias to a function that is intended to handle newly created logs and persist/print it to somewhere

func OutputJsonToWriter

func OutputJsonToWriter(w io.Writer, onError func(error)) Output

OutputJsonToWriter will parse the log fields to JSON and write it to the given Writer interface, calling onError with any errors that occur between the Marshal()/Write() call

func OutputToWriter

func OutputToWriter(w io.Writer, parser OutputParser, onError func(error)) Output

OutputToWriter will call the given parser and write the returned values to the given io.Writer, calling 'onError' with any errors that occur

type OutputParser

type OutputParser = func(LogFields) ([]byte, error)

OutputParser is just an alias to a function that is used to parse the LogFields into a byte array, in order to write it to a file, etc, idk...

type WaitGroup

type WaitGroup interface {
	Wait()
	Done()
	Add(int)
}

WaitGroup is an interface used just to ease tests.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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