Commit ae2e6415 authored by kzkzzzz's avatar kzkzzzz

修改全局log

parent 184f42d0
......@@ -29,13 +29,12 @@ func init() {
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(
kratos.ID(id),
kratos.Name(Name),
kratos.Version(Version),
kratos.Metadata(map[string]string{}),
kratos.Logger(logger),
kratos.Server(
hs,
gs,
......@@ -54,6 +53,8 @@ func main() {
"trace.id", tracing.TraceID(),
"span.id", tracing.SpanID(),
)
log.SetLogger(logger)
c := config.New(
config.WithSource(
file.NewSource(flagconf),
......@@ -70,7 +71,7 @@ func main() {
panic(err)
}
app, cleanup, err := wireApp(bc.Server, bc.Data, logger)
app, cleanup, err := wireApp(bc.Server, bc.Data)
if err != nil {
panic(err)
}
......
//go:build wireinject
// +build wireinject
// The build tag makes sure the stub is not built in the final build.
......@@ -11,11 +12,10 @@ import (
"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"
"github.com/google/wire"
)
// 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))
}
// 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 (
github.com/go-logr/stdr v1.2.2 // indirect
github.com/go-playground/form/v4 v4.2.0 // 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/gorilla/mux v1.8.0 // 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/trace v1.7.0 // indirect
golang.org/x/net v0.0.0-20220520000938-2e3eb7b945c2 // indirect
golang.org/x/sync v0.0.0-20220513210516-0976fa681c29 // indirect
golang.org/x/sys v0.0.0-20220520151302-bc2c85ada10a // 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
)
......@@ -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.7 h1:81/ik6ipDQS2aGcBfIN5dHDB36BwrStyeAQquSYCV4o=
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/uuid v1.1.2/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
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
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-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/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=
......
......@@ -34,12 +34,12 @@ type GreeterUsecase struct {
}
// NewGreeterUsecase new a Greeter usecase.
func NewGreeterUsecase(repo GreeterRepo, logger log.Logger) *GreeterUsecase {
return &GreeterUsecase{repo: repo, log: log.NewHelper(logger)}
func NewGreeterUsecase(repo GreeterRepo) *GreeterUsecase {
return &GreeterUsecase{repo: repo}
}
// CreateGreeter creates a Greeter, and returns the new Greeter.
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)
}
......@@ -15,9 +15,9 @@ type Data struct {
}
// NewData .
func NewData(c *conf.Data, logger log.Logger) (*Data, func(), error) {
func NewData(c *conf.Data) (*Data, func(), error) {
cleanup := func() {
log.NewHelper(logger).Info("closing the data resources")
log.Info("closing the data resources")
}
return &Data{}, cleanup, nil
}
......@@ -4,19 +4,16 @@ import (
"context"
"github.com/go-kratos/kratos-layout/internal/biz"
"github.com/go-kratos/kratos/v2/log"
)
type greeterRepo struct {
data *Data
log *log.Helper
}
// NewGreeterRepo .
func NewGreeterRepo(data *Data, logger log.Logger) biz.GreeterRepo {
func NewGreeterRepo(data *Data) biz.GreeterRepo {
return &greeterRepo{
data: data,
log: log.NewHelper(logger),
}
}
......
......@@ -4,13 +4,12 @@ import (
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/service"
"github.com/go-kratos/kratos/v2/log"
"github.com/go-kratos/kratos/v2/middleware/recovery"
"github.com/go-kratos/kratos/v2/transport/grpc"
)
// 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{
grpc.Middleware(
recovery.Recovery(),
......
......@@ -4,13 +4,12 @@ import (
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/service"
"github.com/go-kratos/kratos/v2/log"
"github.com/go-kratos/kratos/v2/middleware/recovery"
"github.com/go-kratos/kratos/v2/transport/http"
)
// 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{
http.Middleware(
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