zeroturnaround / zt-exec

ZeroTurnaround Process Executor

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Need support for MDC logging context

maruthijr opened this issue · comments

When we use a logger to redirect process output, it loses any MDC logging context from the provided logger.
Currently ZT launches a new executor and a thread to execute the process. There is no way to set or pass the logging context to the process execution thread. This is a big pain point when we use ZT to execute processes in cloud where multiple threads, processes and machines are involved. Very hard to debug anything without a logging context.
Easiest way to do this in current zt code is to invoke one of the before listeners from the process thread and not from the calling thread. Alternate way is to provide hooks into the thread executor creation.
Sample code to show the problem:
`
import java.util.Map;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.slf4j.MDC;
import org.zeroturnaround.exec.ProcessExecutor;
import org.zeroturnaround.exec.stream.slf4j.Slf4jStream;

public class Main {
private static final Logger logger = LoggerFactory.getLogger(Main.class);

public static void main(String[] args) throws Exception {
	MDC.put("jobId", "12232");
	logger.info("Starting ls process");
	ProcessExecutor pe = new ProcessExecutor("ls", "-l");
	pe.redirectOutput(Slf4jStream.of(logger).asInfo());
	pe.execute();
	final Map<String,String> ctxMap = MDC.getCopyOfContextMap();
	new Thread(){
		public void run() {
			MDC.setContextMap(ctxMap);
			logger.info("Log from a child thread");
		};
	}.start();
}

}
`

Log output from the above code. Notice how the process output does not have the context info (jobId):

[main] [jobId=12232] INFO ztlogmdctest.Main - Starting ls process [main] [jobId=12232] DEBUG o.z.exec.ProcessExecutor - Executing [ls, -l]. [main] [jobId=12232] DEBUG o.z.exec.ProcessExecutor - Started java.lang.UNIXProcess@5b464ce8 [Thread-0] [] INFO ztlogmdctest.Main - total 8 [Thread-0] [] INFO ztlogmdctest.Main - -rw-r--r-- 1 maruthir staff 831 Jan 27 11:42 pom.xml [Thread-0] [] INFO ztlogmdctest.Main - drwxr-xr-x 4 maruthir staff 136 Jan 27 11:36 src [Thread-0] [] INFO ztlogmdctest.Main - drwxr-xr-x 4 maruthir staff 136 Jan 27 11:36 target [main] [jobId=12232] DEBUG o.zeroturnaround.exec.WaitForProcess - java.lang.UNIXProcess@5b464ce8 stopped with exit code 0 [Thread-1] [jobId=12232] INFO ztlogmdctest.Main - Log from a child thread

Hey,
thanks for the feedback!
Do those commits solve the case?

Wow that was quick! @reinra thanks for the quick fix. I just tried it and logging context now appeared only on the "Waiting for process.." message. The actual process output still did not have the context. A little debugging and I realized that the thread thats actually pushing out process output is the StreamPumper (Sorry did not see this earlier). I tried this fix and it seemed to work ok. Not sure if there are any other classes that need this treatment. Including the patch for your review and inclusion:
patch.txt

Thanks again

Sry for a delay. It shows that I either react quickly or forget about it for weeks.
Improved the code here: 29e32e0