Log4j2란?
Apache Software Foundation에서 개발한 로그 라이브러리로, 애플리케이션의 동작을 기록하고 디버깅을 도와줌
스프링부트 3.3.0
자바 17
// log4j2
implementation('org.springframework.boot:spring-boot-starter-log4j2') {
exclude group: 'org.springframework.boot', module: 'spring-boot-starter-logging'
}
// log4j2 사용을 위해 Logback 제거
configurations {
all*.exclude module: 'spring-boot-starter-logging'
}
#log4j 설정 / 로깅 설정
logging.config=classpath:log4j2.xml
<?xml version="1.0" encoding="UTF-8"?>
<Configuration status="INFO">
<Properties>
<Property name="LOG_PATH">logs</Property>
<Property name="DEFAULT_PATTERN">[%d{yyyy-MM-dd HH:mm:ss.SSS}] %5p ${sys:PID} --- [%15.15t] %-40.40c{1.} : %m%n%xwEx</Property>
<Property name="COLOR_PATTERN">[%clr{%d{yyyy-MM-dd HH:mm:ss.SSS}}{faint}] %clr{%5p} %clr{${sys:PID}}{magenta} %clr{---}{faint} %clr{[%15.15t]}{faint} %clr{%-40.40c}{cyan} %clr{:}{faint} %m%n%xwEx</Property>
<Property name="TEST_PATTERN">%d %5p [%c] %m%n</Property>
<!-- <Property name="COLOR_PATTERN">[%clr{%d{yyyy-MM-dd HH:mm:ss.SSS}}{faint}] %clr{%5p} %clr{${sys:PID}}{magenta} %clr{---}{faint} %clr{[%15.15t]}{faint} %clr{%-40.40c{1.}}{cyan} %clr{:}{faint} %m%n%xwEx</Property>-->
</Properties>
<Appenders>
<Console name="ConsoleAppender" target="SYSTEM_OUT" follow="true">
<PatternLayout pattern="${COLOR_PATTERN}"/>
</Console>
<RollingFile name ="RollingFileAppender">
<FileName>${LOG_PATH}/log4j2.log</FileName>
<FilePattern>${LOG_PATH}/log4j2.%d{yyyy-MM-dd-hh-mm}.%i.log.gz</FilePattern>
<PatternLayout>
<Pattern>${DEFAULT_PATTERN}</Pattern>
</PatternLayout>
<Policies>
<SizeBasedTriggeringPolicy size="100MB"/>
<TimeBasedTriggeringPolicy interval = "1" modulate = "true"/>
</Policies>
<DefaultRolloverStrategy>
<Delete basePath="${LOG_PATH}" maxDepth="1">
<IfAccumulatedFileCount exceeds="3"/>
</Delete>
</DefaultRolloverStrategy>
</RollingFile>
</Appenders>
<Loggers>
<!-- Root : 일반적인 로그 정책, 반드시 하나 정의해야함 -->
<Root level="INFO" additivity="false">
<AppenderRef ref="ConsoleAppender"/>
<AppenderRef ref="RollingFileAppender"/>
</Root>
<!-- springframework logger -->
<Logger name="org.springframework" level="INFO" additivity="false">
<AppenderRef ref="ConsoleAppender" />
<AppenderRef ref="RollingFileAppender"/>
</Logger>
<!-- jdbc logger -->
<Logger name="jdbc.sqlonly" level="OFF" additivity="false">
<AppenderRef ref="ConsoleAppender"/>
<AppenderRef ref="RollingFileAppender"/>
</Logger>
<Logger name="jdbc.sqltiming" level="INFO" additivity="false">
<AppenderRef ref="ConsoleAppender"/>
<AppenderRef ref="RollingFileAppender"/>
</Logger>
<Logger name="jdbc.resultsettable" level="INFO" additivity="false">
<AppenderRef ref="ConsoleAppender"/>
<AppenderRef ref="RollingFileAppender"/>
</Logger>
<Logger name="jdbc.audit" level="OFF" additivity="false">
<AppenderRef ref="ConsoleAppender"/>
<AppenderRef ref="RollingFileAppender"/>
</Logger>
<Logger name="jdbc.resultset" level="OFF" additivity="false">
<AppenderRef ref="ConsoleAppender"/>
<AppenderRef ref="RollingFileAppender"/>
</Logger>
<Logger name="jdbc.connection" level="OFF" additivity="false">
<AppenderRef ref="ConsoleAppender"/>
<AppenderRef ref="RollingFileAppender"/>
</Logger>
</Loggers>
</Configuration>
Properties
Appenders
ConsoleAppender
RollingFileAppender
Loggers
org.springframework Logger
jdbc.sqlonly, jdbc.sqltiming, jdbc.resultsettable, jdbc.audit, jdbc.resultset, jdbc.connection
등
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import lombok.extern.slf4j.Slf4j;
@Slf4j
@SpringBootApplication
public class BackApplication {
public static void main(String[] args) {
SpringApplication.run(BackApplication.class, args);
log.info("Info level log message / 정보 메세지 예시");
log.error("Error level log message / 오류 메세지 예시");
}
}