zerolog icon indicating copy to clipboard operation
zerolog copied to clipboard

how to refactor the output

Open en-one opened this issue 3 years ago • 3 comments

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")

en-one avatar Aug 08 '22 06:08 en-one

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)
}

gilcrest avatar Aug 08 '22 17:08 gilcrest

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)
}

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 ?

en-one avatar Aug 09 '22 01:08 en-one

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...

gilcrest avatar Aug 09 '22 06:08 gilcrest

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

en-one avatar Aug 25 '22 07:08 en-one