Pregunta

I'm creating an Admin Tool which starts different tasks on demand. I am experiencing the following oddity: I am not able to start an application with ProcessBuilder or Runtime.exec and log4j (1.2.17) logging to consoles. Without logging to consoles it works just fine. With logging to files for example it also works just fine. Manually starting the jar it from commandline with logging to console enabled also works fine.

For obvious reasons I would love it, if I would not have to manually add a line for removing the console appender everytime I do test the jar with the Admin Tool.

my log4j Appender:

<appender name="console" class="org.apache.log4j.ConsoleAppender">
    <param name="Target" value="System.out" />
    <layout class="org.apache.log4j.PatternLayout">
    <param name="ConversionPattern" value="%-5p %c{1} - %m%n" />
    </layout>
</appender>

my code to start the jar: ArrayList commands = new ArrayList(); commands.add("java"); commands.add("-jar"); commands.add(file.getAbsolutePath());

for (CommandLineArgument arg : args) {
    commands.add("-" + arg.getKey() + "=" + arg.getDefaultArgument());
}
ProcessBuilder processBuilder = new ProcessBuilder(commands);

DateFormat dateFormat = new SimpleDateFormat("-dd-MM-yyyy-HH-mm-ss");
String logFileName = baseLogDir + processId + dateFormat.format(new Date()) + ".log";

File stdOutFile = new File(logFileName + ".stdout");
if(!stdOutFile.exists())
    stdOutFile.createNewFile();
processBuilder.redirectInput(stdOutFile);
log.debug("redirecting stdout to " + stdOutFile.getAbsolutePath());

File stdErrFile = new File(logFileName + ".stderr");
if(!stdErrFile.exists())
    stdErrFile.createNewFile();
processBuilder.redirectError(stdErrFile);
log.debug("redirecting stderr to " + stdErrFile.getAbsolutePath());

log.info("starting module with the following command: " + processBuilder.command());
return processBuilder.start();
¿Fue útil?

Solución

I solved the problem.

For future reference: Remove the <param name="Target" value="System.out" /> from your log4j appender. It does not write to std.out afterwards, but it does start just fine. Maybe its time to migrate to log4j2, so maybe this problem does not exist in log4j2.

The final appender:

<appender name="console" class="org.apache.log4j.ConsoleAppender">
    <layout class="org.apache.log4j.PatternLayout">
    <param name="ConversionPattern" value="%-5p %c{1} - %m%n" />
    </layout>
</appender>
Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top