Ad-hoc Logging using the Eclipse Debugger
Categories: Java
Sometimes while debugging a Java application you really need logging output rather than a debugger breakpoint. A colleague recently showed me a neat trick that causes a breakpoint to act like a logging statement - one that can be placed anywhere within the code at runtime (thanks, Rene!).
Within the Eclipse IDE’s debugger:
- set a breakpoint
- go to breakpoints view, right click and select “Breakpoint properties”
- set the breakpoint “condition” to something like this:
if(someObj.someMethod.contains("some text")) {
System.out.println("::::::" + someObj.someMethod());
}
return false;
Interestingly, this System.out output goes to the console of the app being debugged, not to the Eclipse IDE console. If desired, the condition could use an slf4j logger or similar rather than System.out.