Skip to content

Add json-log-path setting #13003

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

Open
wants to merge 2 commits into
base: master
Choose a base branch
from

Conversation

edolstra
Copy link
Member

Motivation

The json-log-path setting specifies a path (which can be a regular file or Unix domain socket) that receives a copy of all Nix log messages (in the same format used by --log-format internal-json but without the @nix prefixes). Internally this is implemented using the new TeeLogger class that dispatches log messages to multiple underlying loggers (i.e. the JSON logger and the regular progress bar).

The main use case is to provide an easy way in CI to process all Nix log messages centrally to extract hash mismatches, build/substitution results, etc.

Context


Add 👍 to pull requests you find important.

The Nix maintainer team uses a GitHub project board to schedule and track reviews.

@edolstra edolstra requested a review from Ericson2314 as a code owner April 11, 2025 14:52
@github-actions github-actions bot added documentation new-cli Relating to the "nix" command store Issues and pull requests concerning the Nix store labels Apr 11, 2025
@edolstra edolstra force-pushed the json-log-path branch 3 times, most recently from b177d16 to dff428c Compare April 11, 2025 15:11
@github-actions github-actions bot added the with-tests Issues related to testing. PRs with tests have some priority label Apr 11, 2025
This setting specifies a path (which can be a regular file or Unix
domain socket) that receives a copy of all Nix log messages (in JSON
format).
@Mic92
Copy link
Member

Mic92 commented Apr 11, 2025

cc @elikoga do you want to review this from an consumer perspective?

@elikoga
Copy link
Contributor

elikoga commented Apr 11, 2025

I'm surprised #12647 is already merged, I thought it'd arrive with a PR like this, seems I did not look too closely two weeks ago

Additionally, it seems like

    resHashMismatch = 109,
    resBuildResult = 110,

from the big DetSys PR in src/libutil/logging.hh is not included in this PR, which is reasonable for it to come later but it means that this does not involve a change in my internal-json parser.

I will verified the following though , once compilation on my end is done:

  • --log-format internal-json works as before
  • json-log-path output looks useful/as expected

I see this PR useful for users of nix in a terminal that want to have additional information accessible after a nix command execution while seeing the normal human-readable output in the terminal. For that it works great. I use --log-format internal-json by completely wrapping the output and reproducing it from the structured data for user consumption so this does not apply to me directly.

@elikoga
Copy link
Contributor

elikoga commented Apr 11, 2025

I believe json-log-path could be documented as a very very very verbose --verbose that does not make the terminal unreadable.

One should maybe warn the user of the resulting filesize, if one decides to run many commands with the setting and write the logs to a normal plain file without any compression, then the disk may run full.

-rw-r--r-- 1 elikoga users 131M Apr 12 01:13 nix.log

One build produced more than 100 megabytes of logs for me.

Useful feature in any case

@elikoga
Copy link
Contributor

elikoga commented Apr 11, 2025

The design could suggest a new useful log message type like commandInvoked, which would allow one to distinguish when a new command output log exists in a log file with outputs from multiple different invocations. That's probably one for the future though.

Setting<Path> jsonLogPath{
this, "", "json-log-path",
R"(
A path to which JSON records of Nix's log output will be
Copy link
Member

@Mic92 Mic92 Apr 15, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
A path to which JSON records of Nix's log output will be
A file or unix socket to which JSON records of Nix's log output will be

R"(
A path to which JSON records of Nix's log output will be
written, in the same format as `--log-format internal-json`
(without the `@nix ` prefixes on each line).
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Optional as suggested by the reviewer:

Suggested change
(without the `@nix ` prefixes on each line).
(without the `@nix ` prefixes on each line).
When writing to a file be aware that the output can in the order of 100MBs per build.

@nixos-discourse
Copy link

This pull request has been mentioned on NixOS Discourse. There might be relevant details there:

https://discourse.nixos.org/t/determinate-nix-3-3-json-logging-a-new-experience-around-hash-mismatches-and-more/63142/4

@Mic92
Copy link
Member

Mic92 commented Apr 17, 2025

@edolstra only some small doc changes and I think this is good to go?

AutoCloseFD fd =
std::filesystem::is_socket(path)
? connect(path)
: toDescriptor(open(path.c_str(), O_CREAT | O_APPEND | O_WRONLY, 0644));
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This will allow multiple Nix instances to append to the same file simultaneously, but the writing isn't synchronized between them, which will lead to interleaved (corrupted) entries, especially if some of the log lines are larger than the page size (e.g. long gcc invocation logs from make, but could be anything)

Comment on lines +209 to +213
/**
* Create a logger that sends log messages to `mainLogger` and the
* list of loggers in `extraLoggers`. Only `mainLogger` is used for
* writing to stdout and getting user input.
*/
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

thought, no action required:
Would be nice to split these concerns at some point. A UI is a logger, but a logger is not a UI, something like that.

@edolstra edolstra closed this Apr 23, 2025
@edolstra edolstra deleted the json-log-path branch April 23, 2025 10:29
@edolstra edolstra restored the json-log-path branch April 23, 2025 10:29
@Mic92 Mic92 reopened this Apr 23, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
documentation new-cli Relating to the "nix" command store Issues and pull requests concerning the Nix store with-tests Issues related to testing. PRs with tests have some priority
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants