Repository: go-kratos/kratos
Pull request #2265 (2022-08-10 06:06:19)

Global logger
<!--
🎉 Thanks for sending a pull request to Kratos! Here are some tips for you:

1. If this is your first time contributing to Kratos, please read our contribution guide: https://go-kratos.dev/en/docs/community/contribution/
2. Ensure you have added or ran the appropriate tests and lint for your PR, please use `make lint` and `make test` before filing your PR, use `make clean` to tidy your go mod.
3. If the PR is unfinished, you may need to mark it as a WIP(Work In Progress) PR or draft PR
4. Please use a semantic commits format title, such as `<type>[optional scope]: <description>`, see: https://go-kratos.dev/docs/community/contribution#type
5. at the same time, please note that similar work should be submitted in one PR as far as possible to reduce the workload of reviewers. Do not split a work into multiple PR unless it should.
-->

<!--
🎉 感谢您向 Kratos 发送 PR 请求！以下是一些提示：
如果这是你第一次为 Kratos 贡献，请阅读我们的贡献指南：https://go-kratos.dev/en/docs/community/contribution/
2、确保您已经为您的 PR 添加或运行了适当的测试和lint，请在提交PR之前使用“make lint”和“make test”，使用“make clean”整理您的 go.mod。
3、如果请购单未完成，您可能需要将其标记为 WIP（在制品）PR 或 draft PR
4、请使用语义提交格式标题，如“<类型>[可选范围]：<说明>`，请参阅：https://go-kratos.dev/docs/community/contribution#type
5. 同时请注意，同类的工作请尽量在一个PR中提交，以减轻 review 者的工作负担，不要把一项工作拆分成很多个PR，除非它应该这样做。
-->

<!--
-->

#### Description (what this PR does / why we need it):

Global `GetLogger` return the inner Logger in global logger appliance, this will make inconsistent logger after `SetLogger` is called.

Return global logger appliance directly to wrap the inner logger, always make the all logger is same from `GetLogger`.


#### Which issue(s) this PR fixes (resolves / be part of):
<!--
* Automatically closes linked issue when PR is merged.
* If your PR is not fully resolved the issue, please use `part of #<issue number>` instead.

Usage: `fixes/resolves #<issue number>`, or `fixes/resolves (paste link of issue)`.
-->


#### Other special notes for the reviewers:
<!--
* Somethings that need extra attention for the reviewers
* Some additional notes, TODO list, etc.
-->


codecov-commenter: # [Codecov](https://codecov.io/gh/go-kratos/kratos/pull/2265?src=pr&el=h1&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=go-kratos) Report
> Merging [#2265](https://codecov.io/gh/go-kratos/kratos/pull/2265?src=pr&el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=go-kratos) (c344544) into [main](https://codecov.io/gh/go-kratos/kratos/commit/0aa9719e3a4ae9e412594a7f14e6b93d581b238d?el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=go-kratos) (0aa9719) will **decrease** coverage by `0.05%`.
> The diff coverage is `66.66%`.

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

```diff
@@            Coverage Diff             @@
##             main    #2265      +/-   ##
==========================================
- Coverage   85.63%   85.57%   -0.06%     
==========================================
  Files          87       87              
  Lines        3793     3793              
==========================================
- Hits         3248     3246       -2     
- Misses        378      380       +2     
  Partials      167      167              
```


| [Impacted Files](https://codecov.io/gh/go-kratos/kratos/pull/2265?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=go-kratos) | Coverage Δ | |
|---|---|---|
| [transport/grpc/resolver/direct/builder.go](https://codecov.io/gh/go-kratos/kratos/pull/2265/diff?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=go-kratos#diff-dHJhbnNwb3J0L2dycGMvcmVzb2x2ZXIvZGlyZWN0L2J1aWxkZXIuZ28=) | `100.00% <ø> (ø)` | |
| [transport/grpc/server.go](https://codecov.io/gh/go-kratos/kratos/pull/2265/diff?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=go-kratos#diff-dHJhbnNwb3J0L2dycGMvc2VydmVyLmdv) | `85.86% <ø> (ø)` | |
| [transport/http/server.go](https://codecov.io/gh/go-kratos/kratos/pull/2265/diff?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=go-kratos#diff-dHJhbnNwb3J0L2h0dHAvc2VydmVyLmdv) | `78.72% <ø> (ø)` | |
| [log/global.go](https://codecov.io/gh/go-kratos/kratos/pull/2265/diff?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=go-kratos#diff-bG9nL2dsb2JhbC5nbw==) | `77.55% <66.66%> (-4.09%)` | :arrow_down: |

Help us with your feedback. Take ten seconds to tell us [how you rate us](https://about.codecov.io/nps?utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=go-kratos). Have a feature suggestion? [Share it here.](https://app.codecov.io/gh/feedback/?utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=go-kratos)


Patch:
diff --git a/log/global.go b/log/global.go
index f75b07e6..624fccb3 100644
--- a/log/global.go
+++ b/log/global.go
@@ -27,8 +27,8 @@ func (a *loggerAppliance) SetLogger(in Logger) {
 	a.Logger = in
 }
 
-func (a *loggerAppliance) GetLogger() Logger {
-	return a.Logger
+func (a *loggerAppliance) Log(level Level, keyvals ...interface{}) error {
+	return a.Logger.Log(level, keyvals...)
 }
 
 // SetLogger should be called before any other log call.
@@ -39,7 +39,7 @@ func SetLogger(logger Logger) {
 
 // GetLogger returns global logger appliance as logger in current process.
 func GetLogger() Logger {
-	return global.GetLogger()
+	return global
 }
 
 // Log Print log by level and keyvals.


Test patch:
diff --git a/log/global_test.go b/log/global_test.go
index 04bb19c5..16aa6773 100644
--- a/log/global_test.go
+++ b/log/global_test.go
@@ -14,7 +14,7 @@ func TestGlobalLog(t *testing.T) {
 	logger := NewStdLogger(buffer)
 	SetLogger(logger)
 
-	if GetLogger() != logger {
+	if global.Logger != logger {
 		t.Error("GetLogger() is not equal to logger")
 	}