Skip to content

Commit

Permalink
调整log包
Browse files Browse the repository at this point in the history
  • Loading branch information
yumaojun03 committed Dec 25, 2023
1 parent 2d21592 commit 186de9e
Show file tree
Hide file tree
Showing 38 changed files with 103 additions and 77 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand Down
4 changes: 2 additions & 2 deletions client/rest/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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,
}
Expand Down
4 changes: 2 additions & 2 deletions client/rest/request.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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
Expand Down
4 changes: 2 additions & 2 deletions client/rest/response.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"),
}
}

Expand Down
4 changes: 2 additions & 2 deletions cmd/module/templates/book/api/http.go.gin.tpl
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
4 changes: 2 additions & 2 deletions cmd/module/templates/book/api/http.go.go-restful.tpl
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
4 changes: 2 additions & 2 deletions cmd/module/templates/book/api/http.go.httprouter.tpl
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
4 changes: 2 additions & 2 deletions cmd/module/templates/book/api/http.go.tpl
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
4 changes: 2 additions & 2 deletions cmd/module/templates/book/impl/impl.go.tpl
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ type service struct {
{{ if $.EnableMongoDB -}}
col *mongo.Collection
{{- end }}
log logger.Logger
log log.Logger
book.UnimplementedServiceServer
}

Expand All @@ -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
}

Expand Down
4 changes: 2 additions & 2 deletions cmd/project/templates/clients/rpc/client.go.tpl
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down Expand Up @@ -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
}
4 changes: 2 additions & 2 deletions cmd/project/templates/cmd/root.go.tpl
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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
}

Expand Down
4 changes: 2 additions & 2 deletions cmd/project/templates/cmd/start/cmd.go.tpl
Original file line number Diff line number Diff line change
Expand Up @@ -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,
}

Expand All @@ -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() {
Expand Down
8 changes: 4 additions & 4 deletions examples/log/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
}
4 changes: 2 additions & 2 deletions examples/project/apps/helloworld/api/http.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)

Expand All @@ -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
}

Expand Down
2 changes: 1 addition & 1 deletion examples/project/etc/application.toml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
name = "exapmle"
key = "this is your app key"

[app.http]
[http]
host = "127.0.0.1"
port = 8020

Expand Down
1 change: 1 addition & 0 deletions examples/project/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import (
)

func main() {
server.DefaultConfig.ConfigFile.Enabled = true
err := server.Run(context.Background())
if err != nil {
panic(err)
Expand Down
4 changes: 2 additions & 2 deletions grpc/middleware/recovery/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)

Expand All @@ -17,7 +17,7 @@ type Handler interface {
// NewZeroLogRecoveryHandler todo
func NewZeroLogRecoveryHandler() *ZeroLogRecoveryHandler {
return &ZeroLogRecoveryHandler{
log: logger.Sub("grpc"),
log: log.Sub("grpc"),
}
}

Expand Down
4 changes: 2 additions & 2 deletions http/middleware/accesslog/log.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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)
}
3 changes: 2 additions & 1 deletion http/middleware/cors/cors.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions http/middleware/ratelimit/ratelimite.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)

Expand Down Expand Up @@ -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,
Expand Down
4 changes: 2 additions & 2 deletions http/middleware/recovery/recovery.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)

Expand All @@ -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"),
}
}

Expand Down
6 changes: 3 additions & 3 deletions http/middleware/trace/trace_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 (
Expand All @@ -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)
Expand Down Expand Up @@ -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)
Expand Down
6 changes: 3 additions & 3 deletions http/restful/response/send.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)
}
}

Expand All @@ -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)
}
}
Loading

0 comments on commit 186de9e

Please sign in to comment.