우선 SpringBoot 기본으로 설정되어있는 slf4j log구현 클래스인 logback 라이브러리를 제외시켜준다.
log4j2 라이브러리를 추가해준다.
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<exclusions>
<exclusion>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-logging</artifactId>
</exclusion>
</exclusions>
</dependency>
변경전
변경후
spring-boot-starter-logging
사라진거 확인
*변경후 로그 안찍힘
<!--2.1 logging -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-log4j2</artifactId>
</dependency>
*로그 다시 나옴
application.yml
# logging 설정
logging:
config: classpath:log4j2/log4j2.xml
log4j2/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>
*패턴주기
{yyyy-MM-dd-hh-mm} : 분마다
{yyyy-MM-dd-hh} : 시마다
{yyyy-MM-dd} : 일마다
*FileName, FilePattern 파일경로 지정시 '/'여부에 따라 경로 차이
'/' 가 있으면, 절대경로 /logs/log4j2.log >>> C:\logs/
'/' 가 없으면, 상대경로 logs/log4j2.log >>> C:\workspace/base/logs/
- name="package"에 해당하는 로그 설정
- Root에 일반적인 로그를 찍고, Logger들은 등록된 설정대로 출력
- AppenderRef에 Logger가 등록된다고 이해하기