mindbenders
Logger
type ILogWriter interface {
WriteLogs(context.Context, Fields, Level, string)
}
//ILogger ...
type IDotpeLogger interface {
ILogWriter
GinLogger() gin.HandlerFunc
}
In order to initialize one has to have pass the 2 things for sure.
-
logger.WithAppInfo(os.Getenv("APP"))
, definition is as follows
func WithAppInfo(app string) Option {
return func(dlogger *dlogger) {
dlogger.app = app
}
}
-
logger.WithHookContainer(hookContainer)
, definition is as follows
//.....
type IHookContainer interface {
GetHook() (Hook, error)
}
//.....
func WithHookContainer(hookContainer IHookContainer) Option {
hook, err := hookContainer.getHook()
if err != nil {
return nil
}
return WithHook(hook)
}
Hook
-
Elastic hook container
logconf = logger.NewKibanaConfig(url, key, secret, os.Getenv("APP"), "")
-
File Hook container
logconf = logger.NewKibanaConfig(url, key, secret, os.Getenv("APP"), "")
logger initializations
Usage [link]
package utils
import (
"os"
mbinterfaces "gitlab.com/dotpe/mindbenders/interfaces"
logger "gitlab.com/dotpe/mindbenders/logging"
)
var DLogger mbinterfaces.IDotpeLogger
//write your logic to initialize or fetch hookContainer objectct
var hookContainer logger.IHookContainer
// InitLogger ..
DLogger, err := logger.Init(
logger.WithAppInfo(os.Getenv("APP")),
logger.WithHookContainer(hookContainer),
logger.WithAccessLogOptions(
logger.AccessLogOptionRequestBody,
aopt1, // check definition of aopt1, similary you can pass more functions as you need
),
logger.WithLogOptions(opt1),// check definition of opt1, similary you can pass more functions as you need
)
func aopt1(c *gin.Context, fields *Fields) {
c.Set("ip", c.ClientIP())
}
func opt1(ctx context.Context, fields *Fields) {
(*fields)["clientIP"] = ctx.Value("ip")
}
attaching GinLogger to add accessLog
apiGroup.Use(utils.DLogger.GinLogger())
Recovery Middleware
The call ginmiddleware.Recovery(utils.DLogger)
is for making the gin-engine failure safe.
Note: you can't control crashes in orphened go-routines