zerolog
                                
                                
                                
                                    zerolog copied to clipboard
                            
                            
                            
                        how to refactor the output
I want to refactor the fileds, integrating other fields, except "time" "level" "message"
from:
{"level":"info","bar":"baz","n":1,"time":"2022-08-08T14:24:08+08:00","message":"hello world"}
to:
{"level":"info","param":{"bar":"baz","n":1},"time":"2022-08-08T14:24:08+08:00","message":"hello world"}
What do I have to achieve?(not by "Dict")
You could use the RawJSON method. You could also add a method to a struct to return a zerolog.Dict or just use Dict... You can always use the Interface method as well if you're okay with reflection.
Below are some examples, that might help?
func main() {
	rawJSON()
	dict()
	dictStructMethod()
	iface()
}
func rawJSON() {
	p := param{Bar: "baz", N: 1}
	j, err := json.Marshal(p)
	if err != nil {
		panic(err)
	}
	log.Info().RawJSON("param", j).Msg("hello world")
	// {"level":"info","param":{"bar":"baz","n":1},"time":"2022-08-08T10:31:58-07:00","message":"hello world"}
}
func dict() {
	d := zerolog.Dict().Str("bar", "baz").Int("n", 1)
	log.Info().Dict("param", d).Msg("hello world")
	// {"level":"info","param":{"bar":"baz","n":1},"time":"2022-08-08T10:31:58-07:00","message":"hello world"}
}
func dictStructMethod() {
	p := param{Bar: "baz", N: 1}
	log.Info().Dict("param", p.dict()).Msg("hello world")
	// {"level":"info","param":{"bar":"baz","n":1},"time":"2022-08-08T10:31:58-07:00","message":"hello world"}
}
func iface() {
	p := param{Bar: "baz", N: 1}
	log.Info().Interface("param", p).Msg("hello world")
	// {"level":"info","param":{"bar":"baz","n":1},"time":"2022-08-08T10:31:58-07:00","message":"hello world"}
}}
type param struct {
	Bar string `json:"bar"`
	N   int    `json:"n"`
}
func (p *param) dict() *zerolog.Event {
	return zerolog.Dict().
		Str("bar", p.Bar).
		Int("n", p.N)
}
                                    
                                    
                                    
                                
You could use the
RawJSONmethod. You could also add a method to a struct to return azerolog.Dictor just useDict... You can always use theInterfacemethod as well if you're okay with reflection.Below are some examples, that might help?
func main() { rawJSON() dict() dictStructMethod() iface() } func rawJSON() { p := param{Bar: "baz", N: 1} j, err := json.Marshal(p) if err != nil { panic(err) } log.Info().RawJSON("param", j).Msg("hello world") // {"level":"info","param":{"bar":"baz","n":1},"time":"2022-08-08T10:31:58-07:00","message":"hello world"} } func dict() { d := zerolog.Dict().Str("bar", "baz").Int("n", 1) log.Info().Dict("param", d).Msg("hello world") // {"level":"info","param":{"bar":"baz","n":1},"time":"2022-08-08T10:31:58-07:00","message":"hello world"} } func dictStructMethod() { p := param{Bar: "baz", N: 1} log.Info().Dict("param", p.dict()).Msg("hello world") // {"level":"info","param":{"bar":"baz","n":1},"time":"2022-08-08T10:31:58-07:00","message":"hello world"} } func iface() { p := param{Bar: "baz", N: 1} log.Info().Interface("param", p).Msg("hello world") // {"level":"info","param":{"bar":"baz","n":1},"time":"2022-08-08T10:31:58-07:00","message":"hello world"} }} type param struct { Bar string `json:"bar"` N int `json:"n"` } func (p *param) dict() *zerolog.Event { return zerolog.Dict(). Str("bar", p.Bar). Int("n", p.N) }
First of all, thanks for your reply,
My current situation is that I have a completed log, such as {"level":"info","bar":"baz","n":1,"time":"2022-08-08T14:24:08+08:00","message":"hello world"}.
i want to refactor the log, collapse other fields except "time" "level" "message". i can't change the program code like log.Info().RawJSON("param", j).Msg("hello world") and fields are dynamic
How would I be able to do this? by "hook" or what ?
I'm not sure I understand when you say a "completed log" - do you mean you've already written the log to your io.writer and you want to take that result and refactor it? Sorry I may not understand what you mean and may not be that helpful...
I'm not sure I understand when you say a "completed log" - do you mean you've already written the log to your io.writer and you want to take that result and refactor it? Sorry I may not understand what you mean and may not be that helpful...
thanks , I have solved the problem