개발

java에서 클래스 명, 라인 번호 찍기.

애바른 2013. 9. 24. 11:56

java에서 클래스 명, 라인 번호 찍기.


java에서 php의 __FILE__ __LINE__과 유사한 기능을 구현하는 방법!!!



String a = "hahaha";


System.out.println("===========디버깅 시작했다~================");

System.out.print("file:" + (new Throwable()).getStackTrace()[0].getClassName() + "  line");

System.out.println((new Throwable()).getStackTrace()[0].getLineNumber());

System.out.println(a);

System.out.println("===========디버깅 끝났다~================");




eclipse의 경우 템플릿을 지정해놓으면 더 쉽게 할 수 있다.


도구->환경설정->Java->편집기->템플리트


새로작성


이름: dprt

컨텍스트: java

자동삽입에 체크

설명: 디버깅용 프린트

패턴:

//* debugging~~ ${date} ${time}

System.out.println("===========디버깅 시작했다~================");

System.out.println("time:" + new java.text.SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new java.util.Date()));

System.out.print(new Throwable().getStackTrace()[0].getClassName() +"." + new Throwable().getStackTrace()[0].getMethodName() + "()" );

System.out.println("  line: " + new Throwable().getStackTrace()[0].getLineNumber());

System.out.println(${cursor});

System.out.println("===========디버깅 끝났다~================");

// the end of debuuging.. */


요렇게 넣고 확인하면..


java 코드에서 "dprt"라고 치고 ctrl+space 치면 위의 구문들이 찍힌다. 디버깅용으로 굉장히 편하다.


log4j 써도 된다~


출처:http://iilii.egloos.com/2856745