Git Product home page Git Product logo

Comments (6)

inhere avatar inhere commented on August 18, 2024 1

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

from slog.

inhere avatar inhere commented on August 18, 2024

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

from slog.

zuozhehao avatar zuozhehao commented on August 18, 2024

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

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

from slog.

inhere avatar inhere commented on August 18, 2024

这种情况应该设置 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
}

from slog.

zuozhehao avatar zuozhehao commented on August 18, 2024

这种情况应该设置 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拿到参数断言判断下就可以做很多事情。

from slog.

inhere avatar inhere commented on August 18, 2024

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

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

from slog.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.