Commit ae2e6415 authored by kzkzzzz's avatar kzkzzzz

修改全局log

parent 184f42d0
...@@ -29,13 +29,12 @@ func init() { ...@@ -29,13 +29,12 @@ func init() {
flag.StringVar(&flagconf, "conf", "", "config path, eg: -conf config.yaml") flag.StringVar(&flagconf, "conf", "", "config path, eg: -conf config.yaml")
} }
func newApp(logger log.Logger, hs *http.Server, gs *grpc.Server) *kratos.App { func newApp(hs *http.Server, gs *grpc.Server) *kratos.App {
return kratos.New( return kratos.New(
kratos.ID(id), kratos.ID(id),
kratos.Name(Name), kratos.Name(Name),
kratos.Version(Version), kratos.Version(Version),
kratos.Metadata(map[string]string{}), kratos.Metadata(map[string]string{}),
kratos.Logger(logger),
kratos.Server( kratos.Server(
hs, hs,
gs, gs,
...@@ -54,6 +53,8 @@ func main() { ...@@ -54,6 +53,8 @@ func main() {
"trace.id", tracing.TraceID(), "trace.id", tracing.TraceID(),
"span.id", tracing.SpanID(), "span.id", tracing.SpanID(),
) )
log.SetLogger(logger)
c := config.New( c := config.New(
config.WithSource( config.WithSource(
file.NewSource(flagconf), file.NewSource(flagconf),
...@@ -70,7 +71,7 @@ func main() { ...@@ -70,7 +71,7 @@ func main() {
panic(err) panic(err)
} }
app, cleanup, err := wireApp(bc.Server, bc.Data, logger) app, cleanup, err := wireApp(bc.Server, bc.Data)
if err != nil { if err != nil {
panic(err) panic(err)
} }
......
//go:build wireinject
// +build wireinject // +build wireinject
// The build tag makes sure the stub is not built in the final build. // The build tag makes sure the stub is not built in the final build.
...@@ -11,11 +12,10 @@ import ( ...@@ -11,11 +12,10 @@ import (
"github.com/go-kratos/kratos-layout/internal/server" "github.com/go-kratos/kratos-layout/internal/server"
"github.com/go-kratos/kratos-layout/internal/service" "github.com/go-kratos/kratos-layout/internal/service"
"github.com/go-kratos/kratos/v2" "github.com/go-kratos/kratos/v2"
"github.com/go-kratos/kratos/v2/log"
"github.com/google/wire" "github.com/google/wire"
) )
// wireApp init kratos application. // wireApp init kratos application.
func wireApp(*conf.Server, *conf.Data, log.Logger) (*kratos.App, func(), error) { func wireApp(*conf.Server, *conf.Data) (*kratos.App, func(), error) {
panic(wire.Build(server.ProviderSet, data.ProviderSet, biz.ProviderSet, service.ProviderSet, newApp)) panic(wire.Build(server.ProviderSet, data.ProviderSet, biz.ProviderSet, service.ProviderSet, newApp))
} }
// Code generated by Wire. DO NOT EDIT.
//go:generate go run github.com/google/wire/cmd/wire
//+build !wireinject
package main
import (
"github.com/go-kratos/kratos-layout/internal/biz"
"github.com/go-kratos/kratos-layout/internal/conf"
"github.com/go-kratos/kratos-layout/internal/data"
"github.com/go-kratos/kratos-layout/internal/server"
"github.com/go-kratos/kratos-layout/internal/service"
"github.com/go-kratos/kratos/v2"
"github.com/go-kratos/kratos/v2/log"
)
// Injectors from wire.go:
// wireApp init kratos application.
func wireApp(confServer *conf.Server, confData *conf.Data, logger log.Logger) (*kratos.App, func(), error) {
dataData, cleanup, err := data.NewData(confData, logger)
if err != nil {
return nil, nil, err
}
greeterRepo := data.NewGreeterRepo(dataData, logger)
greeterUsecase := biz.NewGreeterUsecase(greeterRepo, logger)
greeterService := service.NewGreeterService(greeterUsecase)
httpServer := server.NewHTTPServer(confServer, greeterService, logger)
grpcServer := server.NewGRPCServer(confServer, greeterService, logger)
app := newApp(logger, httpServer, grpcServer)
return app, func() {
cleanup()
}, nil
}
...@@ -16,14 +16,17 @@ require ( ...@@ -16,14 +16,17 @@ require (
github.com/go-logr/stdr v1.2.2 // indirect github.com/go-logr/stdr v1.2.2 // indirect
github.com/go-playground/form/v4 v4.2.0 // indirect github.com/go-playground/form/v4 v4.2.0 // indirect
github.com/golang/protobuf v1.5.2 // indirect github.com/golang/protobuf v1.5.2 // indirect
github.com/google/subcommands v1.0.1 // indirect
github.com/google/uuid v1.3.0 // indirect github.com/google/uuid v1.3.0 // indirect
github.com/gorilla/mux v1.8.0 // indirect github.com/gorilla/mux v1.8.0 // indirect
github.com/imdario/mergo v0.3.12 // indirect github.com/imdario/mergo v0.3.12 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
go.opentelemetry.io/otel v1.7.0 // indirect go.opentelemetry.io/otel v1.7.0 // indirect
go.opentelemetry.io/otel/trace v1.7.0 // indirect go.opentelemetry.io/otel/trace v1.7.0 // indirect
golang.org/x/net v0.0.0-20220520000938-2e3eb7b945c2 // indirect golang.org/x/net v0.0.0-20220520000938-2e3eb7b945c2 // indirect
golang.org/x/sync v0.0.0-20220513210516-0976fa681c29 // indirect golang.org/x/sync v0.0.0-20220513210516-0976fa681c29 // indirect
golang.org/x/sys v0.0.0-20220520151302-bc2c85ada10a // indirect golang.org/x/sys v0.0.0-20220520151302-bc2c85ada10a // indirect
golang.org/x/text v0.3.7 // indirect golang.org/x/text v0.3.7 // indirect
golang.org/x/tools v0.0.0-20190524140312-2c0ae7006135 // indirect
gopkg.in/yaml.v3 v3.0.0 // indirect gopkg.in/yaml.v3 v3.0.0 // indirect
) )
...@@ -62,6 +62,7 @@ github.com/google/go-cmp v0.5.5/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/ ...@@ -62,6 +62,7 @@ github.com/google/go-cmp v0.5.5/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/
github.com/google/go-cmp v0.5.6/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE= github.com/google/go-cmp v0.5.6/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
github.com/google/go-cmp v0.5.7 h1:81/ik6ipDQS2aGcBfIN5dHDB36BwrStyeAQquSYCV4o= github.com/google/go-cmp v0.5.7 h1:81/ik6ipDQS2aGcBfIN5dHDB36BwrStyeAQquSYCV4o=
github.com/google/go-cmp v0.5.7/go.mod h1:n+brtR0CgQNWTVd5ZUFpTBC8YFBDLK/h/bpaJ8/DtOE= github.com/google/go-cmp v0.5.7/go.mod h1:n+brtR0CgQNWTVd5ZUFpTBC8YFBDLK/h/bpaJ8/DtOE=
github.com/google/subcommands v1.0.1 h1:/eqq+otEXm5vhfBrbREPCSVQbvofip6kIz+mX5TUH7k=
github.com/google/subcommands v1.0.1/go.mod h1:ZjhPrFU+Olkh9WazFPsl27BQ4UPiG37m3yTrtFlrHVk= github.com/google/subcommands v1.0.1/go.mod h1:ZjhPrFU+Olkh9WazFPsl27BQ4UPiG37m3yTrtFlrHVk=
github.com/google/uuid v1.1.2/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo= github.com/google/uuid v1.1.2/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
github.com/google/uuid v1.3.0 h1:t6JiXgmwXMjEs8VusXIJk2BXHsn+wx8BZdTaoZ5fu7I= github.com/google/uuid v1.3.0 h1:t6JiXgmwXMjEs8VusXIJk2BXHsn+wx8BZdTaoZ5fu7I=
...@@ -146,6 +147,7 @@ golang.org/x/tools v0.0.0-20190114222345-bf090417da8b/go.mod h1:n7NCudcB/nEzxVGm ...@@ -146,6 +147,7 @@ golang.org/x/tools v0.0.0-20190114222345-bf090417da8b/go.mod h1:n7NCudcB/nEzxVGm
golang.org/x/tools v0.0.0-20190226205152-f727befe758c/go.mod h1:9Yl7xja0Znq3iFh3HoIrodX9oNMXvdceNzlUR8zjMvY= golang.org/x/tools v0.0.0-20190226205152-f727befe758c/go.mod h1:9Yl7xja0Znq3iFh3HoIrodX9oNMXvdceNzlUR8zjMvY=
golang.org/x/tools v0.0.0-20190311212946-11955173bddd/go.mod h1:LCzVGOaR6xXOjkQ3onu1FJEFr0SW1gC7cKk1uF8kGRs= golang.org/x/tools v0.0.0-20190311212946-11955173bddd/go.mod h1:LCzVGOaR6xXOjkQ3onu1FJEFr0SW1gC7cKk1uF8kGRs=
golang.org/x/tools v0.0.0-20190422233926-fe54fb35175b/go.mod h1:LCzVGOaR6xXOjkQ3onu1FJEFr0SW1gC7cKk1uF8kGRs= golang.org/x/tools v0.0.0-20190422233926-fe54fb35175b/go.mod h1:LCzVGOaR6xXOjkQ3onu1FJEFr0SW1gC7cKk1uF8kGRs=
golang.org/x/tools v0.0.0-20190524140312-2c0ae7006135 h1:5Beo0mZN8dRzgrMMkDp0jc8YXQKx9DiJ2k1dkvGsn5A=
golang.org/x/tools v0.0.0-20190524140312-2c0ae7006135/go.mod h1:RgjU9mgBXZiqYHBnxXauZ1Gv1EHHAz9KjViQ78xBX0Q= golang.org/x/tools v0.0.0-20190524140312-2c0ae7006135/go.mod h1:RgjU9mgBXZiqYHBnxXauZ1Gv1EHHAz9KjViQ78xBX0Q=
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1 h1:go1bK/D/BFZV2I8cIQd1NKEZ+0owSTG1fDTci4IqFcE= golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1 h1:go1bK/D/BFZV2I8cIQd1NKEZ+0owSTG1fDTci4IqFcE=
......
...@@ -34,12 +34,12 @@ type GreeterUsecase struct { ...@@ -34,12 +34,12 @@ type GreeterUsecase struct {
} }
// NewGreeterUsecase new a Greeter usecase. // NewGreeterUsecase new a Greeter usecase.
func NewGreeterUsecase(repo GreeterRepo, logger log.Logger) *GreeterUsecase { func NewGreeterUsecase(repo GreeterRepo) *GreeterUsecase {
return &GreeterUsecase{repo: repo, log: log.NewHelper(logger)} return &GreeterUsecase{repo: repo}
} }
// CreateGreeter creates a Greeter, and returns the new Greeter. // CreateGreeter creates a Greeter, and returns the new Greeter.
func (uc *GreeterUsecase) CreateGreeter(ctx context.Context, g *Greeter) (*Greeter, error) { func (uc *GreeterUsecase) CreateGreeter(ctx context.Context, g *Greeter) (*Greeter, error) {
uc.log.WithContext(ctx).Infof("CreateGreeter: %v", g.Hello) log.Infof("CreateGreeter: %v", g.Hello)
return uc.repo.Save(ctx, g) return uc.repo.Save(ctx, g)
} }
...@@ -15,9 +15,9 @@ type Data struct { ...@@ -15,9 +15,9 @@ type Data struct {
} }
// NewData . // NewData .
func NewData(c *conf.Data, logger log.Logger) (*Data, func(), error) { func NewData(c *conf.Data) (*Data, func(), error) {
cleanup := func() { cleanup := func() {
log.NewHelper(logger).Info("closing the data resources") log.Info("closing the data resources")
} }
return &Data{}, cleanup, nil return &Data{}, cleanup, nil
} }
...@@ -4,19 +4,16 @@ import ( ...@@ -4,19 +4,16 @@ import (
"context" "context"
"github.com/go-kratos/kratos-layout/internal/biz" "github.com/go-kratos/kratos-layout/internal/biz"
"github.com/go-kratos/kratos/v2/log"
) )
type greeterRepo struct { type greeterRepo struct {
data *Data data *Data
log *log.Helper
} }
// NewGreeterRepo . // NewGreeterRepo .
func NewGreeterRepo(data *Data, logger log.Logger) biz.GreeterRepo { func NewGreeterRepo(data *Data) biz.GreeterRepo {
return &greeterRepo{ return &greeterRepo{
data: data, data: data,
log: log.NewHelper(logger),
} }
} }
......
...@@ -4,13 +4,12 @@ import ( ...@@ -4,13 +4,12 @@ import (
v1 "github.com/go-kratos/kratos-layout/api/helloworld/v1" v1 "github.com/go-kratos/kratos-layout/api/helloworld/v1"
"github.com/go-kratos/kratos-layout/internal/conf" "github.com/go-kratos/kratos-layout/internal/conf"
"github.com/go-kratos/kratos-layout/internal/service" "github.com/go-kratos/kratos-layout/internal/service"
"github.com/go-kratos/kratos/v2/log"
"github.com/go-kratos/kratos/v2/middleware/recovery" "github.com/go-kratos/kratos/v2/middleware/recovery"
"github.com/go-kratos/kratos/v2/transport/grpc" "github.com/go-kratos/kratos/v2/transport/grpc"
) )
// NewGRPCServer new a gRPC server. // NewGRPCServer new a gRPC server.
func NewGRPCServer(c *conf.Server, greeter *service.GreeterService, logger log.Logger) *grpc.Server { func NewGRPCServer(c *conf.Server, greeter *service.GreeterService) *grpc.Server {
var opts = []grpc.ServerOption{ var opts = []grpc.ServerOption{
grpc.Middleware( grpc.Middleware(
recovery.Recovery(), recovery.Recovery(),
......
...@@ -4,13 +4,12 @@ import ( ...@@ -4,13 +4,12 @@ import (
v1 "github.com/go-kratos/kratos-layout/api/helloworld/v1" v1 "github.com/go-kratos/kratos-layout/api/helloworld/v1"
"github.com/go-kratos/kratos-layout/internal/conf" "github.com/go-kratos/kratos-layout/internal/conf"
"github.com/go-kratos/kratos-layout/internal/service" "github.com/go-kratos/kratos-layout/internal/service"
"github.com/go-kratos/kratos/v2/log"
"github.com/go-kratos/kratos/v2/middleware/recovery" "github.com/go-kratos/kratos/v2/middleware/recovery"
"github.com/go-kratos/kratos/v2/transport/http" "github.com/go-kratos/kratos/v2/transport/http"
) )
// NewHTTPServer new a HTTP server. // NewHTTPServer new a HTTP server.
func NewHTTPServer(c *conf.Server, greeter *service.GreeterService, logger log.Logger) *http.Server { func NewHTTPServer(c *conf.Server, greeter *service.GreeterService) *http.Server {
var opts = []http.ServerOption{ var opts = []http.ServerOption{
http.Middleware( http.Middleware(
recovery.Recovery(), recovery.Recovery(),
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment