1
0
Fork 0
mirror of https://github.com/NixOS/nix synced 2025-06-30 19:57:59 +02:00

Allow separate JSON logging

If the NIX_LOG_FILE environment variable is set, Nix will write JSON
log messages to that file in addition to the regular logger (e.g. the
progress bar).
This commit is contained in:
Eelco Dolstra 2025-02-17 16:36:02 +01:00
parent 8ef94c1114
commit 1f702cdb01
5 changed files with 131 additions and 0 deletions

View file

@ -267,6 +267,24 @@ Logger * makeJSONLogger(Descriptor fd)
return new JSONLogger(fd);
}
Logger * makeJSONLogger(const std::filesystem::path & path)
{
struct JSONFileLogger : JSONLogger {
AutoCloseFD fd;
JSONFileLogger(AutoCloseFD && fd)
: JSONLogger(fd.get())
, fd(std::move(fd))
{ }
};
auto fd{toDescriptor(open(path.c_str(), O_CREAT | O_APPEND | O_WRONLY, 0644))};
if (!fd)
throw SysError("opening log file '%1%'", path);
return new JSONFileLogger(std::move(fd));
}
static Logger::Fields getFields(nlohmann::json & json)
{
Logger::Fields fields;