Resource Keep track of where you're System.out.println()ing from

Discussion in 'Spigot Plugin Development' started by JacobCrofts, May 29, 2016.

  1. Very simple but useful thing I learned today.

    Code (Text):
    public class Yapper {
     
        public static void yap(String message) {
            System.out.println("[YAPPER: " + getStackTraceElement().getFileName() + " " + getStackTraceElement().getLineNumber() + "] " + message);
        }
     
        private static StackTraceElement getStackTraceElement() {
            return Thread.currentThread().getStackTrace()[3];
        }
     
    }
    A plugin I'm working on now exceeds 12,000 lines. When I forget to remove a System.out.println() statement after I finish debugging, I may have trouble finding it again.

    With the Yapper class (or whatever you want to call it), the information you print will also include the class name and line number from which you called the method. As long as you always call Yapper.yap() instead of System.out.println(), you won't have to deal with this problem.

    Output:
    Code (Text):
    [12:58:03 INFO]: [YAPPER: CommandTest.java 25] wheee!
    credit
     
    • Like Like x 1
    • Useful Useful x 1
  2. Very useful, and I'll most definitely be using this, as I've ran into this mistake of leaving debug messages in when I'm finished debugging