diff --git a/doc/manual/command-ref/opt-common-syn.xml b/doc/manual/command-ref/opt-common-syn.xml
index b610b54b96202ce033bbba185b46a7642f341e7f..2660e3bb1cca8daf2e136cdfb0ad1432094658f9 100644
--- a/doc/manual/command-ref/opt-common-syn.xml
+++ b/doc/manual/command-ref/opt-common-syn.xml
@@ -1,5 +1,5 @@
 <nop xmlns="http://docbook.org/ns/docbook">
-  
+
 <arg><option>--help</option></arg>
 <arg><option>--version</option></arg>
 <arg rep='repeat'>
@@ -11,6 +11,10 @@
 <arg>
   <arg choice='plain'><option>--quiet</option></arg>
 </arg>
+<arg>
+  <option>--log-format</option>
+  <replaceable>format</replaceable>
+</arg>
 <arg>
   <group choice='plain'>
     <arg choice='plain'><option>--no-build-output</option></arg>
diff --git a/doc/manual/command-ref/opt-common.xml b/doc/manual/command-ref/opt-common.xml
index 0383bfaedf151ebe011438bd1c1b36cccb23e82f..a68eef1d0e7636ed1c4823d104f9cb4cd0513e2d 100644
--- a/doc/manual/command-ref/opt-common.xml
+++ b/doc/manual/command-ref/opt-common.xml
@@ -92,6 +92,37 @@
 </varlistentry>
 
 
+<varlistentry xml:id="opt-log-format"><term><option>--log-format</option> <replaceable>format</replaceable></term>
+
+  <listitem>
+
+  <para>This option can be used to change the output of the log format, with
+  <replaceable>format</replaceable> being one of:</para>
+
+  <variablelist>
+
+    <varlistentry><term>raw</term>
+    <listitem><para>This is the raw format, as outputted by nix-build.</para></listitem>
+    </varlistentry>
+
+    <varlistentry><term>internal-json</term>
+    <listitem><para>Outputs the logs in a structured manner. NOTE: the json schema is not guarantees to be stable between releases.</para></listitem>
+    </varlistentry>
+
+    <varlistentry><term>bar</term>
+    <listitem><para>Only display a progress bar during the builds.</para></listitem>
+    </varlistentry>
+
+    <varlistentry><term>bar-with-logs</term>
+    <listitem><para>Display the raw logs, with the progress bar at the bottom.</para></listitem>
+    </varlistentry>
+
+  </variablelist>
+
+  </listitem>
+
+</varlistentry>
+
 <varlistentry><term><option>--no-build-output</option> / <option>-Q</option></term>
 
   <listitem><para>By default, output written by builders to standard
diff --git a/src/libmain/common-args.cc b/src/libmain/common-args.cc
index 51e199ea5ceeadd63fe2df1f6b947df0388a39b3..051668e53a20d0a2ca53f12bcd52d6943c96e20c 100644
--- a/src/libmain/common-args.cc
+++ b/src/libmain/common-args.cc
@@ -1,5 +1,6 @@
 #include "common-args.hh"
 #include "globals.hh"
+#include "loggers.hh"
 
 namespace nix {
 
@@ -38,6 +39,14 @@ MixCommonArgs::MixCommonArgs(const string & programName)
         }},
     });
 
