JAVA
로그 레벨 (log4j)
by yororing
2024. 5. 7.
01 log4j란
1. 정의
- Java logging framework (composed of an API, its implementations, and components to assist the deployment for various use cases)
- Apache Log4j는 자바 기반의 로깅 유틸리티로서 Apache Software Foundation의 한 프로젝트인 Apache Logging Services의 일부분이다 (Ceki Gülcü가 작성)
2. 로그 레벨
- 단계: ALL < TRACE < DEBUG < INFO < WARN < ERROR < FATAL < OFF
- Logger의 setLevel에서 지정된 로그 레벨 미만의 로깅 이벤트 무시됨
- 아래 레벨 외에 커스텀 로그 레벨 (custom log level) 정의 가능
- A source code generator tool is provided to create Loggers that support custom log levels identically to the built-in log levels.
- Custom log levels can either complement or replace the built-in log levels.
로그 레벨 | 설명 |
ALL | 커스텀 레벨을 포함한 모든 레벨 |
TRACE | log4j1.2.12에서 신규 추가된 레벨, DEBUG보다 더 상세한 정보를 담음 |
DEBUG | 시스템의 flow에 대한 상세한 정보를 담음. 로그에만 작성됨. designates fine-grained informational events that are most useful to debug an application |
INFO | designates informational messages that highlight the progress of the application at coarse-grained level 로그인, 상태 변경과 같은 정보성 메시지를 담음 |
WARN | designates potentially harmful situations |
ERROR | designates error events that might still allow the application to continue running |
FATAL | designates very severe error events that will presumably lead the application to abort |
OFF | 로깅 끔 |
3. 로그 레벨 설정
- config 파일을 통해 설정 가능
- 가능한 파일 종류: XML, JSON, YAML, or properties file
- 예) an example configuration file that performs the task using the log.setLevel(Level.WARN) method
# Define the root logger with appender file
log = /usr/home/log4j
log4j.rootLogger = WARN, FILE
# Define the file appender
log4j.appender.FILE=org.apache.log4j.FileAppender
log4j.appender.FILE.File=${log}/log.out
# Define the layout for file appender
log4j.appender.FILE.layout=org.apache.log4j.PatternLayout
log4j.appender.FILE.layout.conversionPattern=%m%n
4. 로그 메세지 형식 (TTCC)
1) TTCC란
- 'Time Thread Category Component'의 약자
- log4j의 메세지 형식
- 사용하는 패턴:
%r [%t] %-5p %c %x - %m%n
Mnemonic | |
%r | used to output the number of milliseconds elapsed from the construction of the layout until the creation of the logging event |
%t | used to output the name of the thread that generated the logging event |
%p | used to output the priority of the logging event |
%c | used to output the category of the logging event |
%x | used to output the NDC (nested diagnostic context) associated with the thread that generated the logging event |
%X{key} | used to output the MDC (mapped diagnostic context) associated with the thread that generated the logging event for specified key |
%m | used to output the application supplied message associated with the logging event |
%n | used to output the plat-form specific newline character(s) |
5. 로그 사용하기
1) java 파일 작성
import org.apache.log4j.*;
public class LogClass {
private static org.apache.log4j.Logger log = Logger.getLogger(LogClass.class);
public static void main(String[] args) {
log.trace("Trace Message!");
log.debug("Debug Message!");
log.info("Info Message!");
log.warn("Warn Message!");
log.error("Error Message!");
log.fatal("Fatal Message!");
}
}
2) 컴파일 후 프로그램 실행
- /usr/home/log4j/log.out file
# 실행 결과
Warn Message!
Error Message!
Fatal Message!
참조
- https://en.wikipedia.org/wiki/Log4j
- https://logging.apache.org/log4j/2.x/
- https://m.blog.naver.com/PostView.naver?isHttpsRedirect=true&blogId=2zino&logNo=221641662104
- https://www.tutorialspoint.com/log4j/log4j_logging_levels.htm
- https://xxvigrufv.tistory.com/2
- https://www.tutorialspoint.com/log4j/log4j_logging_levels.htm
-