Repository: zeromicro/go-zero
Pull request #2946 (2023-03-01 10:05:51)

feat: add debug log for logc

codecov[bot]: # [Codecov](https://codecov.io/gh/zeromicro/go-zero/pull/2946?src=pr&el=h1&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=None) Report
> Merging [#2946](https://codecov.io/gh/zeromicro/go-zero/pull/2946?src=pr&el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=None) (0bcb77c) into [master](https://codecov.io/gh/zeromicro/go-zero/commit/f3369f8e816d7bdeef970069b409ca3a017818bf?el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=None) (f3369f8) will **increase** coverage by `0.13%`.
> The diff coverage is `0.00%`.

> :exclamation: Current head 0bcb77c differs from pull request most recent head 5ba63c2. Consider uploading reports for the commit 5ba63c2 to get more accurate results


| [Impacted Files](https://codecov.io/gh/zeromicro/go-zero/pull/2946?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=None) | Coverage Δ | |
|---|---|---|
| [core/logc/logs.go](https://codecov.io/gh/zeromicro/go-zero/pull/2946?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=None#diff-Y29yZS9sb2djL2xvZ3MuZ28=) | `84.00% <0.00%> (-16.00%)` | :arrow_down: |
| [core/stores/cache/cacheopt.go](https://codecov.io/gh/zeromicro/go-zero/pull/2946?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=None#diff-Y29yZS9zdG9yZXMvY2FjaGUvY2FjaGVvcHQuZ28=) | `45.00% <0.00%> (-55.00%)` | :arrow_down: |
| [core/stores/cache/cachenode.go](https://codecov.io/gh/zeromicro/go-zero/pull/2946?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=None#diff-Y29yZS9zdG9yZXMvY2FjaGUvY2FjaGVub2RlLmdv) | `45.89% <0.00%> (-44.53%)` | :arrow_down: |
| [core/stores/cache/cachestat.go](https://codecov.io/gh/zeromicro/go-zero/pull/2946?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=None#diff-Y29yZS9zdG9yZXMvY2FjaGUvY2FjaGVzdGF0Lmdv) | `93.54% <0.00%> (-6.46%)` | :arrow_down: |
| [core/stores/sqlx/trace.go](https://codecov.io/gh/zeromicro/go-zero/pull/2946?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=None#diff-Y29yZS9zdG9yZXMvc3FseC90cmFjZS5nbw==) | `73.33% <0.00%> (-4.45%)` | :arrow_down: |
| [core/stores/sqlx/sqlconn.go](https://codecov.io/gh/zeromicro/go-zero/pull/2946?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=None#diff-Y29yZS9zdG9yZXMvc3FseC9zcWxjb25uLmdv) | `56.37% <0.00%> (-1.48%)` | :arrow_down: |
| [core/stores/sqlx/sqlmanager.go](https://codecov.io/gh/zeromicro/go-zero/pull/2946?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=None#diff-Y29yZS9zdG9yZXMvc3FseC9zcWxtYW5hZ2VyLmdv) | `67.74% <0.00%> (-0.83%)` | :arrow_down: |
| [core/mapping/unmarshaler.go](https://codecov.io/gh/zeromicro/go-zero/pull/2946?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=None#diff-Y29yZS9tYXBwaW5nL3VubWFyc2hhbGVyLmdv) | `88.97% <0.00%> (-0.37%)` | :arrow_down: |
| [core/stores/redis/redis.go](https://codecov.io/gh/zeromicro/go-zero/pull/2946?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=None#diff-Y29yZS9zdG9yZXMvcmVkaXMvcmVkaXMuZ28=) | `98.85% <0.00%> (-0.18%)` | :arrow_down: |
| [core/trace/utils.go](https://codecov.io/gh/zeromicro/go-zero/pull/2946?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=None#diff-Y29yZS90cmFjZS91dGlscy5nbw==) | `100.00% <0.00%> (ø)` | |
| ... and [34 more](https://codecov.io/gh/zeromicro/go-zero/pull/2946?src=pr&el=tree-more&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=None) | |


Patch:
diff --git a/core/logc/logs.go b/core/logc/logs.go
index ff413dcd..71233545 100644
--- a/core/logc/logs.go
+++ b/core/logc/logs.go
@@ -27,6 +27,26 @@ func Close() error {
 	return logx.Close()
 }
 
+// Debug writes v into access log.
+func Debug(ctx context.Context, v ...interface{}) {
+	getLogger(ctx).Debug(v...)
+}
+
+// Debugf writes v with format into access log.
+func Debugf(ctx context.Context, format string, v ...interface{}) {
+	getLogger(ctx).Debugf(format, v...)
+}
+
+// Debugv writes v into access log with json content.
+func Debugv(ctx context.Context, v interface{}) {
+	getLogger(ctx).Debugv(v)
+}
+
+// Debugw writes msg along with fields into access log.
+func Debugw(ctx context.Context, msg string, fields ...LogField) {
+	getLogger(ctx).Debugw(msg, fields...)
+}
+
 // Error writes v into error log.
 func Error(ctx context.Context, v ...any) {
 	getLogger(ctx).Error(v...)


Test patch:
diff --git a/core/logc/logs_test.go b/core/logc/logs_test.go
index 0e2f93b0..fd1825cd 100644
--- a/core/logc/logs_test.go
+++ b/core/logc/logs_test.go
@@ -140,6 +140,54 @@ func TestInfow(t *testing.T) {
 	assert.True(t, strings.Contains(buf.String(), fmt.Sprintf("%s:%d", file, line+1)))
 }
 
+func TestDebug(t *testing.T) {
+	var buf strings.Builder
+	writer := logx.NewWriter(&buf)
+	old := logx.Reset()
+	logx.SetWriter(writer)
+	defer logx.SetWriter(old)
+
+	file, line := getFileLine()
+	Debug(context.Background(), "foo")
+	assert.True(t, strings.Contains(buf.String(), fmt.Sprintf("%s:%d", file, line+1)))
+}
+
+func TestDebugf(t *testing.T) {
+	var buf strings.Builder
+	writer := logx.NewWriter(&buf)
+	old := logx.Reset()
+	logx.SetWriter(writer)
+	defer logx.SetWriter(old)
+
+	file, line := getFileLine()
+	Debugf(context.Background(), "foo %s", "bar")
+	assert.True(t, strings.Contains(buf.String(), fmt.Sprintf("%s:%d", file, line+1)))
+}
+
+func TestDebugv(t *testing.T) {
+	var buf strings.Builder
+	writer := logx.NewWriter(&buf)
+	old := logx.Reset()
+	logx.SetWriter(writer)
+	defer logx.SetWriter(old)
+
+	file, line := getFileLine()
+	Debugv(context.Background(), "foo")
+	assert.True(t, strings.Contains(buf.String(), fmt.Sprintf("%s:%d", file, line+1)))
+}
+
+func TestDebugw(t *testing.T) {
+	var buf strings.Builder
+	writer := logx.NewWriter(&buf)
+	old := logx.Reset()
+	logx.SetWriter(writer)
+	defer logx.SetWriter(old)
+
+	file, line := getFileLine()
+	Debugw(context.Background(), "foo", Field("a", "b"))
+	assert.True(t, strings.Contains(buf.String(), fmt.Sprintf("%s:%d", file, line+1)))
+}
+
 func TestMust(t *testing.T) {
 	assert.NotPanics(t, func() {
 		Must(nil)