Package com.jcabi.log
Class VerboseProcess
java.lang.Object
com.jcabi.log.VerboseProcess
- All Implemented Interfaces:
Closeable,AutoCloseable
Utility class for getting
stdout from a running process
and logging it through SLF4J.
For example:
String name = new VerboseProcess(
new ProcessBuilder("who", "am", "i")
).stdout();
The class throws an exception if the process returns a non-zero exit code.
The class is thread-safe.
- Since:
- 0.5
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic final classClass representing the result of a process. -
Constructor Summary
ConstructorsConstructorDescriptionVerboseProcess(Process prc) Public ctor.VerboseProcess(ProcessBuilder builder) Public ctor (builder will be configured to redirect error input to thestdoutand will receive an emptystdin).VerboseProcess(ProcessBuilder bdr, Level stdout, Level stderr) Public ctor, with a given process and logging levels forstdoutandstderr.VerboseProcess(Process prc, Level stdout, Level stderr) Public ctor, with a given process and logging levels forstdoutandstderr. -
Method Summary
Modifier and TypeMethodDescriptionvoidclose()stdout()Getstdoutfrom the process, after its finish (the method will wait for the process and log its output).Getstdoutfrom the process, after its finish (the method will wait for the process and log its output).waitFor()Wait for the process to stop, logging its output in parallel.
-
Constructor Details
-
VerboseProcess
Public ctor.- Parameters:
prc- The process to work with
-
VerboseProcess
Public ctor (builder will be configured to redirect error input to thestdoutand will receive an emptystdin).- Parameters:
builder- Process builder to work with
-
VerboseProcess
Public ctor, with a given process and logging levels forstdoutandstderr. Neitherstdoutnorstderrcannot be set toLevel.ALLbecause it is intended to be used only for internal configuration.- Parameters:
prc- Process to execute and monitorstdout- Log level for stdoutstderr- Log level for stderr- Since:
- 0.11
-
VerboseProcess
Public ctor, with a given process and logging levels forstdoutandstderr.- Parameters:
bdr- Process builder to execute and monitorstdout- Log level for stdoutstderr- Log level for stderr- Since:
- 0.12
-
-
Method Details
-
stdout
Getstdoutfrom the process, after its finish (the method will wait for the process and log its output).The method will check process exit code, and if it won't be equal to zero a runtime exception will be thrown. A non-zero exit code usually is an indicator of problem. If you want to ignore this code, use
stdoutQuietly()instead.- Returns:
- Full
stdoutof the process
-
stdoutQuietly
Getstdoutfrom the process, after its finish (the method will wait for the process and log its output).This method ignores exit code of the process. Even if it is not equal to zero (which usually is an indicator of an error), the method will quietly return its output. The method is useful when you're running a background process. You will kill it with
Process.destroy(), which usually will lead to a non-zero exit code, which you want to ignore.- Returns:
- Full
stdoutof the process - Since:
- 0.10
-
waitFor
Wait for the process to stop, logging its output in parallel.- Returns:
- Stdout produced by the process
- Throws:
InterruptedException- If interrupted in between
-
close
public void close()- Specified by:
closein interfaceAutoCloseable- Specified by:
closein interfaceCloseable
-