Skip to content

Latest commit

 

History

History
194 lines (125 loc) · 4.01 KB

readme.md

File metadata and controls

194 lines (125 loc) · 4.01 KB

Logella

A simple loggers and errors library.



Logger Package

This package defines the log settings (zerolog). Allows you to use default or customized colors configuration.

Import

import "github.com/Lucasvmarangoni/logella/config/log"

ConfigDefault: It has pre-defined log color settings, meaning there is no need to specify a any parameter.

logger.ConfigDefault()

ConfigCustom: Allows you to customize log level, message, and operation colors.

ConfigCustom(info, err, warn, debug, fatal, message, operation colors)

The parameters must be passed using the variables already defined by the package with the name of the colors.

Colors: Black, Red, Green, Yellow, Blue, Magenta, Cyan or White.

Example:

logger.ConfigCustom(Green, Red, Yellow, Cyan, Red, Magenta, Blue)



Errors Package

The Errors package is a custom error handling library. Its primary feature is to attach contextual information to errors, allowing them to be propagated up the call stack.

It also provides standardized error types, such as invalid and required.

Import

import "github.com/Lucasvmarangoni/logella/err"

ErrCtx: ErrCtx is used to add the error and the operation that triggered the exception. The operations stack is not returned by ErrCtx, but rather persisted.

GetOperations: GetOperations is used to retrieve only the operations stack.

ErrStack: ErrStack returns the error along with the operations stack.

Alt text

Use

errors.ErrCtx(err, "repo.InitTables")
errors.GetOperations()
errors.ErrStack()

ErrCtx(err error, value string)

This function creates an error with context. Here are examples of how to use it:

cfg.Db, err = pgx.ParseConfig(url)
if err != nil {
    return nil, errors.ErrCtx(err, "pgx.ParseConfig(url)")
}
dbConnection, err := db.Connect(ctx)
if err != nil {
    return nil, errors.ErrCtx(err, "db.Connect")
}

The output of this function includes a field named "Operation", which is the main feature of this package.

Standard Errors

The package provides standardized errors, such as IsInvalidError and IsRequiredError. Here's an example of how to use IsInvalidError:

errors.IsInvalidError("Customer", "Must be google uuid")
  • IsInvalidError(fieldName, msg string) error
  • IsRequiredError(fieldName, msg string) error
  • FailOnErrLog(err error, msg string)
  • PanicErr(err error, ctx string)
  • PanicBool(boolean bool, msg string)



Router Package

The Router is a logging package for initializing routes using go-chi.

Alt text

Import

import "github.com/Lucasvmarangoni/logella/router"

Use

router := router.NewRouter()

Instance Creation

To create a new instance of the Router, use the NewRouter function:

func NewRouter() *Router {
    ro := &Router{}    
    return ro
}

InitializeRoute Function

The InitializeRoute function is the main function of the Router package. It takes a chi router, a path, and a handler function as arguments:

func (ro *Router) InitializeRoute(r chi.Router, path string, handler http.HandlerFunc)
router.InitializeRoute(r, "/jwt", u.userHandler.Authentication)

Method Function

The Method function sets the HTTP method for the route. It accepts a string argument, which can be either uppercase or lowercase:

func (ro *Router) Method(m string) *Router
router.Method("POST").InitializeRoute(r, "/jwt", u.userHandler.Authentication)

Prefix Function

The Prefix function sets a prefix for the route, if there is one:

func (ro *Router) Prefix(p string) *Router 
router.Method("POST").Prefix("/authn").InitializeRoute(r, "/jwt", u.userHandler.Authentication)