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

libutil/logging: extend internal-json logger to make it more machine-readable #3847

Merged
merged 1 commit into from Aug 28, 2020

Conversation

Ma27
Copy link
Member

@Ma27 Ma27 commented Jul 21, 2020

The new error-format is pretty nice from a UX point-of-view, however
it's fairly hard to parse the output e.g. for editor plugins such as
vim-ale[1] that use nix-instantiate --parse to determine syntax errors in
Nix expression files.

This patch extends the internal-json logger by adding the fields
line, column and file to easily locate an error in a file and the
field raw_msg which contains the error-message itself without
code-lines and additional helpers.

An exemplary output may look like this:

[nix-shell]$ ./inst/bin/nix-instantiate ~/test.nix --log-format minimal
{"action":"msg","column":1,"file":"/home/ma27/test.nix","level":0,"line":4,"raw_msg":"syntax error, unexpected IF, expecting $end","msg":"<full error-msg with code-lines etc>"}

[1] https://github.com/dense-analysis/ale

@Kloenk
Copy link
Member

Kloenk commented Jul 22, 2020

Is Json a better name, because it's in a Json format?

Ma27 added a commit to Ma27/ale that referenced this pull request Jul 22, 2020
In the (unreleased) Nix 2.4 the error-messages have been reformatted[1].
This patch aims to retain proper `.nix`-support in `ale`, but is only
targeted for folks using an unstable Nix version.

Please note that this currently depends on a pending PR[2] which adds
support for minimal JSON error-outputs.

[1] NixOS/nix#3590
[2] NixOS/nix#3847
@Ma27
Copy link
Member Author

Ma27 commented Jul 22, 2020

Fair point. Can we agree on minimal-json though? That's IMHO better to make it distinguishable from internal-json (which I intentionally didn't choose as it basically puts the entire, pretty-printed error into a JSON).

cc @edolstra WDYT?

@edolstra
Copy link
Member

I don't really understand why we need another JSON output format when we already have one. What information can't you get from the existing format?

@Ma27
Copy link
Member Author

Ma27 commented Jul 22, 2020

@edolstra the current json format returns a fully detailed message containing code-lines etc. This is IMHO fairly hard to parse for editor-plugins where I only need the line-number, line-column and message (which is provided in single JSON-fields in my new logger).

Also, we probably want to use the full-blown human-readable message in the internal-json logger (AFAIU) since that's how log-messages are received from a remote builder.

Finally, the internal-json logger is subject to change between releases (according to the man-pages) which would mean that plugins have to be modified for each new Nix release (in the worst case).

I see two options to take care of it:

  • provide another logger for the purpose I described (which is currently the case in this PR)
  • modify the internal-json logger to define (1) a somewhat stable structure and (2) expose error-msg (without code-lines), line-nr and line-column as additional json fields.

I'm fine with either of those options, so feel free to decide which you prefer.

@Ma27
Copy link
Member Author

Ma27 commented Jul 22, 2020

cc @bburdette who also might be interested in reviewing this as they implemented the beautiful new error-messages.

@edolstra
Copy link
Member

I think it's better to improve the existing JSON logger than to add another one. The whole point of the JSON logger was that it's the last logger we'll ever need, since it should provide all information in a format that other tools can easily process.

Finally, the internal-json logger is subject to change between releases

