앱)Slf4j API print format 정리 예제

Posted by HULIA(휴리아)
2018. 1. 31. 10:19 백엔드개발/자바스프링
debug, info, warn, fatal, error 모두 같은 형식을 지원하므로
 debug만 대표적으로 알아봄

slf4j.debug 메소드의 overloading 메소드 정리
debug(String arg0);
debug(Marker arg0, String arg1);
debug(String arg0, Object arg1);
debug(String arg0, Object...arg1);
debug(String arg0, Throwable arg1);
debug(Marker arg0, String arg1, Object arg2);
debug(Marker arg0, String arg1, Object...arg2);
debug(Marker arg0, String arg1, Throwable arg2);
debug(String arg0, Ojbect arg1, Obejct arg2);
debug(Marker arg0, String arg1, Object arg2, Object arg3);



Marker
Marker fatal = MarkerFactory.getMarker("FATAL");

Logger
Logger logger = LoggerFactory.getLogger("LOG");

Throwable과 Exception은 비슷
try-catch문에서 catch문에서 Exception출력을 할때 이용하거나 Exception을 throw 할때 사용


예시1)
logger.error(fatal, "Failed to obtain JDBC connection", e);

예시2)
logger.debug("Entry number: " + i + "is" + String.valueOf(entry[i]));

예시3)
Object entry = new SomeObject();//자바의 모든 객체 다 쓸 수 있음
logger.debug("The entry is {}" , entry);
//출력 : The entry is 3

예시4)
logger.debug("The new entry is {}. It replaces {}.", entry, oldEntry);
//출력 : The new entry is 3. It replaces 6.

예시5)
logger.debug("Value {} was inserted between {} and {}.", newVal, below, above);
//출력: Value 7 was inserted between 5 and 10.


예시6)
logger.debug("Set {1,2} differs from {{}}", "3");
//출력 : Set {1,2} differs from {3}


예시7)
logger.debug("Set \\{} differs from {}", "3");
//출력 : Set {} differs from 3


예시8)
logger.debug("File name is c:\\\\{}.", "file.zip");
//출력 : File name is c:\file.zip.


예시9)
String s = "hello world";
try {
Integer i = Integer.valueOf(s);
}catch(NumberFormatException e) {
 logger.error("Failed to format {}, s, e);
}


예시10)
public class LoggerUtil {
public static void debug (Logger logger,String msg,Object...params){
if (logger.isDebugEnabled ()){
logger.debug (msg,params);
}
}
}