Skip to content

fix: Logging middleware will be missing trace_id when logger is filterlogger #2571

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed

Conversation

LuoWeiJun-cynicism
Copy link

Description (what this PR does / why we need it):

修复启用链路追逐后log中间件使用logfilter后,打印的请求日志中缺失trace.id值,如下

INFO ts=2022-12-08T15:46:40+08:00 caller=log/log.go:30 trace.id= kind=server component=grpc operation=/video.basics.v1.notification.NotificationSrv/GetNotification args=webmaster_id:1595665043859443712 limit:20 code=0 reason= stack= latency=0.000850499

Which issue(s) this PR fixes (resolves / be part of):

fix/logfilter #2570

Other special notes for the reviewers:

应该在log.WithContext()方法中进一步断言logger类型,并取出初始logger的prefix

@LuoWeiJun-cynicism LuoWeiJun-cynicism changed the title fix: middleware log filter会忽略初始logger的prefix设置,如trace_id fix: middleware log 启用filterlog会导致初始logger的prefix设置,如trace_id,在log中间件打印的请求日志中缺失 Dec 8, 2022
@LuoWeiJun-cynicism LuoWeiJun-cynicism changed the title fix: middleware log 启用filterlog会导致初始logger的prefix设置,如trace_id,在log中间件打印的请求日志中缺失 fix: Logging middleware will be missing trace_id when logger is filterlogger Dec 8, 2022
@codecov-commenter
Copy link

codecov-commenter commented Jan 1, 2023

Codecov Report

Merging #2571 (dc60dfd) into main (33d51a8) will decrease coverage by 0.02%.
The diff coverage is 60.00%.

@@            Coverage Diff             @@
##             main    #2571      +/-   ##
==========================================
- Coverage   85.18%   85.15%   -0.03%     
==========================================
  Files          88       88              
  Lines        3840     3846       +6     
==========================================
+ Hits         3271     3275       +4     
- Misses        396      397       +1     
- Partials      173      174       +1     
Impacted Files Coverage Δ
log/log.go 88.57% <60.00%> (-4.77%) ⬇️
transport/http/codec.go 60.37% <0.00%> (+0.76%) ⬆️

Help us with your feedback. Take ten seconds to tell us how you rate us. Have a feature suggestion? Share it here.

@shenqidebaozi
Copy link
Member

@Cynicism-zx please fix lint failed

@LuoWeiJun-cynicism
Copy link
Author

@shenqidebaozi fixed

Comment on lines +58 to +64
f, ok := l.(*Filter)
if ok {
lg, ok := f.logger.(*logger)
if ok {
return &logger{logger: lg.logger, prefix: lg.prefix, hasValuer: lg.hasValuer, ctx: ctx}
}
}
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggest to replace the if !ok {...} block with something like the following to be able to handle nested Filters:

	for {
		if f, ok := l.(*Filter); ok {
			l = f.logger
		} else {
			break
		}
	}
	c, ok = l.(*logger)
	if !ok {
		return &logger{logger: l, ctx: ctx}
	}

@stonelgh
Copy link

@Cynicism-zx @shenqidebaozi Could you please comment?

@freezeChen
Copy link
Contributor

don't simple to using origin logger, It will cause the Filter not working.

freezeChen added a commit to freezeChen/kratos that referenced this pull request Jun 15, 2023
fix logging middle don't get trace_id value when logger is filterLogger

Which issue(s) this PR fixes (resolves / be part of):
 go-kratos#2570
 go-kratos#2571
shenqidebaozi pushed a commit that referenced this pull request Jun 16, 2023
…ger (#2869)

* Description (what this PR does / why we need it):
fix logging middle don't get trace_id value when logger is filterLogger

Which issue(s) this PR fixes (resolves / be part of):
 #2570
 #2571

* format

* fix:remove unuse test
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants