1
0
Fork 0
mirror of https://github.com/NixOS/nix synced 2024-09-19 10:50:24 -04:00

Add basic json logger

It closely resembles the current Internal JSON logger, with the exception that we have eliminated the `@nix` prefix.

refs #7853
This commit is contained in:
Jean-François Roche 2023-08-21 19:37:02 +02:00
parent 10afcf06aa
commit 0fab0644da
4 changed files with 16 additions and 6 deletions

View file

@ -13,6 +13,8 @@ LogFormat parseLogFormat(const std::string & logFormatStr) {
return LogFormat::rawWithLogs;
else if (logFormatStr == "internal-json")
return LogFormat::internalJSON;
else if (logFormatStr == "json")
return LogFormat::json;
else if (logFormatStr == "bar")
return LogFormat::bar;
else if (logFormatStr == "bar-with-logs")
@ -27,7 +29,9 @@ Logger * makeDefaultLogger() {
case LogFormat::rawWithLogs:
return makeSimpleLogger(true);
case LogFormat::internalJSON:
return makeJSONLogger(*makeSimpleLogger(true));
return makeJSONLogger(*makeSimpleLogger(true), true);
case LogFormat::json:
return makeJSONLogger(*makeSimpleLogger(true), false);
case LogFormat::bar:
return makeProgressBar();
case LogFormat::barWithLogs: {

View file

@ -11,6 +11,7 @@ enum class LogFormat {
internalJSON,
bar,
barWithLogs,
json,
};
void setLogFormat(const std::string & logFormatStr);

View file

@ -150,8 +150,9 @@ void to_json(nlohmann::json & json, std::shared_ptr<AbstractPos> pos)
struct JSONLogger : Logger {
Logger & prevLogger;
bool internalJSON;
JSONLogger(Logger & prevLogger) : prevLogger(prevLogger) { }
JSONLogger(Logger & prevLogger, bool internalJSON) : prevLogger(prevLogger) { }
bool isVerbose() override {
return true;
@ -172,7 +173,11 @@ struct JSONLogger : Logger {
void write(const nlohmann::json & json)
{
if (internalJSON) {
prevLogger.log(lvlError, "@nix " + json.dump(-1, ' ', false, nlohmann::json::error_handler_t::replace));
} else {
prevLogger.log(lvlError, json.dump(-1, ' ', true, nlohmann::json::error_handler_t::replace));
}
}
void log(Verbosity lvl, std::string_view s) override
@ -244,9 +249,9 @@ struct JSONLogger : Logger {
}
};
Logger * makeJSONLogger(Logger & prevLogger)
Logger * makeJSONLogger(Logger & prevLogger, bool internalJSON)
{
return new JSONLogger(prevLogger);
return new JSONLogger(prevLogger, internalJSON);
}
static Logger::Fields getFields(nlohmann::json & json)

View file

@ -171,7 +171,7 @@ extern Logger * logger;
Logger * makeSimpleLogger(bool printBuildLogs = true);
Logger * makeJSONLogger(Logger & prevLogger);
Logger * makeJSONLogger(Logger & prevLogger, bool internalJSON = true);
std::optional<nlohmann::json> parseJSONMessage(const std::string & msg);