logging/logging.go

50 lines
780 B
Go
Raw Normal View History

2017-08-30 04:03:03 +00:00
package logging
import (
2017-09-11 03:42:00 +00:00
"encoding/json"
2017-09-13 03:14:20 +00:00
"flag"
2017-09-13 03:22:05 +00:00
"log"
2017-09-11 03:42:00 +00:00
"git.loafle.net/commons_go/config"
2017-08-30 04:03:03 +00:00
2017-08-30 05:00:22 +00:00
"go.uber.org/zap"
2017-08-30 04:03:03 +00:00
)
type loggerKeyType int
const loggerKey loggerKeyType = iota
2017-09-11 03:42:00 +00:00
var Logger *zap.Logger
2017-08-30 04:03:03 +00:00
func init() {
2017-09-13 03:14:20 +00:00
configPath := flag.String("config", ".", "The path of config")
flag.Parse()
2017-09-13 03:22:05 +00:00
log.Printf("logging: config path1[%s]", *configPath)
2017-09-13 03:14:20 +00:00
2017-09-11 03:42:00 +00:00
conf := config.New()
conf.SetConfigName("logging")
conf.SetConfigType("json")
2017-09-13 03:14:20 +00:00
conf.AddConfigPath(*configPath)
// conf.AddConfigPath("./config")
2017-09-11 03:42:00 +00:00
err := conf.ReadInConfig()
if nil != err {
panic(err)
2017-08-30 05:00:22 +00:00
}
2017-08-30 04:03:03 +00:00
2017-09-11 03:42:00 +00:00
buf, err := conf.Marshal("json")
if err != nil {
panic(err)
2017-08-30 04:03:03 +00:00
}
2017-09-11 03:42:00 +00:00
var cfg zap.Config
if err = json.Unmarshal(buf, &cfg); err != nil {
panic(err)
2017-08-30 04:03:03 +00:00
}
2017-09-11 03:42:00 +00:00
Logger, err = cfg.Build()
if err != nil {
panic(err)
}
2017-08-30 04:03:03 +00:00
}