+    addFlag({
+        .longName = "log-format",
+        .description = "format of log output; \"raw\", \"internal-json\", \"bar\" "
+                        "or \"bar-with-logs\"",
+        .labels = {"format"},
+        .handler = {[](std::string format) { setLogFormat(format); }},
+    });
+
     addFlag({
         .longName = "max-jobs",
         .shortName = 'j',
diff --git a/src/libmain/loggers.cc b/src/libmain/loggers.cc
new file mode 100644
index 0000000000000000000000000000000000000000..c44bb640869a626c15792ffdffdf28fbb7e18ef7
--- /dev/null
+++ b/src/libmain/loggers.cc
@@ -0,0 +1,52 @@
+#include "loggers.hh"
+#include "progress-bar.hh"
+
+namespace nix {
+
+LogFormat defaultLogFormat = LogFormat::raw;
+
+LogFormat parseLogFormat(const std::string & logFormatStr) {
+    if (logFormatStr == "raw")
+        return LogFormat::raw;
+    else if (logFormatStr == "raw-with-logs")
+        return LogFormat::rawWithLogs;
+    else if (logFormatStr == "internal-json")
+        return LogFormat::internalJson;
+    else if (logFormatStr == "bar")
+        return LogFormat::bar;
+    else if (logFormatStr == "bar-with-logs")
+        return LogFormat::barWithLogs;
+    throw Error("option 'log-format' has an invalid value '%s'", logFormatStr);
+}
+
+Logger * makeDefaultLogger() {
+    switch (defaultLogFormat) {
+    case LogFormat::raw:
+        return makeSimpleLogger(false);
+    case LogFormat::rawWithLogs:
+        return makeSimpleLogger(true);
+    case LogFormat::internalJson:
+        return makeJSONLogger(*makeSimpleLogger());
+    case LogFormat::bar:
+        return makeProgressBar();
+    case LogFormat::barWithLogs:
+        return makeProgressBar(true);
+    default:
+        abort();
+    }
+}
+
+void setLogFormat(const std::string & logFormatStr) {
+    setLogFormat(parseLogFormat(logFormatStr));
+}
+
+void setLogFormat(const LogFormat & logFormat) {
+    defaultLogFormat = logFormat;
+    createDefaultLogger();
+}
+
+void createDefaultLogger() {
+    logger = makeDefaultLogger();
+}
+
+}
diff --git a/src/libmain/loggers.hh b/src/libmain/loggers.hh
new file mode 100644
index 0000000000000000000000000000000000000000..cada0311049feb32d8ac74fbb1727cad09b6a0a0
--- /dev/null
+++ b/src/libmain/loggers.hh
@@ -0,0 +1,20 @@
+#pragma once
+
+#include "types.hh"
+
+namespace nix {
+
+enum class LogFormat {
+  raw,
+  rawWithLogs,
+  internalJson,
+  bar,
+  barWithLogs,
+};
+
+void setLogFormat(const std::string & logFormatStr);
+void setLogFormat(const LogFormat & logFormat);
+
+void createDefaultLogger();
+
+}
diff --git a/src/nix/progress-bar.cc b/src/libmain/progress-bar.cc
similarity index 98%
rename from src/nix/progress-bar.cc
rename to src/libmain/progress-bar.cc
index c677010989eafa740b41c970a17dc43801b25610..b287de8a3bcdeb17c549e5486dd5e14e08181ccf 100644
--- a/src/nix/progress-bar.cc
+++ b/src/libmain/progress-bar.cc
@@ -106,7 +106,7 @@ public:
         updateThread.join();
     }
 
-    void stop()
+    void stop() override
     {
         auto state(state_.lock());
         if (!state->active) return;
@@ -119,6 +119,10 @@ public:
         quitCV.notify_one();
     }
 
+    bool isVerbose() override {
+        return printBuildLogs;
+    }
+
     void log(Verbosity lvl, const FormatOrString & fs) override
     {
         auto state(state_.lock());
@@ -457,11 +461,17 @@ public:
     }
 };
 
-void startProgressBar(bool printBuildLogs)
+Logger * makeProgressBar(bool printBuildLogs)
 {
-    logger = new ProgressBar(
+    return new ProgressBar(
         printBuildLogs,
-        isatty(STDERR_FILENO) && getEnv("TERM").value_or("dumb") != "dumb");
+        isatty(STDERR_FILENO) && getEnv("TERM").value_or("dumb") != "dumb"
+    );
+}
+
+void startProgressBar(bool printBuildLogs)
+{
+    logger = makeProgressBar(printBuildLogs);
 }
 
 void stopProgressBar()