I don't think that should be the case. Having a log format that can change in arbitrary ways is not very useful. I think it's more that clients should be prepared to handle message types or fields being added or removed. (Conversely though, who says that the a new logger wouldn't be subject to change?)

Also, we probably want to use the full-blown human-readable message in the internal-json logger (AFAIU) since that's how log-messages are received from a remote builder.

No, the daemon should send structured error messages to the client. Currently errors from the daemon don't get formatted very well. For instance, the daemon doesn't know the terminal width of the client. And it's hard for the client to add context to an error received from the daemon.

modify the internal-json logger to define (1) a somewhat stable structure and (2) expose error-msg (without code-lines), line-nr and line-column as additional json fields.

I think this is the best approach. The code lines can probably be put in a separate JSON field which tools like IDEs can ignore.

@Ma27
Copy link
Member Author

Ma27 commented Jul 22, 2020

Conversely though, who says that the a new logger wouldn't be subject to change?

Well, first of all it was explicitly stated in the man-pages. Also, error-information is only provided in the msg field which can be an arbitrary string (which is in this case a problem as we'd currently have to parse e.g. line-numbers etc. from it).

I think this is the best approach. The code lines can probably be put in a separate JSON field which tools like IDEs can ignore.

That's also perfectly fine for me. I'll update the PR later today, thanks for your feedback!

@Ma27 Ma27 changed the title libutil/logging: Add minimal log-format which aims to be easily machine-readable libutil/logging: extend internal-json logger to make it more machine-readable Jul 22, 2020
@Ma27
Copy link
Member Author

Ma27 commented Jul 22, 2020

@edolstra updated accordingly %)

Ma27 added a commit to Ma27/ale that referenced this pull request Jul 22, 2020
In the (unreleased) Nix 2.4 the error-messages have been reformatted[1].
This patch aims to retain proper `.nix`-support in `ale`, but is only
targeted for folks using an unstable Nix version.

Please note that this currently depends on a pending PR[2] which adds
support for minimal JSON error-outputs.

[1] NixOS/nix#3590
[2] NixOS/nix#3847
@Ericson2314
Copy link
Member

Maybe take a look at what i wrote in #3841 ? I think this separation of rendering and data would also be good for machine-readable logging; currently we bake a lot of data in the hint which is conceptually separate, and should be actually separate for machine-readable logging.

@bburdette
Copy link
Contributor

I like it! I could see adding the trace info into the json format if at some point there's a tool that needs it.

@Ma27
Copy link
Member Author

Ma27 commented Jul 24, 2020

@bburdette fair point, added traces as well.
@Ericson2314 AFAIU this is something where we'd have to refactor the error-API first, right? After reading through the issue I'd generally agree with you, but this seems slightly out of scope for this PR (unless I misunderstood your point).
@edolstra anything else tbd? :)

@Ericson2314
Copy link
Member

Yes I suppose this changes are independent of this PR.

src/libutil/logging.cc Outdated Show resolved Hide resolved
…e-readable

The new error-format is pretty nice from a UX point-of-view, however
it's fairly hard to parse the output e.g. for editor plugins such as
vim-ale[1] that use `nix-instantiate --parse` to determine syntax errors in
Nix expression files.

This patch extends the `internal-json` logger by adding the fields
`line`, `column` and `file` to easily locate an error in a file and the
field `raw_msg` which contains the error-message itself without
code-lines and additional helpers.

An exemplary output may look like this:

```
[nix-shell]$ ./inst/bin/nix-instantiate ~/test.nix --log-format minimal
{"action":"msg","column":1,"file":"/home/ma27/test.nix","level":0,"line":4,"raw_msg":"syntax error, unexpected IF, expecting $end","msg":"<full error-msg with code-lines etc>"}
```

[1] https://github.com/dense-analysis/ale
@Ma27
Copy link
Member Author

Ma27 commented Jul 28, 2020

afaics all comments are resolved. Anything else to add @edolstra?

@Ma27
Copy link
Member Author

Ma27 commented Aug 3, 2020

ping @edolstra anything else tbd to get this merged?

@Ma27
Copy link
Member Author

Ma27 commented Aug 18, 2020

ping @edolstra @Ericson2314

@edolstra edolstra merged commit 691a1bd into NixOS:master Aug 28, 2020
@Ma27 Ma27 deleted the minimal-logger branch August 28, 2020 09:03
Ma27 added a commit to Ma27/ale that referenced this pull request Sep 27, 2020
In the (unreleased) Nix 2.4 the error-messages have been reformatted[1].
This patch aims to retain proper `.nix`-support in `ale`, but is only
targeted for folks using an unstable Nix version.

Please note that this currently depends on a pending PR[2] which adds
support for minimal JSON error-outputs.

[1] NixOS/nix#3590
[2] NixOS/nix#3847
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

Successfully merging this pull request may close these issues.

None yet

5 participants