-
Notifications
You must be signed in to change notification settings - Fork 6
1. How To Use Customized Handlers In Config File
水不要鱼 edited this page May 10, 2020
·
2 revisions
If you want to use customized handlers in your config file, just register them to logit.
Demo:
package main
import (
"fmt"
"github.com/FishGoddess/logit"
)
// **************************** customized handler ****************************
type MyHandler struct{
prefix string
}
func (mh *MyHandler) Handle(log *logit.Log) bool {
// The service of handler
// Print message of log here
fmt.Println(mh.prefix, "I am handling this log:", log.Msg())
return true
}
// *************************************************************************
func main() {
// Register this handler to logit
logit.RegisterHandler("my-handler", func(params map[string]interface{}) logit.Handler {
return &MyHandler{
// The "prefix" in config file will be injected to params automatically by logit
prefix: params["prefix"].(string),
}
})
// Create a logger by config file
// The content of config file:
// "handlers": {
// "my-handler": {
// # logit will inject this parameter into params
// "prefix": "[prefix...]"
// }
// }
logger := logit.NewLoggerFromPath("./logger.conf")
logger.Info("real message!")
}
The content of config file: (Notice that the filename is "logger.conf")
"handlers": {
"my-handler": {
# logit will inject this parameter into params
"prefix": "[prefix...]"
}
}
Check _examples/handler.go to know more information.
Check source code handler_extension.go to know how logit registers provided handlers.
More information about config in _examples/config.