Home > Eclipse, Java > Make System.out.println() Rocks!

Make System.out.println() Rocks!

System.out.println() is very strong and useful debug tool for long time.
The problem is that we tend to don’t remove this code after we debug.

What if do we have a lots of codes which we forgot to remove, finally debug messages can be useless trashes.
But tracing location of println() code is very difficult since we can’t use break points on system console stream.

So, Here is a cool solution:

@Override
/**
 * @author Jeeeyul 2011. 11. 1. 오후 4:36:51
 * @since M1.10
 */
public class DebugStream extends PrintStream {
   private static final DebugStream INSTANCE = new DebugStream();

   public static void activate() {
      System.setOut(INSTANCE);
   }

   private DebugStream() {
      super(System.out);
   }

   @Override
   public void println(Object x) {
      showLocation();
      super.println(x);
   }

   @Override
   public void println(String x) {
      showLocation();
      super.println(x);
   }

   private void showLocation() {
      StackTraceElement element = Thread.currentThread().getStackTrace()[3];
      super.print(MessageFormat.format("({0}:{1, number,#}) : ", element.getFileName(), element.getLineNumber()));
   }
}

Just call DebugStream.activate() when your application start.

It converts messages in console view from:

Hello World.

into:

(MyHelloWorld.java:10) : Hello World.

Yes, you can click and jump to your System.out.println() code in Eclipse console view. It makes debugging so easy.

Categories: Eclipse, Java
  1. 2012/10/18 at 1:18 pm

    A more portable way is to SLF4J and be done with it. It makes life much easier for everybody.

    • 2012/10/18 at 2:03 pm

      This is not about logging. It’s about debugging. And it’s fully different idea.

      Displaying java source file name and line number is important to trace debugging point since Eclipse JDT Console can let user jump to source by clicking. This function is not provided by other logging libraries. (because they were not designed for Eclipse JDT Console)

      I think just one source file is more portable then using external libraries.

      • 2012/10/18 at 2:28 pm

        Thanks Jeeeyul. That clarifies a lot.

        Your article title should reflect this fact though. The “upgrade” to System.out.println should be logging, as System.out.println was never intended to be a debugging tool.

        BTW, SLF4J does support tracing of code location: http://www.slf4j.org/api/org/slf4j/spi/LocationAwareLogger.html

        So by using SLF4J, the logging code is standardized, and the logging configuration at runtime/debugging can be customized without changing app code.

        The “no dependencies” view is only applicable for small apps. Most apps require a whole bunch of libs than SLF4J, eg Apache Commons, Guava, Spring, etc. And lots of libraries already depend on SLF4J anyway.

      • 2014/09/23 at 6:37 pm

        SLF4J, or specifically Logback, can display the source file name line number, even customizable to a specified depth.

        See %file, %line, %caller at http://logback.qos.ch/manual/layouts.html

  2. Wolfgang
    2012/10/18 at 3:50 pm

    Thats fine now I can force a System.exit() whenever one of my colleagues uses System.out inside our own code instead of the given logging framework:)

    • 2012/10/18 at 3:52 pm

      That’s very fun punishment!

  3. Sebastien Gandon
    2012/10/18 at 5:05 pm

    That is a great tip, thank a lot.

  4. Jmini
    2012/10/18 at 5:15 pm

    Or you can add a Checkstyle rule that is looking for System.out.println in order to display a warning. That way you do not let a System.out.println by mistake in your code.

    See an example of checkstyle configuration at the end of this page:
    http://checkstyle.sourceforge.net/config_regexp.html

  5. Mo
    2012/10/18 at 11:24 pm

    You can also use breakpoints for System.outs. Just go to breakpoint properties and enter as “condition”:
    System.out.println(); return false;
    This way you don’t pollute your code and still get System.outs that can be activated, deactivated and deleted.

  6. 2012/10/19 at 3:17 am

    Neat trick!

  7. 2012/10/19 at 6:28 pm

    That is super useful. Thank you. Under which license is this example published? EPL?

    Thanks for posting your very useful examples and extentions.

  8. 2013/01/17 at 12:08 pm

    Nice tools.. no doubt..

  9. Adrian
    2015/01/27 at 7:15 am

    Very cool! Thanks, I will use this for sure.

    One thing to note is that if you are printing out int’s, or double’s etc. you also need to override the println’s for them.

  1. 2012/11/09 at 7:04 pm

Leave a reply to vogella Cancel reply