diff --git a/src/nix/progress-bar.hh b/src/libmain/progress-bar.hh
similarity index 70%
rename from src/nix/progress-bar.hh
rename to src/libmain/progress-bar.hh
index 4d61175c24e4f7cfa384bcc1036aaf54c9ca5eec..7f0dafecf8ce3b6011ea032fea09620563fb015e 100644
--- a/src/nix/progress-bar.hh
+++ b/src/libmain/progress-bar.hh
@@ -4,6 +4,8 @@
 
 namespace nix {
 
+Logger * makeProgressBar(bool printBuildLogs = false);
+
 void startProgressBar(bool printBuildLogs = false);
 
 void stopProgressBar();
diff --git a/src/libmain/shared.cc b/src/libmain/shared.cc
index 70d1f0186c2646c1a5e80f9f2633189914bc6ddc..3bbb5cf9380ef4d0cdf75c4e1778a580bd143be7 100644
--- a/src/libmain/shared.cc
+++ b/src/libmain/shared.cc
@@ -2,6 +2,7 @@
 #include "shared.hh"
 #include "store-api.hh"
 #include "util.hh"
+#include "loggers.hh"
 
 #include <algorithm>
 #include <cctype>
@@ -169,7 +170,7 @@ LegacyArgs::LegacyArgs(const std::string & programName,
         .longName = "no-build-output",
         .shortName = 'Q',
         .description = "do not show build output",
-        .handler = {&settings.verboseBuild, false},
+        .handler = {[&]() {setLogFormat(LogFormat::raw); }},
     });
 
     addFlag({
diff --git a/src/libstore/build.cc b/src/libstore/build.cc
index 9582a90075852e3988b7a81166ee1922fa1664b1..bdf03ff944ffcfbe5e9921db6fde41bf21f59163 100644
--- a/src/libstore/build.cc
+++ b/src/libstore/build.cc
@@ -1642,7 +1642,7 @@ void DerivationGoal::buildDone()
                 worker.store.printStorePath(drvPath),
                 statusToString(status));
 
-            if (!settings.verboseBuild && !logTail.empty()) {
+            if (!logger->isVerbose() && !logTail.empty()) {
                 msg += (format("; last %d log lines:") % logTail.size()).str();
                 for (auto & line : logTail)
                     msg += "\n  " + line;
@@ -1691,11 +1691,7 @@ void DerivationGoal::buildDone()
                 }
 
                 void flushLine() {
-                    if (settings.verboseBuild) {
-                        printError("post-build-hook: " + currentLine);
-                    } else {
-                        act.result(resPostBuildLogLine, currentLine);
-                    }
+                    act.result(resPostBuildLogLine, currentLine);
                     currentLine.clear();
                 }
 
@@ -4155,13 +4151,8 @@ void DerivationGoal::flushLine()
         ;
 
     else {
-        if (settings.verboseBuild &&
-            (settings.printRepeatedBuilds || curRound == 1))
-            printError(currentLogLine);
-        else {
-            logTail.push_back(currentLogLine);
-            if (logTail.size() > settings.logLines) logTail.pop_front();
-        }
+        logTail.push_back(currentLogLine);
+        if (logTail.size() > settings.logLines) logTail.pop_front();
 
         act->result(resBuildLogLine, currentLogLine);
     }
diff --git a/src/libexpr/names.cc b/src/libstore/names.cc
similarity index 100%
rename from src/libexpr/names.cc
rename to src/libstore/names.cc
diff --git a/src/libexpr/names.hh b/src/libstore/names.hh
similarity index 100%
rename from src/libexpr/names.hh
rename to src/libstore/names.hh
diff --git a/src/libutil/logging.cc b/src/libutil/logging.cc
index 3cc4ef8f15b91a686aee5be19e01e6f772ad2413..15cbc1589ead4cbbb3f170ad1eb5bd84b744d528 100644
--- a/src/libutil/logging.cc
+++ b/src/libutil/logging.cc
@@ -18,7 +18,7 @@ void setCurActivity(const ActivityId activityId)
     curActivity = activityId;
 }
 
-Logger * logger = makeDefaultLogger();
+Logger * logger = makeSimpleLogger(true);
 
 void Logger::warn(const std::string & msg)
 {
@@ -35,13 +35,19 @@ class SimpleLogger : public Logger
 public:
 
     bool systemd, tty;
+    bool printBuildLogs;
 
-    SimpleLogger()
+    SimpleLogger(bool printBuildLogs)
+        : printBuildLogs(printBuildLogs)
     {
         systemd = getEnv("IN_SYSTEMD") == "1";
         tty = isatty(STDERR_FILENO);
     }
 
+    bool isVerbose() override {
+        return printBuildLogs;
+    }
+
     void log(Verbosity lvl, const FormatOrString & fs) override
     {
         if (lvl > verbosity) return;
@@ -70,6 +76,18 @@ public:
         if (lvl <= verbosity && !s.empty())
             log(lvl, s + "...");
     }
+
+    void result(ActivityId act, ResultType type, const Fields & fields) override
+    {
+        if (type == resBuildLogLine && printBuildLogs) {
+            auto lastLine = fields[0].s;
+            printError(lastLine);
+        }
+        else if (type == resPostBuildLogLine && printBuildLogs) {
+            auto lastLine = fields[0].s;
+            printError("post-build-hook: " + lastLine);
+        }
+    }
 };
 
 Verbosity verbosity = lvlInfo;
@@ -94,9 +112,9 @@ void writeToStderr(const string & s)
     }
 }
 
