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

Logging option to format sink as JSON #2869

Closed
pascalgn opened this issue Nov 8, 2019 · 8 comments
Closed

Logging option to format sink as JSON #2869

pascalgn opened this issue Nov 8, 2019 · 8 comments
Labels
feature ⚙️ New feature or request
Milestone

Comments

@pascalgn
Copy link
Contributor

pascalgn commented Nov 8, 2019

1. What would you like to have changed?

Currently, the sink logger just outputs plain text:

2019/11/08 16:34:34 [INFO][cache:0xc0000bb270] Started certificate maintenance routine

It would be nice if there was an option where this could be encapsulated in a simple JSON structure, like

{ "msg": "2019/11/08 16:34:34 [INFO][cache:0xc0000bb270] Started certificate maintenance routine" }

2. Why is this feature a useful, necessary, and/or important addition to this project?

Makes it easier to send the log to Elastic (and probably also similar logging tools)

3. What alternatives are there, or what are you doing in the meantime to work around the lack of this feature?

It's possible to manually configure log line patterns in Elastic, but it's more effort and not very easy / user-friendly to do

4. Please link to any relevant issues, pull requests, or other discussions.

--

@pascalgn pascalgn added the feature ⚙️ New feature or request label Nov 8, 2019
@mholt
Copy link
Member

mholt commented Nov 8, 2019

We can customize only a few things about the sink logger, things which are exported here: https://golang.org/pkg/log/ -- so basically, the writer, and some "flags."

The best way to do this would be to make a log writer module that wraps its output in a JSON string. Depending on how this is implemented, it would have some cost in terms of performance. I bet it could be done without significant buffering and with using pools if necessary, though.

@mholt mholt added this to the 2.0 milestone Nov 8, 2019
@mholt mholt added the v2 label Nov 8, 2019
@pascalgn
Copy link
Contributor Author

pascalgn commented Nov 8, 2019

If possible, it would of course be nice if we could also extract the time and level information. However, I had a short look at the log module and I think the only option is log.SetOutput. That would mean there is no way to get these information directly, other than parsing the raw log line

@mholt
Copy link
Member

mholt commented Nov 8, 2019

Correct: since writers only get an array of bytes to work with, there has to be some parsing (hence the perf overhead).

I am also not 100% sure we can guarantee that each call to Write() correlates 1:1 with a log entry. From looking at the implementation of log.Output(), it appears that each log entry correlates 1:1 with calls to Write(), so that's convenient -- but the documentation does not explicitly guarantee this, I don't think (it just says that it will always end with a newline, but that doesn't mean that log entries can't have newlines in the middle of them too).

In any case, perhaps we can rest on the assumption that a single call to Write() consists of a single and complete log entry. If we assume that, then this problem is easier, since we can just parse out the timestamp.

Level information is definitely not structured -- there's absolutely no guarantee that the text within the [ ] after the timestamp will be the level. Perhaps it could be parsed and then compared against a list of known values, and then used as a level only if it matches a known level string.

Again, just adds performance overhead. But if that's OK, then I guess we can take a stab at it...

Want to give it a try?

@mholt mholt removed the v2 label Mar 23, 2020
@francislavoie
Copy link
Member

@mholt I think this is essentially done?

https://caddyserver.com/docs/json/logging/logs/encoder/json/

@mholt
Copy link
Member

mholt commented Apr 20, 2020

@francislavoie No, unfortunately that's not the sink log. The sink is just an io.Writer and that's about all right now because of the reasons mentioned above. There's not much we can do without structured log messages.

@francislavoie
Copy link
Member

francislavoie commented Aug 23, 2020

I think this is largely resolved at this point. CertMagic now uses zap for logging, and #3668 should resolve the only other major source of non-zap logs.

Should we close @mholt?

@mholt
Copy link
Member

mholt commented Aug 26, 2020

That PR only formats http.Server logs as JSON; it doesn't change the whole standard lib's default logger to JSON. We can probably do that pretty easily following a similar pattern. Do we feel that is the right thing to do?

@mholt
Copy link
Member

mholt commented Dec 2, 2020

Mmm, guess we'll close this and see if we get any other requests/complaints about sink log formats.

@mholt mholt closed this as completed Dec 2, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feature ⚙️ New feature or request
Projects
None yet
Development

No branches or pull requests

3 participants