ETC
로깅을 println()으로 절대 사용하면 안되는 이유
728x90
반응형
@kotlin.internal.InlineOnly
public actual inline fun println() {
System.out.println()
}
public void println() {
newLine();
}
println()은 내부적으로 newLine()을 호출한다.
newLine의 구현부를 보자
private void newLine() {
try {
synchronized (this) {
ensureOpen();
textOut.newLine();
textOut.flushBuffer();
charOut.flushBuffer();
if (autoFlush)
out.flush();
}
}
catch (InterruptedIOException x) {
Thread.currentThread().interrupt();
}
catch (IOException x) {
trouble = true;
}
}
안에 synchronized가 붙어있는걸 볼 수 있다.
따라서 멀티 스레드 환경에서 println()을 사용하게 되면 락이 잡혀 성능저하가 발생할수 있다.
절대 운영중인 코드에서는 System.out.println()을 사용하면 안된다.
slf4j
println()대신 slf4j를 사용하자.
- slf4j를 사용하면 로그가 휘발되지 않고 파일로 남길 수 있다.
- debug, info, warn, error 등 로그별로 레벨을 지정할 수 있다.
- 로그가 발생한 날짜, 시각, 로그가 발생한 위치 등 최소한의 정보가 기록된다.
728x90
반응형
댓글