-Logger * makeDefaultLogger()
+Logger * makeSimpleLogger(bool printBuildLogs)
 {
-    return new SimpleLogger();
+    return new SimpleLogger(printBuildLogs);
 }
 
 std::atomic<uint64_t> nextId{(uint64_t) getpid() << 32};
@@ -114,6 +132,10 @@ struct JSONLogger : Logger
 
     JSONLogger(Logger & prevLogger) : prevLogger(prevLogger) { }
 
+    bool isVerbose() override {
+        return true;
+    }
+
     void addFields(nlohmann::json & json, const Fields & fields)
     {
         if (fields.empty()) return;
diff --git a/src/libutil/logging.hh b/src/libutil/logging.hh
index 18c24d50877806c93cdaf7b2eb410dcc83a7f833..e3d91e01fb47faa76076f576cd856c6e0bffde6d 100644
--- a/src/libutil/logging.hh
+++ b/src/libutil/logging.hh
@@ -63,6 +63,11 @@ public:
 
     virtual ~Logger() { }
 
+    virtual void stop() { };
+
+    // Whether the logger prints the whole build log
+    virtual bool isVerbose() { return false; }
+
     virtual void log(Verbosity lvl, const FormatOrString & fs) = 0;
 
     void log(const FormatOrString & fs)
@@ -141,7 +146,7 @@ struct PushActivity
 
 extern Logger * logger;
 
-Logger * makeDefaultLogger();
+Logger * makeSimpleLogger(bool printBuildLogs = true);
 
 Logger * makeJSONLogger(Logger & prevLogger);
 
diff --git a/src/libutil/util.cc b/src/libutil/util.cc
index 71db92d772aebe56c5b11caf9800ecd6ce78c53f..e0a99152b56e2d89bbb48c3590799665639755b5 100644
--- a/src/libutil/util.cc
+++ b/src/libutil/util.cc
@@ -989,7 +989,7 @@ pid_t startProcess(std::function<void()> fun, const ProcessOptions & options)
 {
     auto wrapper = [&]() {
         if (!options.allowVfork)
-            logger = makeDefaultLogger();
+            logger = makeSimpleLogger();
         try {
 #if __linux__
             if (options.dieWithParent && prctl(PR_SET_PDEATHSIG, SIGKILL) == -1)
diff --git a/src/nix-build/nix-build.cc b/src/nix-build/nix-build.cc
index 0a058a31b495bc418d1c5c79711d7ee2b6ff2866..8649de5e9e8839599f81f8fb01941924142aa50b 100755
--- a/src/nix-build/nix-build.cc
+++ b/src/nix-build/nix-build.cc
@@ -472,6 +472,8 @@ static void _main(int argc, char * * argv)
 
         restoreSignals();
 
+        logger->stop();
+
         execvp(shell->c_str(), argPtrs.data());
 
         throw SysError("executing shell '%s'", *shell);
@@ -521,6 +523,8 @@ static void _main(int argc, char * * argv)
             if (auto store2 = store.dynamic_pointer_cast<LocalFSStore>())
                 store2->addPermRoot(store->parseStorePath(symlink.second), absPath(symlink.first), true);
 
+        logger->stop();
+
         for (auto & path : outPaths)
             std::cout << path << '\n';
     }
diff --git a/src/nix-env/nix-env.cc b/src/nix-env/nix-env.cc
index d62febaff53dd0475b25b72a917f8e5103b47cf1..f7b04eb2bfa37bbf294c08cf32008739b85293b7 100644
--- a/src/nix-env/nix-env.cc
+++ b/src/nix-env/nix-env.cc
@@ -1446,6 +1446,8 @@ static int _main(int argc, char * * argv)
 
         globals.state->printStats();
 
+        logger->stop();
+
         return 0;
     }
 }
diff --git a/src/nix-prefetch-url/nix-prefetch-url.cc b/src/nix-prefetch-url/nix-prefetch-url.cc
index 5a686c8cd67cdc4596e6452acabf741011613679..b645bdc1b0aa254bff543172a80f596352c10658 100644
--- a/src/nix-prefetch-url/nix-prefetch-url.cc
+++ b/src/nix-prefetch-url/nix-prefetch-url.cc
@@ -8,7 +8,7 @@
 #include "attr-path.hh"
 #include "finally.hh"
 #include "../nix/legacy.hh"
-#include "../nix/progress-bar.hh"
+#include "progress-bar.hh"
 #include "tarfile.hh"
 
 #include <iostream>
diff --git a/src/nix-store/nix-store.cc b/src/nix-store/nix-store.cc
index 9491a0c26a1e3cf232f4e53089da24b93fd45aaf..ef8fb0951711780304ec7dfd7ac1eae475aa45af 100644
--- a/src/nix-store/nix-store.cc
+++ b/src/nix-store/nix-store.cc
@@ -1098,6 +1098,8 @@ static int _main(int argc, char * * argv)
 
         op(opFlags, opArgs);
 
+        logger->stop();
+
         return 0;
     }
 }
