diff --git a/java/runtime/src/main/java/io/ray/runtime/util/LoggingUtil.java b/java/runtime/src/main/java/io/ray/runtime/util/LoggingUtil.java index 42f874222344..f7320989ebc8 100644 --- a/java/runtime/src/main/java/io/ray/runtime/util/LoggingUtil.java +++ b/java/runtime/src/main/java/io/ray/runtime/util/LoggingUtil.java @@ -3,6 +3,7 @@ import com.typesafe.config.Config; import io.ray.runtime.config.RayConfig; import io.ray.runtime.generated.Common.WorkerType; +import java.io.FileWriter; import org.apache.commons.lang3.StringUtils; import org.apache.logging.log4j.Level; import org.apache.logging.log4j.core.LoggerContext; @@ -78,18 +79,20 @@ public static synchronized void setupLogging(RayConfig rayConfig) { RootLoggerComponentBuilder rootLoggerBuilder = globalConfigBuilder.newAsyncRootLogger(level); rootLoggerBuilder.addAttribute("RingBufferSize", "1048576"); final String javaWorkerLogName = "JavaWorkerLogToRollingFile"; + String logFileName = "java-worker-" + jobIdHex + "-" + SystemUtil.pid(); setupLogger( globalConfigBuilder, rayConfig.logDir, new RayConfig.LoggerConf( - javaWorkerLogName, - "java-worker-" + jobIdHex + "-" + SystemUtil.pid(), - config.getString("ray.logging.pattern")), + javaWorkerLogName, logFileName, config.getString("ray.logging.pattern")), maxFileSize, maxBackupFiles, null); rootLoggerBuilder.add(globalConfigBuilder.newAppenderRef(javaWorkerLogName)); globalConfigBuilder.add(rootLoggerBuilder); + // write `:job_id:` to the beginning of log file to conform + // to PR #31772 + writeJobId(rayConfig.logDir + "/" + logFileName + ".log", jobIdHex); /// Setup user loggers. for (RayConfig.LoggerConf conf : rayConfig.loggers) { final String logPattern = @@ -108,6 +111,15 @@ public static synchronized void setupLogging(RayConfig rayConfig) { } } + private static void writeJobId(String logFilePath, String jobIdHex) { + try (FileWriter writer = new FileWriter(logFilePath)) { + writer.write(":job_id:" + jobIdHex + "\n"); + } catch (Exception e) { + throw new RuntimeException( + "Failed to write job id, " + jobIdHex + ", to log file, " + logFilePath, e); + } + } + private static void setupUserLogger( ConfigurationBuilder globalConfigBuilder, String logDir,