From 140418b1f2c2dfa03987020b3f22a6c848027fb2 Mon Sep 17 00:00:00 2001 From: Alex Archambault Date: Wed, 26 Mar 2025 11:54:13 +0100 Subject: [PATCH] Adjust logback outfile file upon startup --- .../src/mill/runner/client/MillClientMain.java | 4 ++++ runner/resources/logback.xml | 2 +- runner/src/mill/runner/MillMain.scala | 12 ++++++++++++ runner/src/mill/runner/MillServerMain.scala | 2 ++ 4 files changed, 19 insertions(+), 1 deletion(-) diff --git a/runner/client/src/mill/runner/client/MillClientMain.java b/runner/client/src/mill/runner/client/MillClientMain.java index ca2744d7c17..a927b3198e1 100644 --- a/runner/client/src/mill/runner/client/MillClientMain.java +++ b/runner/client/src/mill/runner/client/MillClientMain.java @@ -17,6 +17,10 @@ */ public class MillClientMain { public static void main(String[] args) throws Exception { + + if (System.getProperty("mill.log.file") == null) + System.setProperty("mill.log.file", OutFiles.out + "/mill.log"); + boolean runNoServer = false; if (args.length > 0) { String firstArg = args[0]; diff --git a/runner/resources/logback.xml b/runner/resources/logback.xml index bd38360c7f0..200ab4b8963 100644 --- a/runner/resources/logback.xml +++ b/runner/resources/logback.xml @@ -1,6 +1,6 @@ - out/mill.log + ${mill.log.file:-out/mill.log} %date %level [%thread] %logger{36} %msg%n diff --git a/runner/src/mill/runner/MillMain.scala b/runner/src/mill/runner/MillMain.scala index 00e93bd3b5c..69d713e2f94 100644 --- a/runner/src/mill/runner/MillMain.scala +++ b/runner/src/mill/runner/MillMain.scala @@ -41,6 +41,7 @@ object MillMain { } def main(args: Array[String]): Unit = SystemStreams.withTopLevelSystemStreamProxy { + adjustLogbackOutputFile() val initialSystemStreams = SystemStreams.original // setup streams val (runnerStreams, cleanupStreams, bspLog) = @@ -432,6 +433,17 @@ object MillMain { for ((k, v) <- desiredProps) System.setProperty(k, v) } + // Logback is set up via a logback.xml file in resources, so that if an app + // tries to log via slf4j, its logs will go somewhere. We need to tell logback + // where to write its output file. The out directory might not be "out/", so we + // set a Java property, used in logback.xml and then read by logback, so that + // the logs go to a file under the actual out directory. + // This method should be called early on in Mill processes, not to miss any + // slf4j use. + def adjustLogbackOutputFile(): Unit = + if (!sys.props.contains("mill.log.file")) + sys.props("mill.log.file") = s"${OutFiles.out}/mill.log" + def withOutLock[T]( noBuildLock: Boolean, noWaitForBuildLock: Boolean, diff --git a/runner/src/mill/runner/MillServerMain.scala b/runner/src/mill/runner/MillServerMain.scala index 08064c22189..2a7a869496f 100644 --- a/runner/src/mill/runner/MillServerMain.scala +++ b/runner/src/mill/runner/MillServerMain.scala @@ -23,6 +23,8 @@ object MillServerMain { } ) + MillMain.adjustLogbackOutputFile() + val acceptTimeoutMillis = Try(System.getProperty("mill.server_timeout").toInt).getOrElse(30 * 60 * 1000) // 30 minutes