diff --git a/src/nix/main.cc b/src/nix/main.cc
index 1120ba5efa4bf7d08973a3c76d12e693f5c90792..203901168b93aff5784465a732c883aa127673d7 100644
--- a/src/nix/main.cc
+++ b/src/nix/main.cc
@@ -10,6 +10,7 @@
 #include "progress-bar.hh"
 #include "filetransfer.hh"
 #include "finally.hh"
+#include "loggers.hh"
 
 #include <sys/types.h>
 #include <sys/socket.h>
@@ -90,7 +91,7 @@ struct NixArgs : virtual MultiCommand, virtual MixCommonArgs
             .longName = "print-build-logs",
             .shortName = 'L',
             .description = "print full build logs on stderr",
-            .handler = {&printBuildLogs, true},
+            .handler = {[&]() {setLogFormat(LogFormat::barWithLogs); }},
         });
 
         addFlag({
@@ -165,6 +166,10 @@ void mainWrapped(int argc, char * * argv)
     verbosity = lvlWarn;
     settings.verboseBuild = false;
 
+    setLogFormat("bar");
+
+    Finally f([] { logger->stop(); });
+
     NixArgs args;
 
     args.parseCmdline(argvToStrings(argc, argv));
@@ -178,10 +183,6 @@ void mainWrapped(int argc, char * * argv)
         && args.command->first != "upgrade-nix")
         settings.requireExperimentalFeature("nix-command");
 
-    Finally f([]() { stopProgressBar(); });
-
-    startProgressBar(args.printBuildLogs);
-
     if (args.useNet && !haveInternet()) {
         warn("you don't have Internet access; disabling some network-dependent features");
         args.useNet = false;