본문 바로가기

Programming!

System.out.println 을 집어치워! 라지만....


System.out.println 을 집어치워!

http://www.vipan.com/htdocs/log4jhelp.html

미안하네, 하지만 난 그렇게 못하겠네... 재미가 솔솔하거든...친구.

log4j 의 로그레벨

DEBUG - application 의 Debug 시 대게 사용됩니다.
INFO - application 의 진행 사항등 정보/알림 정도에서 사용됩니다.
WARN - 오류는 아니지만 잠재적 해악( ? ) 에 사용됩니다.
ERROR - application 내 진행 가능한 오류 메세지로 사용됩니다.
FATAL - 심각한 문제의 발생시 사용합니다.

가령 log level을 INFO로 지정했을 경우, DEBUG 레벨의 로그는 보이지 않습니다.

log4j.properties

시작하기

  • 기본
    log4j.rootLogger=DEBUG, console
    log4j.appender.console=org.apache.log4j.ConsoleAppender
    log4j.appender.console.layout=org.apache.log4j.PatternLayout
    log4j.appender.console.layout.ConversionPattern=%d %p [%c] - %m%n
    • 최상위 레벨은 DEBUG 이며,
    • Console Appender로 출력한다.
    • 패턴 Layout 클래스는 log4j에서 제공하는 Patternlayout 을 사용하며
    • 패턴은 %d %p %c - %m%n 이다. - '날짜' '로깅이벤트 priority출력[ DEBUG, INFO, ERROR... ]' '-' '로깅메세지' '플랫폼에 독립적인 한라인내리기' -
    • 결과
      2009-12-22 14:07:39,140 INFO [com.tistory.eclipse4j.TestLogging] - Just testing a log message with priority set to INFO
      2009-12-22 14:07:39,140 WARN [com.tistory.eclipse4j.TestLogging] - Just testing a log message with priority set to WARN
      2009-12-22 14:07:39,140 ERROR [com.tistory.eclipse4j.TestLogging] - Just testing a log message with priority set to ERROR
      2009-12-22 14:07:39,140 FATAL [com.tistory.eclipse4j.TestLogging] - Just testing a log message with priority set to FATAL

몇가지 문제

  • 중복출력
    2009-12-22 14:07:39,140 INFO [com.tistory.eclipse4j.TestLogging] - Just testing a log message with priority set to INFO
    2009-12-22 14:07:39,140 INFO [com.tistory.eclipse4j.TestLogging] - Just testing a log message with priority set to INFO
    • 따라하기등을 이용해서 실행해봤을 경우 log가 중복출력되는 경우가 있다. RootLogger 와 지정 Logger의 중복으로 인한 문제다.- 계층구조에의한 -
    • 위의 링크를 통해 보면 알 수 있듯이 setAdditivity(false)가 필요하다.
    • log4j.properties 의 경우
      log4j.additivity.your.category.name=false
    • log4j.xml 의 경우
      <logger name="your.category.name" additivity="false">
      <level value="info" />
      <appender-ref ref="console" />
      </logger>
    • 개인적으로는 rootLogger에 모든걸 따르도록 놔두고 Appender를 이용하는 방법이 좋을 듯.
    • 개발시 모두 stdout 또는 eclipst tail plugins를 이용한 로깅보기, 운영시 Appender분리.