-
log4j2란?
개발자들은 로그에 많이 의존합니다. 버그의 원인을 찾을 때 사용하기도 하며 사용자를 추적할 수도 있죠.
요즘과 같은 빅데이터 시대에는 로그 데이터는 중요한 역할을 합니다.
log4j는 아파치에서 만든 로그 작성을 위한 라이브러리 중 하나입니다.
로깅 라이브러리로는 log4j와 logback등이 존재합니다. 이 라이브러리에서 고도화된 것이 log4j입니다.
설정 설명
log4j의 설정 방법은 yml, json 등 다양한 방법이 있지만 본인은 xml 설정이 더 눈에 익숙하기에 xml 설정으로 하겠습니다.
파일 위치는 logging.config를 통해 제어가능합니다. defualt는 resources 밑입니다.
<?xml version="1.0" encoding="UTF-8"?> <Configuration status="warn" name="MyApp" packages=""> <Properties> <Property name="baseDir">logs</Property> </Properties> <Appenders> <RollingFile name="RollingFile" fileName="${baseDir}/app.log" filePattern="${baseDir}/$${date:yyyy-MM}/app-%d{yyyy-MM-dd}.log.gz"> <PatternLayout pattern="%d %p %c{1.} [%t] %m%n" /> <CronTriggeringPolicy schedule="0 0 0 * * ?"/> <DefaultRolloverStrategy> <Delete basePath="${baseDir}" maxDepth="2"> <IfFileName glob="*/app-*.log.gz" /> <IfLastModified age="P60D" /> </Delete> </DefaultRolloverStrategy> </RollingFile> </Appenders> <Loggers> <Root level="error"> <AppenderRef ref="RollingFile"/> </Root> </Loggers> </Configuration>
properties
파일 내부에서 상수를 선언할 수 있습니다.
appenders
로그가 저장,출력될 위치를 나타냅니다.
Appenders태그명 출력 위치ConsoleAppneder <Console> 콘솔에 출력 FileAppneder,rollingrandomaccessfile (성능up) <File> 파일에 출력 RollingFileAppneder <RollingFile> 조건에 따라 파일에 출력 JDBCAppender <JDBC> RDB Table에 출력
PatternLayout
로그의 형식을 지정합니다.
policy
file rolling up의 기준입니다.
- OnStartupTriggeringPolicy : jvm start시 trigger
- TimeBasedTriggeringPolicy : time에 따른 trigger
- SizeBasedTriggeringPolicy : file size에 따른 trigger
- CronTriggeringPolicy : Cron Expression(시간에 관한 표현)에 따른 trigger
file 패턴과 policy의 조합으로 rolling 됩니다.
ex)% d {yyyy-MM-dd-hh-mm} .log.zip 매분마다 로그 파일 롤링 interval = 2이면 2 분마다 롤오버가 발생합니다.
예 : 2017-07-26-09-57.log.zip , 2017-07-26-09-59.log.zip , 2017-07-26-10-01.log.zip , 2017-07-26- 10-03.log.zip 등 ..% d {yyyy-MM-dd-hh} .log.zip 로그 파일을 매 시간 롤업하십시오. interval = 4이면 4 시간마다 롤오버가 발생합니다.
예 : 2017-07-26-09.log.zip , 2017-07-26-10.log.zip , 2017-07-26-11.log.zip 등% d {yyyy-MM-dd} .log.zip 매일 로그 파일 롤링 interval = 1이면 롤오버가 매일 발생합니다.
예 : 2017-07-26.log.zip , 2017-07-27.log.zip 등DefaultRolloverStrategy
트리거는 수행해야하는지를 정한다면 RolloverStrategy는 롤링 업의 방법을 정의합니다.
정의하지 않으면 DefaultRolloverStrategy가 사용됩니다.
설정으로는 로그 삭제를 지원합니다.
base 패턴에서 최대 2 depth 밑에 있는 파일까지 판단하며 이름이 패턴과 같고 수정일이 패턴에 맞으면 삭제를 합니다.
logger
로깅을 직접 수행하는 요소입니다.
root 로거는 필수적이며 추가적으로 logger를 패키지 별로 등록할 수 있습니다.
additivity속성은 상위 appender를 상속받는 것을 제어할 수 있습니다.
level을 통해 해당 레벨 이상을 로깅할 수 있습니다.
'글또' 카테고리의 다른 글
레거시 코드를 대하는 법 (0) 2022.09.03 동시성 해결에 관해 (0) 2022.08.21 토스 slash 22 를 보고 (0) 2022.07.25 jvm 시간대 (0) 2022.07.18 디자인 패턴 적용기 (0) 2022.06.26