diff --git a/src/libstore/daemon.cc b/src/libstore/daemon.cc index 1013b23a3..32c8f4d2d 100644 --- a/src/libstore/daemon.cc +++ b/src/libstore/daemon.cc @@ -1050,17 +1050,7 @@ void processConnection( if (!recursive) { prevLogger_ = std::move(logger); logger = std::move(tunnelLogger_); - - if (!loggerSettings.jsonLogPath.get().empty()) { - try { - std::vector> loggers; - loggers.push_back(std::move(logger)); - loggers.push_back(makeJSONLogger(std::filesystem::path(loggerSettings.jsonLogPath.get()), false)); - logger = makeTeeLogger(std::move(loggers)); - } catch (...) { - ignoreExceptionExceptInterrupt(); - } - } + applyJSONLogger(); } unsigned int opCount = 0; diff --git a/src/libutil/logging.cc b/src/libutil/logging.cc index b4bca0b36..fd54cc580 100644 --- a/src/libutil/logging.cc +++ b/src/libutil/logging.cc @@ -341,6 +341,20 @@ std::unique_ptr makeJSONLogger(const std::filesystem::path & path, bool return std::make_unique(std::move(fd), includeNixPrefix); } +void applyJSONLogger() +{ + if (!loggerSettings.jsonLogPath.get().empty()) { + try { + std::vector> loggers; + loggers.push_back(std::move(logger)); + loggers.push_back(makeJSONLogger(std::filesystem::path(loggerSettings.jsonLogPath.get()), false)); + logger = makeTeeLogger(std::move(loggers)); + } catch (...) { + ignoreExceptionExceptInterrupt(); + } + } +} + static Logger::Fields getFields(nlohmann::json & json) { Logger::Fields fields; diff --git a/src/libutil/logging.hh b/src/libutil/logging.hh index 5b69f501c..290a49bb8 100644 --- a/src/libutil/logging.hh +++ b/src/libutil/logging.hh @@ -221,6 +221,8 @@ std::unique_ptr makeJSONLogger(Descriptor fd, bool includeNixPrefix = tr std::unique_ptr makeJSONLogger(const std::filesystem::path & path, bool includeNixPrefix = true); +void applyJSONLogger(); + /** * @param source A noun phrase describing the source of the message, e.g. "the builder". */ diff --git a/src/nix/main.cc b/src/nix/main.cc index f81a02ce6..a2dc371d4 100644 --- a/src/nix/main.cc +++ b/src/nix/main.cc @@ -487,16 +487,7 @@ void mainWrapped(int argc, char * * argv) if (!args.helpRequested && !args.completions) throw; } - if (!loggerSettings.jsonLogPath.get().empty()) { - try { - std::vector> loggers; - loggers.push_back(std::move(logger)); - loggers.push_back(makeJSONLogger(std::filesystem::path(loggerSettings.jsonLogPath.get()), false)); - logger = makeTeeLogger(std::move(loggers)); - } catch (...) { - ignoreExceptionExceptInterrupt(); - } - } + applyJSONLogger(); if (args.helpRequested) { std::vector subcommand;