Repository: go-kratos/kratos
Pull request #2059 (2022-05-26 15:53:56)

app: fix instance nil when not registered
```go
logger, err := logimpl.NewLogger()
if err != nil {
    log.Fatal(err)
}
defer logger.Close()

app := kratos.New(
    kratos.Name(Name),
    kratos.Logger(logger), // log.SetLogger(logger)
    kratos.Version(Version),
    kratos.Server(httpSrv, grpcSrv),
)
if err := app.Run(); err != nil {
    log.Fatal(err)
}
```

codecov-commenter: # [Codecov](https://codecov.io/gh/go-kratos/kratos/pull/2059?src=pr&el=h1&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=go-kratos) Report
> Merging [#2059](https://codecov.io/gh/go-kratos/kratos/pull/2059?src=pr&el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=go-kratos) (9ba7a5d) into [main](https://codecov.io/gh/go-kratos/kratos/commit/8fd0dc7680ca7f614531d2483384c8978bdefd8d?el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=go-kratos) (8fd0dc7) will **decrease** coverage by `0.03%`.
> The diff coverage is `81.81%`.

> :exclamation: Current head 9ba7a5d differs from pull request most recent head eea3f62. Consider uploading reports for the commit eea3f62 to get more accurate results

```diff
@@            Coverage Diff             @@
##             main    #2059      +/-   ##
==========================================
- Coverage   79.54%   79.51%   -0.04%     
==========================================
  Files          82       82              
  Lines        3677     3676       -1     
==========================================
- Hits         2925     2923       -2     
  Misses        543      543              
- Partials      209      210       +1     
```


| [Impacted Files](https://codecov.io/gh/go-kratos/kratos/pull/2059?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=go-kratos) | Coverage Δ | |
|---|---|---|
| [app.go](https://codecov.io/gh/go-kratos/kratos/pull/2059/diff?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=go-kratos#diff-YXBwLmdv) | `83.16% <80.00%> (-1.00%)` | :arrow_down: |
| [options.go](https://codecov.io/gh/go-kratos/kratos/pull/2059/diff?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=go-kratos#diff-b3B0aW9ucy5nbw==) | `91.66% <100.00%> (-0.34%)` | :arrow_down: |

------

[Continue to review full report at Codecov](https://codecov.io/gh/go-kratos/kratos/pull/2059?src=pr&el=continue&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=go-kratos).
> **Legend** - [Click here to learn more](https://docs.codecov.io/docs/codecov-delta?utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=go-kratos)
> `Δ = absolute <relative> (impact)`, `ø = not affected`, `? = missing data`
> Powered by [Codecov](https://codecov.io/gh/go-kratos/kratos/pull/2059?src=pr&el=footer&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=go-kratos). Last update [8fd0dc7...eea3f62](https://codecov.io/gh/go-kratos/kratos/pull/2059?src=pr&el=lastupdated&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=go-kratos). Read the [comment docs](https://docs.codecov.io/docs/pull-request-comments?utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=go-kratos).


Patch:
diff --git a/.github/workflows/go.yml b/.github/workflows/go.yml
index 634f80f6..3fea4feb 100644
--- a/.github/workflows/go.yml
+++ b/.github/workflows/go.yml
@@ -43,7 +43,7 @@ jobs:
     steps:
       - uses: actions/checkout@v3
       - name: Set up Go
-        uses: actions/setup-go@v2
+        uses: actions/setup-go@v3.2.0
         with:
           go-version: ${{ matrix.go }}
 
diff --git a/app.go b/app.go
index 7a1ed446..df992250 100644
--- a/app.go
+++ b/app.go
@@ -31,7 +31,7 @@ type App struct {
 	opts     options
 	ctx      context.Context
 	cancel   func()
-	lk       sync.Mutex
+	mu       sync.Mutex
 	instance *registry.ServiceInstance
 }
 
@@ -39,7 +39,6 @@ type App struct {
 func New(opts ...Option) *App {
 	o := options{
 		ctx:              context.Background(),
-		logger:           log.NewHelper(log.GetLogger()),
 		sigs:             []os.Signal{syscall.SIGTERM, syscall.SIGQUIT, syscall.SIGINT},
 		registrarTimeout: 10 * time.Second,
 		stopTimeout:      10 * time.Second,
@@ -50,6 +49,9 @@ func New(opts ...Option) *App {
 	for _, opt := range opts {
 		opt(&o)
 	}
+	if o.logger != nil {
+		log.SetLogger(o.logger)
+	}
 	ctx, cancel := context.WithCancel(o.ctx)
 	return &App{
 		ctx:    ctx,
@@ -72,10 +74,10 @@ func (a *App) Metadata() map[string]string { return a.opts.metadata }
 
 // Endpoint returns endpoints.
 func (a *App) Endpoint() []string {
-	if a.instance == nil {
-		return []string{}
+	if a.instance != nil {
+		return a.instance.Endpoints
 	}
-	return a.instance.Endpoints
+	return nil
 }
 
 // Run executes all OnStart hooks registered with the application's Lifecycle.
@@ -84,6 +86,9 @@ func (a *App) Run() error {
 	if err != nil {
 		return err
 	}
+	a.mu.Lock()
+	a.instance = instance
+	a.mu.Unlock()
 	eg, ctx := errgroup.WithContext(NewContext(a.ctx, a))
 	wg := sync.WaitGroup{}
 	for _, srv := range a.opts.servers {
@@ -107,9 +112,6 @@ func (a *App) Run() error {
 		if err := a.opts.registrar.Register(rctx, instance); err != nil {
 			return err
 		}
-		a.lk.Lock()
-		a.instance = instance
-		a.lk.Unlock()
 	}
 	c := make(chan os.Signal, 1)
 	signal.Notify(c, a.opts.sigs...)
@@ -120,7 +122,6 @@ func (a *App) Run() error {
 				return ctx.Err()
 			case <-c:
 				if err := a.Stop(); err != nil {
-					a.opts.logger.Errorf("failed to stop app: %v", err)
 					return err
 				}
 			}
@@ -134,9 +135,9 @@ func (a *App) Run() error {
 
 // Stop gracefully stops the application.
 func (a *App) Stop() error {
-	a.lk.Lock()
+	a.mu.Lock()
 	instance := a.instance
-	a.lk.Unlock()
+	a.mu.Unlock()
 	if a.opts.registrar != nil && instance != nil {
 		ctx, cancel := context.WithTimeout(NewContext(a.ctx, a), a.opts.registrarTimeout)
 		defer cancel()
diff --git a/options.go b/options.go
index d18d75da..8f9e9a55 100644
--- a/options.go
+++ b/options.go
@@ -25,7 +25,7 @@ type options struct {
 	ctx  context.Context
 	sigs []os.Signal
 
-	logger           *log.Helper
+	logger           log.Logger
 	registrar        registry.Registrar
 	registrarTimeout time.Duration
 	stopTimeout      time.Duration
@@ -64,9 +64,7 @@ func Context(ctx context.Context) Option {
 
 // Logger with service logger.
 func Logger(logger log.Logger) Option {
-	return func(o *options) {
-		o.logger = log.NewHelper(logger)
-	}
+	return func(o *options) { o.logger = logger }
 }
 
 // Server with transport servers.


Test patch:
diff --git a/app_test.go b/app_test.go
index 9165ce07..80831b54 100644
--- a/app_test.go
+++ b/app_test.go
@@ -154,7 +154,7 @@ func TestApp_Endpoint(t *testing.T) {
 				endpoint []string
 				metadata map[string]string
 			}{
-				id: "3", version: "v3", name: "kratos-v3", endpoint: []string{},
+				id: "3", version: "v3", name: "kratos-v3", endpoint: nil,
 				metadata: map[string]string{},
 			},
 		},
diff --git a/options_test.go b/options_test.go
index 953c52f5..4880bd19 100644
--- a/options_test.go
+++ b/options_test.go
@@ -79,7 +79,7 @@ func TestLogger(t *testing.T) {
 	o := &options{}
 	v := xlog.NewStdLogger(log.Writer())
 	Logger(v)(o)
-	if !reflect.DeepEqual(xlog.NewHelper(v), o.logger) {
+	if !reflect.DeepEqual(v, o.logger) {
 		t.Fatalf("o.logger:%v is not equal to xlog.NewHelper(v):%v", o.logger, xlog.NewHelper(v))
 	}
 }