This package defines the log settings (zerolog). Allows you to use default or customized colors configuration.
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)
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 "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.
errors.ErrCtx(err, "repo.InitTables")
errors.GetOperations()
errors.ErrStack()
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.
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)
The Router
is a logging package for initializing routes using go-chi.
import "github.com/Lucasvmarangoni/logella/router"
router := router.NewRouter()
To create a new instance of the Router
, use the NewRouter
function:
func NewRouter() *Router {
ro := &Router{}
return ro
}
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)
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)
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)