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

[Bug] Parameters not in order #155

Open
toni-moreno opened this issue Jun 1, 2019 · 10 comments
Open

[Bug] Parameters not in order #155

toni-moreno opened this issue Jun 1, 2019 · 10 comments

Comments

@toni-moreno
Copy link

Hi We are working with zerolog to create a kind of HTTP access.log and as mentioned here #50 (comment) I need the order I use in the code.

Here the code.

https://github.com/toni-moreno/influxdb-srelay/blob/0e7f56f7c6e3f626773fa17d88b33ad20b453fda/relay/http_middlewares.go#L69-L84

		h.acclog.Info().
			Str("trace-route", rc.TraceRoute.String()).
			Str("referer", r.Referer()).
			Str("url", r.URL.String()).
			Int("write-size", rc.RequestSize).
			Int("write-points", rc.RequestPoints).
			Int("returnsize", rc.SentDataLength).
			Dur("duration_ms", time.Since(rc.InputTime)).
			Dur("bk_duration_ms", time.Since(rc.BackendTime)).
			Dur("latency_ms", rc.BackendTime.Sub(rc.InputTime)).
			Int("status", rc.SentHTTPStatus).
			Str("method", r.Method).
			Str("user", utils.GetUserFromRequest(r)).
			Str("source", r.RemoteAddr).
			Str("user-agent", r.UserAgent()).
			Msg("")

And here the output with the consolewritter

2019-06-01 07:44:20 INF  bk_duration_ms=30.448107 duration_ms=113.510554 latency_ms=83.075333 method=POST referer= returnsize=0 source=127.0.0.1:45078 status=204 trace-route="http:example-http-influxdb2> rt:telegraf> decode:ILP> rule:rename_mem_measurement_to_mem_2> rule:to_namespace_metrics> rule:route_all_data_to_cluster_linux> " url=/write?db=telegraf user=admin user-agent=telegraf write-points=23 write-size=4770

As you can see there is no order in the output

@rs
Copy link
Owner

rs commented Jun 1, 2019

The json out is ordered as written in the code. The ConsoleWriter does not keep this order because it does umarshaling of the json to format it.

@toni-moreno
Copy link
Author

Hi @rs thank you for the fast response.

There is any workaround to force an specific order also with ConsoleWriter?

@rs
Copy link
Owner

rs commented Jun 1, 2019

We would need to change the way the json is unmarshaled. Possible but requires a patch.

@TommyCpp
Copy link

TommyCpp commented Jul 21, 2019

Hi I came across this issue and I think maybe we can add a function in ConsoleWriter that allows user to customize the order of output field?

@toni-moreno
Copy link
Author

Hi @TommyCpp this sounds great!

@BenjamenMeyer
Copy link

Ordering by the JSON keys is important as it allows one to more easily parse/read through the logs since fields are in the same order. Having random ordering of the key-values make it hard to read when going through logs.

If your only goal is to load into an ELK stack or similar then ordering may not matter as much. However, developers often need to be able to read log files locally and this makes a huge difference.

Having ordering as an option that can be enabled/disabled would still allow for the increased speed where folks don't care, and the negligible slowdown when they do.

@WQPhello
Copy link

I completely agree with your point of view. Formatted logs can help developers, testers, and operations personnel quickly read them without the need for complex searching and eye scanning.

@madkins23
Copy link
Contributor

Just submitted PR #550.

@heaven
Copy link

heaven commented May 4, 2024

Any chance for this to get merged?

@phuslu
Copy link

phuslu commented May 4, 2024

I think it's hard/impossible to fix completely on top of stdlib, that's why I develop a home made json parser based on gjson to output text fields same as logging statement. https://github.com/phuslu/log/blob/master/formatter.go

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

No branches or pull requests

8 participants