Skip to content

Commit

Permalink
[Java] Prepend ":job_id:<jobid>" to java-worker-<jobid>-<pid>.log to …
Browse files Browse the repository at this point in the history
…make Java logging consistent with Python (ray-project#33665)

To make Java logging consistent with PR ray-project#31772 which seems for lazy worker binding. Otherwise, we may print too many logs from different drivers in shell console.

Co-authored-by: Qing Wang <[email protected]>
Signed-off-by: elliottower <[email protected]>
  • Loading branch information
2 people authored and elliottower committed Apr 22, 2023
1 parent 09cf678 commit 271e07c
Showing 1 changed file with 15 additions and 3 deletions.
18 changes: 15 additions & 3 deletions java/runtime/src/main/java/io/ray/runtime/util/LoggingUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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:<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 =
Expand All @@ -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<BuiltConfiguration> globalConfigBuilder,
String logDir,
Expand Down

0 comments on commit 271e07c

Please sign in to comment.