What is Log4j?
Log4j is a Java library that specializes in logging. At its most basic level, you can think of it as a replacement for System.out.println's in your code. Why is it better than System.out.println's? The reasons are numerous.
To begin with, System.out.println outputs to standard output, which typically is a console window. The output from Log4j can go to the console, but it can also go to an email server, a database table, a log file, or various other destinations.
Another great benefit of Log4j is that different levels of logging can be set. The levels are hierarchical and are as follows: TRACE, DEBUG, INFO, WARN, ERROR, and FATAL. If you set a particular log level, messages will get logged for that level and all levels above it, and not for any log levels below that. As an example, if your log level is set to ERROR, you will log messages that are errors and fatals. If your log level is set to INFO, you will log messages that are infos, warns, errors, and fatals. Typically, when you develop on your local machine, it's good to set the log level to DEBUG, and when you deploy a web application, you should set the log level to INFO or higher so that you don't fill up your error logs with debug messages.
Log4j log levels
FATAL - Designates very severe error events that will presumably lead the application to abort.
ERROR - Designates error events that might still allow the application to continue running.
WARN - Designates potentially harmful situations.
INFO - Designates informational messages that highlight the progress of the application at coarse-grained level.
DEBUG - Detailed information on the flow through the system. Expect these to be written to logs only. Generally speaking, most lines logged by your application should be written as DEBUG.
TRACE - Most detailed information. Expect these to be written to logs only.
Log4j logging service configuration
Logger(Category) : 로깅 메세지를 Appender에 전달한다. log4j의 심장부에 위치 하며, 개발자가 직접 로그출력 여부를 런타임에 조정되도록 해준다. logger는 로그레벨을 가지고 있으며, 로그의 출력 여부는 로그문의 레벨과 로거의 레벨을 가지고 결정된다.
Appender : 전달된 로깅 메세지를 파일에다 기록할 것인지, 콘솔에 출력할 것인지 아니면 DB에 저장할 것인지 매개체 역활을 담당한다. 즉, 로그의 출력위치를 결정한다.
Layout : Appender가 어디에 출력할 것인지 결정했다면 어떤 형식으로 출력할 것이지 출력 layout을 결정한다.
Log4j TTCC(TTCC is a message format used by log4j. TTCC is an acronym for Time Thread Category Component.)
%m : 사용자가 지정한 메시지를 출력한다.
%p : 로깅 이벤트의 priority를 출력한다.
%d : 로깅 이벤트가 일어난 날짜 출력한다. %d{HH:mm:ss} 또는 %d{dd MMMM yyyy HH:mm:ss} 만약 날짜 형식 지정자가 주어져 있지 않다면 ISO8601 형식으로 나타난다. 더 빠른 성능을 위해 %d{ISO8601}, %d{ABSOLUTE} 사용
%c : 로깅 요청을 일으킨 호출자의 클래스명을 출력한다. 예를 들어 org.apache.xyz.SomeClass 처럼 되어 있다면 %C{2}는 xyz.SomeClass 가 출력된다.
%t : 로그 이벤트가 발생한 쓰레드의 이름을 출력한다.
%m : 로깅이 발생한 method 이름을 출력한다.
%n : 플랫폼 종속적인 개행문자가 출력(\r\n 또는 \n)-%% = %표시를 출력한다.
%F : 로깅이 발생한 프로그램 파일명을 출력한다.
%l : 소스코드의 위치 정보를 출력한다.
%L : 로깅이 발생한 행번호를 출력한다.
Log4j supports various log methods(console, file, DB, etc.)
org.apache.log4j.ConsoleAppender : Console 화면으로 출력하기 위한 Appender.
org.apache.log4j.DailyRollingFileAppender : 설정한 날짜 또는 조건에 맞춰 로깅을 수행하기 위한 Appender. DailyRollingFileAppender 파일에서 사용할 수 있는 몇가지 날짜 포맷은 다음과 같다. 하위에 File, DatePattern과 같은 parameter를 정의할 수 있다.
org.apache.log4j.jdbc.JDBCAppender : DB에 로그를 출력하기 위한 Appender로 하위에 Driver, URL, User, Password, Sql과 같은 parameter를 정의할 수 있다. 다음은 log4j.xml 파일 내의 JDBCAppender에 대한 속성 정의 내용이다.
Log4j logger
로깅 이벤트 발생시 같은 이름으로 선언된 logger를 찾아 해당 logger에게 로그 메시지를 보내고 additivity가 true일 경우, 상위 logger에게도 로그 메시지를 보낸다.
특정 패키지 출력 설정
Log4j root
해당 logger가 존재하지 않거나 상위 logger가 존재하지 않을 경우 모든 로그는 root logger의 정책에 따라 출력된다.