Skip to content
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

如何获取原始参数 #104

Closed
zuozhehao opened this issue May 31, 2023 · 6 comments
Closed

如何获取原始参数 #104

zuozhehao opened this issue May 31, 2023 · 6 comments
Assignees
Labels
question Further information is requested resolved

Comments

@zuozhehao
Copy link

slog.PushHandlers(handler...)

func (n *notify) Handle(record *slog.Record) error {

	return nil
}

在record 如何获取slog.Error 传入的args

@inhere inhere added the question Further information is requested label May 31, 2023
@inhere
Copy link
Member

inhere commented May 31, 2023

handler 这里拿不到了, args 已经被格式化到 record.Message 了

@zuozhehao
Copy link
Author

zuozhehao commented Jun 12, 2023

handler 这里拿不到了, args 已经被格式化到 record.Message 了

是否有别的方法可以拿到,便于判断是否需要做通知 @inhere

@inhere
Copy link
Member

inhere commented Jun 13, 2023

这种情况应该设置 context data 或者 extra 数据, 使用更方便:

// add log
slog.WithData(slog.M{"notify": true}).Info("some message")

// in handler

func (n *notify) Handle(record *slog.Record) error {
	notify := record.Data["notify"]
	if notify {
		// do something ...
	}
	return nil
}

@zuozhehao
Copy link
Author

这种情况应该设置 context data 或者 extra 数据, 使用更方便:

// add log
slog.WithData(slog.M{"notify": true}).Info("some message")

// in handler

func (n *notify) Handle(record *slog.Record) error {
	notify := record.Data["notify"]
	if notify {
		// do something ...
	}
	return nil
}

@inhere
1、那这样就需要在所有地方都加上.WithData(slog.M{"notify": true})
2、对于那些返回自定义error 的有附加数据得map[string]any,那我还需要把map[string]any 转成slog.M

需要做以上两个处理才可以。

如果能在record 拿到args,那改动就很小,slog.Error(err) 只需在record拿到参数断言判断下就可以做很多事情。

@inhere
Copy link
Member

inhere commented Jun 14, 2023

记录下 args 也可以,我下个版本在 record 加个 Fmt, Args 备份。

@inhere
Copy link
Member

inhere commented Jun 19, 2023

发了 v0.5.2, logger 新增了选项:

	// BackupArgs backup log input args to Record.Args
	BackupArgs bool

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
question Further information is requested resolved
Projects
None yet
Development

No branches or pull requests

2 participants