diff --git a/README.md b/README.md index d52926a..7197ca0 100644 --- a/README.md +++ b/README.md @@ -135,7 +135,7 @@ type HelloServiceApiHandler struct { // 对象自定义初始化 func (h *HelloServiceApiHandler) Init() error { - h.log = logger.Sub("helloworld.api") + h.log = log.Sub("helloworld.api") return nil } diff --git a/client/rest/client.go b/client/rest/client.go index 69ec58b..d07dfd4 100644 --- a/client/rest/client.go +++ b/client/rest/client.go @@ -10,7 +10,7 @@ import ( "github.com/infraboard/mcube/v2/client/negotiator" "github.com/infraboard/mcube/v2/flowcontrol" "github.com/infraboard/mcube/v2/flowcontrol/tokenbucket" - "github.com/infraboard/mcube/v2/ioc/config/logger" + "github.com/infraboard/mcube/v2/ioc/config/log" "github.com/rs/zerolog" "go.opentelemetry.io/otel/propagation" oteltrace "go.opentelemetry.io/otel/trace" @@ -26,7 +26,7 @@ func NewRESTClient() *RESTClient { return &RESTClient{ rateLimiter: tokenbucket.NewBucketWithRate(10, 10), client: client, - log: logger.Sub("client.rest"), + log: log.Sub("client.rest"), headers: NewDefaultHeader(), transport: transport, } diff --git a/client/rest/request.go b/client/rest/request.go index 90bad1d..3aadc25 100644 --- a/client/rest/request.go +++ b/client/rest/request.go @@ -15,7 +15,7 @@ import ( "github.com/infraboard/mcube/v2/flowcontrol" "github.com/infraboard/mcube/v2/flowcontrol/tokenbucket" "github.com/infraboard/mcube/v2/http/queryparams" - "github.com/infraboard/mcube/v2/ioc/config/logger" + "github.com/infraboard/mcube/v2/ioc/config/log" "github.com/rs/zerolog" "go.opentelemetry.io/contrib/instrumentation/net/http/httptrace/otelhttptrace" "go.opentelemetry.io/otel/trace" @@ -33,7 +33,7 @@ func NewRequest(c *RESTClient) *Request { authType: c.authType, user: c.user, token: c.token, - log: logger.Sub("http.request"), + log: log.Sub("http.request"), } return r diff --git a/client/rest/response.go b/client/rest/response.go index c7ecaf3..091fefa 100644 --- a/client/rest/response.go +++ b/client/rest/response.go @@ -9,13 +9,13 @@ import ( "github.com/infraboard/mcube/v2/client/compressor" "github.com/infraboard/mcube/v2/client/negotiator" - "github.com/infraboard/mcube/v2/ioc/config/logger" + "github.com/infraboard/mcube/v2/ioc/config/log" "github.com/rs/zerolog" ) func NewResponse(c *RESTClient) *Response { return &Response{ - log: logger.Sub("http.response"), + log: log.Sub("http.response"), } } diff --git a/cmd/module/templates/book/api/http.go.gin.tpl b/cmd/module/templates/book/api/http.go.gin.tpl index 567248e..c57c613 100644 --- a/cmd/module/templates/book/api/http.go.gin.tpl +++ b/cmd/module/templates/book/api/http.go.gin.tpl @@ -15,11 +15,11 @@ var ( type handler struct { service book.ServiceServer - log logger.Logger + log log.Logger } func (h *handler) Config() error { - h.log = logger.Sub(book.AppName) + h.log = log.Sub(book.AppName) h.service = app.GetGrpcApp(book.AppName).(book.ServiceServer) return nil } diff --git a/cmd/module/templates/book/api/http.go.go-restful.tpl b/cmd/module/templates/book/api/http.go.go-restful.tpl index aa73c90..c8ef1f6 100644 --- a/cmd/module/templates/book/api/http.go.go-restful.tpl +++ b/cmd/module/templates/book/api/http.go.go-restful.tpl @@ -17,11 +17,11 @@ var ( type handler struct { service book.ServiceServer - log logger.Logger + log log.Logger } func (h *handler) Config() error { - h.log = logger.Sub(book.AppName) + h.log = log.Sub(book.AppName) h.service = app.GetGrpcApp(book.AppName).(book.ServiceServer) return nil } diff --git a/cmd/module/templates/book/api/http.go.httprouter.tpl b/cmd/module/templates/book/api/http.go.httprouter.tpl index 9cd1663..4e5e5c3 100644 --- a/cmd/module/templates/book/api/http.go.httprouter.tpl +++ b/cmd/module/templates/book/api/http.go.httprouter.tpl @@ -16,11 +16,11 @@ var ( type handler struct { service book.ServiceServer - log logger.Logger + log log.Logger } func (h *handler) Config() error { - h.log = logger.Sub(book.AppName) + h.log = log.Sub(book.AppName) h.service = app.GetGrpcApp(book.AppName).(book.ServiceServer) return nil } diff --git a/cmd/module/templates/book/api/http.go.tpl b/cmd/module/templates/book/api/http.go.tpl index 9cd1663..4e5e5c3 100644 --- a/cmd/module/templates/book/api/http.go.tpl +++ b/cmd/module/templates/book/api/http.go.tpl @@ -16,11 +16,11 @@ var ( type handler struct { service book.ServiceServer - log logger.Logger + log log.Logger } func (h *handler) Config() error { - h.log = logger.Sub(book.AppName) + h.log = log.Sub(book.AppName) h.service = app.GetGrpcApp(book.AppName).(book.ServiceServer) return nil } diff --git a/cmd/module/templates/book/impl/impl.go.tpl b/cmd/module/templates/book/impl/impl.go.tpl index c37cdee..2ef69e4 100644 --- a/cmd/module/templates/book/impl/impl.go.tpl +++ b/cmd/module/templates/book/impl/impl.go.tpl @@ -31,7 +31,7 @@ type service struct { {{ if $.EnableMongoDB -}} col *mongo.Collection {{- end }} - log logger.Logger + log log.Logger book.UnimplementedServiceServer } @@ -51,7 +51,7 @@ func (s *service) Config() error { s.col = db.Collection(s.Name()) {{- end }} - s.log = logger.Sub(s.Name()) + s.log = log.Sub(s.Name()) return nil } diff --git a/cmd/project/templates/clients/rpc/client.go.tpl b/cmd/project/templates/clients/rpc/client.go.tpl index 272926d..eb8b8e4 100644 --- a/cmd/project/templates/clients/rpc/client.go.tpl +++ b/cmd/project/templates/clients/rpc/client.go.tpl @@ -26,7 +26,7 @@ func NewClientSetFromEnv() (*ClientSet, error) { // NewClient todo func NewClientSetFromConfig(conf *rpc.Config) (*ClientSet, error) { - log := logger.Sub("sdk.{{.Name}}") + log := log.Sub("sdk.{{.Name}}") ctx, cancel := context.WithTimeout(context.Background(), conf.Timeout()) defer cancel() @@ -60,5 +60,5 @@ func NewClientSetFromConfig(conf *rpc.Config) (*ClientSet, error) { type ClientSet struct { conf *rpc.Config conn *grpc.ClientConn - log logger.Logger + log log.Logger } \ No newline at end of file diff --git a/cmd/project/templates/cmd/root.go.tpl b/cmd/project/templates/cmd/root.go.tpl index 796a15d..046be90 100644 --- a/cmd/project/templates/cmd/root.go.tpl +++ b/cmd/project/templates/cmd/root.go.tpl @@ -63,7 +63,7 @@ func loadGlobalConfig(configType string) error { } func loadCache() error { - l := logger.Sub("INIT") + l := log.Sub("INIT") c := conf.C() // 设置全局缓存 switch c.Cache.Type { @@ -114,7 +114,7 @@ func loadGlobalLogger() error { if err := zap.Configure(zapConfig); err != nil { return err } - logger.Sub("init").Info(logInitMsg) + log.Sub("init").Info(logInitMsg) return nil } diff --git a/cmd/project/templates/cmd/start/cmd.go.tpl b/cmd/project/templates/cmd/start/cmd.go.tpl index 8362c00..6acc6f4 100644 --- a/cmd/project/templates/cmd/start/cmd.go.tpl +++ b/cmd/project/templates/cmd/start/cmd.go.tpl @@ -47,7 +47,7 @@ func newService(cnf *conf.Config) (*service, error) { svr := &service{ http: http, grpc: grpc, - log: logger.Sub("cli"), + log: log.Sub("cli"), ch: ch, } @@ -58,7 +58,7 @@ type service struct { http *protocol.HTTPService grpc *protocol.GRPCService ch chan os.Signal - log logger.Logger + log log.Logger } func (s *service) start() { diff --git a/examples/log/main.go b/examples/log/main.go index b8f1171..ba87517 100644 --- a/examples/log/main.go +++ b/examples/log/main.go @@ -4,19 +4,19 @@ import ( "context" "github.com/infraboard/mcube/v2/ioc" - "github.com/infraboard/mcube/v2/ioc/config/logger" + "github.com/infraboard/mcube/v2/ioc/config/log" ) func main() { ioc.DevelopmentSetup() - gLogger := logger.L() + gLogger := log.L() gLogger.Debug().Msg("this is global logger debug msg") - subLogger := logger.Sub("app1") + subLogger := log.Sub("app1") subLogger.Debug().Msg("this is app1 sub logger debug msg") ctx := context.Background() - traceLogger := logger.T("app1").Trace(ctx) + traceLogger := log.T("app1").Trace(ctx) traceLogger.Debug().Msg("this is app1 trace logger debug msg") } diff --git a/examples/project/apps/helloworld/api/http.go b/examples/project/apps/helloworld/api/http.go index 613346d..8596313 100644 --- a/examples/project/apps/helloworld/api/http.go +++ b/examples/project/apps/helloworld/api/http.go @@ -6,7 +6,7 @@ import ( "github.com/gin-gonic/gin" "github.com/infraboard/mcube/v2/examples/project/apps/helloworld" "github.com/infraboard/mcube/v2/ioc" - "github.com/infraboard/mcube/v2/ioc/config/logger" + "github.com/infraboard/mcube/v2/ioc/config/log" "github.com/rs/zerolog" ) @@ -30,7 +30,7 @@ type HelloServiceApiHandler struct { // 对象自定义初始化 func (h *HelloServiceApiHandler) Init() error { - h.log = logger.Sub("helloworld.api") + h.log = log.Sub("helloworld.api") return nil } diff --git a/examples/project/etc/application.toml b/examples/project/etc/application.toml index f9428de..fdc5a15 100644 --- a/examples/project/etc/application.toml +++ b/examples/project/etc/application.toml @@ -2,7 +2,7 @@ name = "exapmle" key = "this is your app key" -[app.http] +[http] host = "127.0.0.1" port = 8020 diff --git a/examples/project/main.go b/examples/project/main.go index 26fadfa..e559ba1 100644 --- a/examples/project/main.go +++ b/examples/project/main.go @@ -13,6 +13,7 @@ import ( ) func main() { + server.DefaultConfig.ConfigFile.Enabled = true err := server.Run(context.Background()) if err != nil { panic(err) diff --git a/grpc/middleware/recovery/handler.go b/grpc/middleware/recovery/handler.go index 6501827..6b8cd1e 100644 --- a/grpc/middleware/recovery/handler.go +++ b/grpc/middleware/recovery/handler.go @@ -4,7 +4,7 @@ import ( "context" "runtime/debug" - "github.com/infraboard/mcube/v2/ioc/config/logger" + "github.com/infraboard/mcube/v2/ioc/config/log" "github.com/rs/zerolog" ) @@ -17,7 +17,7 @@ type Handler interface { // NewZeroLogRecoveryHandler todo func NewZeroLogRecoveryHandler() *ZeroLogRecoveryHandler { return &ZeroLogRecoveryHandler{ - log: logger.Sub("grpc"), + log: log.Sub("grpc"), } } diff --git a/http/middleware/accesslog/log.go b/http/middleware/accesslog/log.go index 7c46e50..7e974a0 100644 --- a/http/middleware/accesslog/log.go +++ b/http/middleware/accesslog/log.go @@ -8,7 +8,7 @@ import ( "time" "github.com/infraboard/mcube/v2/http/response" - "github.com/infraboard/mcube/v2/ioc/config/logger" + "github.com/infraboard/mcube/v2/ioc/config/log" ) // LoggerEntry is the structure passed to the template. @@ -88,5 +88,5 @@ func (l *Logger) debug(msg string) { l.debugFn(msg) } - logger.Sub("access_log").Debug().Msg(msg) + log.Sub("access_log").Debug().Msg(msg) } diff --git a/http/middleware/cors/cors.go b/http/middleware/cors/cors.go index 27fe94b..3385555 100644 --- a/http/middleware/cors/cors.go +++ b/http/middleware/cors/cors.go @@ -6,8 +6,9 @@ import ( "strconv" "strings" - "github.com/infraboard/mcube/v2/ioc/config/logger" "github.com/rs/zerolog" + + logger "github.com/infraboard/mcube/v2/ioc/config/log" ) // Cors interface diff --git a/http/middleware/ratelimit/ratelimite.go b/http/middleware/ratelimit/ratelimite.go index 3edda10..de84932 100644 --- a/http/middleware/ratelimit/ratelimite.go +++ b/http/middleware/ratelimit/ratelimite.go @@ -8,7 +8,7 @@ import ( "github.com/infraboard/mcube/v2/flowcontrol" "github.com/infraboard/mcube/v2/flowcontrol/tokenbucket" - "github.com/infraboard/mcube/v2/ioc/config/logger" + "github.com/infraboard/mcube/v2/ioc/config/log" "github.com/rs/zerolog" ) @@ -42,7 +42,7 @@ func new(rate float64, capacity int64, mode Mode) *Limiter { rate: rate, capacity: capacity, limiters: make(map[string]flowcontrol.RateLimiter), - l: logger.Sub("Rate Limiter"), + l: log.Sub("Rate Limiter"), mode: mode, remoteIPHeaderKey: []string{"X-Forwarded-For", "X-Real-IP"}, maxSize: 1000, diff --git a/http/middleware/recovery/recovery.go b/http/middleware/recovery/recovery.go index 6c1acb5..a9fb316 100644 --- a/http/middleware/recovery/recovery.go +++ b/http/middleware/recovery/recovery.go @@ -8,7 +8,7 @@ import ( "github.com/infraboard/mcube/v2/exception" "github.com/infraboard/mcube/v2/http/response" "github.com/infraboard/mcube/v2/http/router" - "github.com/infraboard/mcube/v2/ioc/config/logger" + "github.com/infraboard/mcube/v2/ioc/config/log" "github.com/rs/zerolog" ) @@ -17,7 +17,7 @@ const recoveryExplanation = "Something went wrong" // New returns a new recovery instance func New() router.Middleware { return &recovery{ - logger: logger.Sub("recovery"), + logger: log.Sub("recovery"), } } diff --git a/http/middleware/trace/trace_test.go b/http/middleware/trace/trace_test.go index 18fdc09..6f09bcd 100644 --- a/http/middleware/trace/trace_test.go +++ b/http/middleware/trace/trace_test.go @@ -11,7 +11,7 @@ import ( "github.com/infraboard/mcube/v2/http/middleware/trace" "github.com/infraboard/mcube/v2/http/router/httprouter" - "github.com/infraboard/mcube/v2/ioc/config/logger" + "github.com/infraboard/mcube/v2/ioc/config/log" ) var ( @@ -29,7 +29,7 @@ func Test_Trace(t *testing.T) { tr := trace.New(tracer, peer) tr.Debug(true) - tr.SetLogger(logger.Sub("Trace")) + tr.SetLogger(log.Sub("Trace")) r := httprouter.New() r.Use(tr) @@ -76,7 +76,7 @@ func Test_Trace(t *testing.T) { // tr := trace.New(jaegerS, peer) // tr.Debug(true) -// tr.SetLogger(logger.Sub("Trace")) +// tr.SetLogger(log.Sub("Trace")) // r := httprouter.New() // r.Use(tr) diff --git a/http/restful/response/send.go b/http/restful/response/send.go index 21927ea..c7db4d9 100644 --- a/http/restful/response/send.go +++ b/http/restful/response/send.go @@ -6,7 +6,7 @@ import ( "github.com/emicklei/go-restful/v3" "github.com/infraboard/mcube/v2/exception" "github.com/infraboard/mcube/v2/http/response" - "github.com/infraboard/mcube/v2/ioc/config/logger" + "github.com/infraboard/mcube/v2/ioc/config/log" ) // Failed use to response error messge @@ -51,7 +51,7 @@ func Failed(w *restful.Response, err error, opts ...response.Option) { err = w.WriteHeaderAndEntity(httpCode, resp) if err != nil { - logger.L().Error().Msgf("send failed response error, %s", err) + log.L().Error().Msgf("send failed response error, %s", err) } } @@ -64,6 +64,6 @@ func Success(w *restful.Response, data any, opts ...response.Option) { err := w.WriteEntity(data) if err != nil { - logger.L().Error().Msgf("send success response error, %s", err) + log.L().Error().Msgf("send success response error, %s", err) } } diff --git a/ioc/apps/health/checker.go b/ioc/apps/health/checker.go index a5f67fb..b427874 100644 --- a/ioc/apps/health/checker.go +++ b/ioc/apps/health/checker.go @@ -6,7 +6,7 @@ import ( "github.com/gin-gonic/gin" h_response "github.com/infraboard/mcube/v2/http/response" "github.com/infraboard/mcube/v2/http/restful/response" - "github.com/infraboard/mcube/v2/ioc/config/logger" + "github.com/infraboard/mcube/v2/ioc/config/log" "github.com/rs/zerolog" "google.golang.org/grpc/health" healthgrpc "google.golang.org/grpc/health/grpc_health_v1" @@ -19,7 +19,7 @@ func NewDefaultHealthChecker() *HealthChecker { func NewHealthChecker(checker healthgrpc.HealthServer) *HealthChecker { return &HealthChecker{ service: checker, - log: logger.Sub("health_check"), + log: log.Sub("health_check"), HealthCheckPath: "/healthz", } } @@ -42,6 +42,18 @@ func (h *HealthChecker) WebService() *restful.WebService { return ws } +func (h *HealthChecker) Gin() *restful.WebService { + ws := new(restful.WebService) + ws.Path(h.HealthCheckPath) + tags := []string{"健康检查"} + ws.Route(ws.GET("/").To(h.RestfulHandleFunc). + Doc("查询服务当前状态"). + Metadata(restfulspec.KeyOpenAPITags, tags). + Returns(200, "OK", HealthCheckResponse{})) + + return ws +} + func (h *HealthChecker) RestfulHandleFunc(r *restful.Request, w *restful.Response) { req := NewHealthCheckRequest() resp, err := h.service.Check( diff --git a/ioc/apps/metric/gin/metric.go b/ioc/apps/metric/gin/metric.go index cd8c2ba..c9be847 100644 --- a/ioc/apps/metric/gin/metric.go +++ b/ioc/apps/metric/gin/metric.go @@ -5,7 +5,7 @@ import ( "github.com/infraboard/mcube/v2/ioc" "github.com/infraboard/mcube/v2/ioc/apps/metric" "github.com/infraboard/mcube/v2/ioc/config/http" - "github.com/infraboard/mcube/v2/ioc/config/logger" + "github.com/infraboard/mcube/v2/ioc/config/log" "github.com/prometheus/client_golang/prometheus/promhttp" "github.com/rs/zerolog" ) @@ -24,7 +24,7 @@ type ginHandler struct { } func (h *ginHandler) Init() error { - h.log = logger.Sub(metric.AppName) + h.log = log.Sub(metric.AppName) return nil } diff --git a/ioc/apps/metric/restful/metric.go b/ioc/apps/metric/restful/metric.go index 4e2a320..cdcb37a 100644 --- a/ioc/apps/metric/restful/metric.go +++ b/ioc/apps/metric/restful/metric.go @@ -6,7 +6,7 @@ import ( "github.com/infraboard/mcube/v2/ioc" "github.com/infraboard/mcube/v2/ioc/apps/metric" "github.com/infraboard/mcube/v2/ioc/config/http" - "github.com/infraboard/mcube/v2/ioc/config/logger" + "github.com/infraboard/mcube/v2/ioc/config/log" "github.com/prometheus/client_golang/prometheus/promhttp" "github.com/rs/zerolog" ) @@ -25,7 +25,7 @@ type restfulHandler struct { } func (h *restfulHandler) Init() error { - h.log = logger.Sub(metric.AppName) + h.log = log.Sub(metric.AppName) return nil } diff --git a/ioc/apps/oss/mongo/impl.go b/ioc/apps/oss/mongo/impl.go index 2496860..720f10d 100644 --- a/ioc/apps/oss/mongo/impl.go +++ b/ioc/apps/oss/mongo/impl.go @@ -3,10 +3,10 @@ package mongo import ( "github.com/infraboard/mcube/v2/ioc" "github.com/infraboard/mcube/v2/ioc/apps/oss" - "github.com/infraboard/mcube/v2/ioc/config/logger" "github.com/rs/zerolog" "go.mongodb.org/mongo-driver/mongo" + "github.com/infraboard/mcube/v2/ioc/config/log" ioc_mongo "github.com/infraboard/mcube/v2/ioc/config/mongo" ) @@ -22,7 +22,7 @@ type service struct { func (s *service) Init() error { s.db = ioc_mongo.DB() - s.log = logger.Sub("storage") + s.log = log.Sub("storage") return nil } diff --git a/ioc/config/grpc/grpc.go b/ioc/config/grpc/grpc.go index 1be6518..e4ea99e 100644 --- a/ioc/config/grpc/grpc.go +++ b/ioc/config/grpc/grpc.go @@ -7,7 +7,7 @@ import ( "github.com/infraboard/mcube/v2/grpc/middleware/recovery" "github.com/infraboard/mcube/v2/ioc" - "github.com/infraboard/mcube/v2/ioc/config/logger" + "github.com/infraboard/mcube/v2/ioc/config/log" "github.com/infraboard/mcube/v2/ioc/config/trace" "github.com/rs/zerolog" "go.opentelemetry.io/contrib/instrumentation/google.golang.org/grpc/otelgrpc" @@ -51,6 +51,10 @@ type Grpc struct { PreStop func(context.Context) error `json:"-" yaml:"-" toml:"-" env:"-"` } +func (g *Grpc) Name() string { + return AppName +} + func (g *Grpc) setEnable(v bool) { g.Enable = &v } @@ -60,7 +64,7 @@ func (g *Grpc) Addr() string { } func (g *Grpc) Init() error { - g.log = logger.Sub("grpc") + g.log = log.Sub("grpc") if g.Enable == nil { g.setEnable(ioc.GrpcControllerCount() > 0) diff --git a/ioc/config/http/http.go b/ioc/config/http/http.go index 20cd9f3..1e2002d 100644 --- a/ioc/config/http/http.go +++ b/ioc/config/http/http.go @@ -11,7 +11,7 @@ import ( "github.com/go-openapi/spec" "github.com/infraboard/mcube/v2/ioc" "github.com/infraboard/mcube/v2/ioc/config/application" - "github.com/infraboard/mcube/v2/ioc/config/logger" + "github.com/infraboard/mcube/v2/ioc/config/log" "github.com/rs/zerolog" ) @@ -186,9 +186,13 @@ func (h *Http) maxHandlerCount() (maxKey WEB_FRAMEWORK, maxValue int) { return } +func (h *Http) Name() string { + return AppName +} + // 配置数据解析 func (h *Http) Init() error { - h.log = logger.Sub("http") + h.log = log.Sub("http") h.DetectAndSetWebFramework() if !*h.Enable { diff --git a/ioc/config/http/restful.go b/ioc/config/http/restful.go index f31826c..79da4c3 100644 --- a/ioc/config/http/restful.go +++ b/ioc/config/http/restful.go @@ -8,7 +8,7 @@ import ( "github.com/infraboard/mcube/v2/ioc/apps/apidoc" "github.com/infraboard/mcube/v2/ioc/apps/health" "github.com/infraboard/mcube/v2/ioc/config/application" - "github.com/infraboard/mcube/v2/ioc/config/logger" + "github.com/infraboard/mcube/v2/ioc/config/log" "github.com/infraboard/mcube/v2/ioc/config/trace" "go.opentelemetry.io/contrib/instrumentation/github.com/emicklei/go-restful/otelrestful" ) @@ -28,7 +28,7 @@ func (b *GoRestfulRouterBuilder) Config(c *BuildConfig) { } func (b *GoRestfulRouterBuilder) Build() (http.Handler, error) { - log := logger.Sub("go-restful") + log := log.Sub("go-restful") r := restful.DefaultContainer restful.DefaultResponseContentType(restful.MIME_JSON) diff --git a/ioc/config/kafka/kafka.go b/ioc/config/kafka/kafka.go index c6dea9d..f2e7181 100644 --- a/ioc/config/kafka/kafka.go +++ b/ioc/config/kafka/kafka.go @@ -5,7 +5,7 @@ import ( "time" "github.com/infraboard/mcube/v2/ioc" - "github.com/infraboard/mcube/v2/ioc/config/logger" + "github.com/infraboard/mcube/v2/ioc/config/log" kafka "github.com/segmentio/kafka-go" "github.com/segmentio/kafka-go/sasl" "github.com/segmentio/kafka-go/sasl/scram" @@ -56,7 +56,7 @@ func (k *Kafka) scramAlgorithm() scram.Algorithm { } func (k *Kafka) Producer(topic string) *kafka.Writer { - l := logger.Sub(fmt.Sprintf("producer_%s", topic)) + l := log.Sub(fmt.Sprintf("producer_%s", topic)) w := &kafka.Writer{ Addr: kafka.TCP(k.Brokers...), @@ -73,7 +73,7 @@ func (k *Kafka) Producer(topic string) *kafka.Writer { } func (k *Kafka) ConsumerGroup(groupId string, topics []string) *kafka.Reader { - l := logger.Sub(fmt.Sprintf("consumer_group_%s", groupId)) + l := log.Sub(fmt.Sprintf("consumer_group_%s", groupId)) dialer := &kafka.Dialer{ Timeout: 10 * time.Second, DualStack: true, diff --git a/ioc/config/logger/interface.go b/ioc/config/log/interface.go similarity index 96% rename from ioc/config/logger/interface.go rename to ioc/config/log/interface.go index 9165a48..558e94d 100644 --- a/ioc/config/logger/interface.go +++ b/ioc/config/log/interface.go @@ -1,4 +1,4 @@ -package logger +package log import ( "github.com/infraboard/mcube/v2/ioc" diff --git a/ioc/config/logger/logger.go b/ioc/config/log/logger.go similarity index 99% rename from ioc/config/logger/logger.go rename to ioc/config/log/logger.go index 5d6dba2..0d10f0c 100644 --- a/ioc/config/logger/logger.go +++ b/ioc/config/log/logger.go @@ -1,4 +1,4 @@ -package logger +package log import ( "context" diff --git a/ioc/config/logger/logger_test.go b/ioc/config/log/logger_test.go similarity index 68% rename from ioc/config/logger/logger_test.go rename to ioc/config/log/logger_test.go index b839812..47f38bc 100644 --- a/ioc/config/logger/logger_test.go +++ b/ioc/config/log/logger_test.go @@ -1,4 +1,4 @@ -package logger_test +package log_test import ( "context" @@ -8,12 +8,12 @@ import ( "github.com/BurntSushi/toml" "github.com/infraboard/mcube/v2/ioc" - "github.com/infraboard/mcube/v2/ioc/config/logger" + "github.com/infraboard/mcube/v2/ioc/config/log" ) func TestGetClientGetter(t *testing.T) { - sub := logger.Sub("module_a") - logger.T("module_a").Trace(context.Background()) + sub := log.Sub("module_a") + log.T("module_a").Trace(context.Background()) sub.Debug().Msgf("hello %s", "a") } @@ -22,7 +22,7 @@ func TestDefaultConfig(t *testing.T) { if err != nil { t.Fatal(err) } - appConf := map[string]any{logger.AppName: ioc.Config().Get(logger.AppName).(*logger.Config)} + appConf := map[string]any{log.AppName: ioc.Config().Get(log.AppName).(*log.Config)} toml.NewEncoder(f).Encode(appConf) } @@ -30,7 +30,7 @@ func TestPanicStack(t *testing.T) { // 捕获 panic defer func() { if err := recover(); err != nil { - logger.L().Error().Stack().Msgf("Panic occurred: %v\n%s", err, debug.Stack()) + log.L().Error().Stack().Msgf("Panic occurred: %v\n%s", err, debug.Stack()) } }() diff --git a/ioc/config/logger/test/default.toml b/ioc/config/log/test/default.toml similarity index 100% rename from ioc/config/logger/test/default.toml rename to ioc/config/log/test/default.toml diff --git a/ioc/config/trace/trace.go b/ioc/config/trace/trace.go index a05192c..6dabf61 100644 --- a/ioc/config/trace/trace.go +++ b/ioc/config/trace/trace.go @@ -18,12 +18,11 @@ func init() { } type Trace struct { - Enable bool `json:"enable" yaml:"enable" toml:"enable" env:"TRACE_ENABLE"` - Provider TRACE_PROVIDER `toml:"provider" json:"provider" yaml:"provider" env:"TRACE_PROVIDER"` - Endpoint string `toml:"endpoint" json:"endpoint" yaml:"endpoint" env:"TRACE_ENDPOINT"` - ServiceName string `toml:"service_name" json:"service_name" yaml:"service_name" env:"TRACE_SERVICE_NAME"` - ioc.ObjectImpl + + Enable bool `json:"enable" yaml:"enable" toml:"enable" env:"TRACE_ENABLE"` + Provider TRACE_PROVIDER `toml:"provider" json:"provider" yaml:"provider" env:"TRACE_PROVIDER"` + Endpoint string `toml:"endpoint" json:"endpoint" yaml:"endpoint" env:"TRACE_ENDPOINT"` } // 优先初始化, 以供后面的组件使用 @@ -31,6 +30,10 @@ func (i *Trace) Priority() int { return 99 } +func (i *Trace) Name() string { + return AppName +} + func (t *Trace) Init() error { exporter, err := jaeger.New(jaeger.WithCollectorEndpoint(jaeger.WithEndpoint(t.Endpoint))) if err != nil { diff --git a/ioc/server/server.go b/ioc/server/server.go index fd2ff54..0fa01f7 100644 --- a/ioc/server/server.go +++ b/ioc/server/server.go @@ -9,7 +9,7 @@ import ( "github.com/infraboard/mcube/v2/ioc" "github.com/infraboard/mcube/v2/ioc/config/grpc" "github.com/infraboard/mcube/v2/ioc/config/http" - "github.com/infraboard/mcube/v2/ioc/config/logger" + "github.com/infraboard/mcube/v2/ioc/config/log" "github.com/rs/zerolog" ) @@ -35,7 +35,7 @@ func NewServer() *Server { return &Server{ http: http.Get(), grpc: grpc.Get(), - log: logger.Sub("server"), + log: log.Sub("server"), ch: ch, ctx: ctx, cancle: cancle, diff --git a/ioc/store.go b/ioc/store.go index 250be5d..a401657 100644 --- a/ioc/store.go +++ b/ioc/store.go @@ -296,6 +296,7 @@ func (s *NamespaceStore) Init() error { for i := range s.Items { obj := s.Items[i] err := obj.Value.Init() + fmt.Println(obj.Name) if err != nil { return fmt.Errorf("init object %s error, %s", obj.Name, err) }