mirror of
https://github.com/NixOS/nix.git
synced 2025-11-17 07:52:43 +01:00
Add json-log-path setting
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).
This commit is contained in:
parent
0087188d47
commit
7240fb198f
8 changed files with 200 additions and 5 deletions
|
|
@ -7,6 +7,7 @@
|
|||
#include "nix/util/source-path.hh"
|
||||
#include "nix/util/position.hh"
|
||||
#include "nix/util/sync.hh"
|
||||
#include "nix/util/unix-domain-socket.hh"
|
||||
|
||||
#include <atomic>
|
||||
#include <sstream>
|
||||
|
|
@ -182,8 +183,12 @@ void to_json(nlohmann::json & json, std::shared_ptr<Pos> pos)
|
|||
|
||||
struct JSONLogger : Logger {
|
||||
Descriptor fd;
|
||||
bool includeNixPrefix;
|
||||
|
||||
JSONLogger(Descriptor fd) : fd(fd) { }
|
||||
JSONLogger(Descriptor fd, bool includeNixPrefix)
|
||||
: fd(fd)
|
||||
, includeNixPrefix(includeNixPrefix)
|
||||
{ }
|
||||
|
||||
bool isVerbose() override {
|
||||
return true;
|
||||
|
|
@ -212,7 +217,7 @@ struct JSONLogger : Logger {
|
|||
void write(const nlohmann::json & json)
|
||||
{
|
||||
auto line =
|
||||
"@nix " +
|
||||
(includeNixPrefix ? "@nix " : "") +
|
||||
json.dump(-1, ' ', false, nlohmann::json::error_handler_t::replace);
|
||||
|
||||
/* Acquire a lock to prevent log messages from clobbering each
|
||||
|
|
@ -300,9 +305,49 @@ struct JSONLogger : Logger {
|
|||
}
|
||||
};
|
||||
|
||||
std::unique_ptr<Logger> makeJSONLogger(Descriptor fd)
|
||||
std::unique_ptr<Logger> makeJSONLogger(Descriptor fd, bool includeNixPrefix)
|
||||
{
|
||||
return std::make_unique<JSONLogger>(fd);
|
||||
return std::make_unique<JSONLogger>(fd, includeNixPrefix);
|
||||
}
|
||||
|
||||
std::unique_ptr<Logger> makeJSONLogger(const std::filesystem::path & path, bool includeNixPrefix)
|
||||
{
|
||||
struct JSONFileLogger : JSONLogger {
|
||||
AutoCloseFD fd;
|
||||
|
||||
JSONFileLogger(AutoCloseFD && fd, bool includeNixPrefix)
|
||||
: JSONLogger(fd.get(), includeNixPrefix)
|
||||
, fd(std::move(fd))
|
||||
{ }
|
||||
};
|
||||
|
||||
AutoCloseFD fd =
|
||||
std::filesystem::is_socket(path)
|
||||
? connect(path)
|
||||
: toDescriptor(open(path.c_str(), O_CREAT | O_APPEND | O_WRONLY, 0644));
|
||||
if (!fd)
|
||||
throw SysError("opening log file %1%", path);
|
||||
|
||||
return std::make_unique<JSONFileLogger>(std::move(fd), includeNixPrefix);
|
||||
}
|
||||
|
||||
void applyJSONLogger()
|
||||
{
|
||||
if (!loggerSettings.jsonLogPath.get().empty()) {
|
||||
try {
|
||||
std::vector<std::unique_ptr<Logger>> loggers;
|
||||
loggers.push_back(makeJSONLogger(std::filesystem::path(loggerSettings.jsonLogPath.get()), false));
|
||||
try {
|
||||
logger = makeTeeLogger(std::move(logger), std::move(loggers));
|
||||
} catch (...) {
|
||||
// `logger` is now gone so give up.
|
||||
abort();
|
||||
}
|
||||
} catch (...) {
|
||||
ignoreExceptionExceptInterrupt();
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
static Logger::Fields getFields(nlohmann::json & json)
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue