로깅
- 콘솔로그 찍는 대신에 로깅
spring
--- log4j
를 연결해주는 slf4j
pom.xml
<properties>
<java-version>11</java-version>
<org.springframework-version>4.3.25.RELEASE</org.springframework-version>
<org.aspectj-version>1.6.10</org.aspectj-version>
<org.slf4j-version>1.7.30</org.slf4j-version>
</properties>
...
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.17</version>
</dependency>
log4j.xml
<!DOCTYPE log4j:configuration SYSTEM "http://logging.apache.org/log4j/1.2/apidocs/org/apache/log4j/xml/doc-files/log4j.dtd">
- level을
trace
까지 낮춰야 5개가 다 나온다!!
<logger name="com.kh.app21">
<level value="trace" />
</logger>
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE log4j:configuration SYSTEM "http://logging.apache.org/log4j/1.2/apidocs/org/apache/log4j/xml/doc-files/log4j.dtd">
<log4j:configuration xmlns:log4j="http://jakarta.apache.org/log4j/">
<appender name="console" class="org.apache.log4j.ConsoleAppender">
<param name="Target" value="System.out" />
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="%d{yyyy-MM-dd HH:mm:ss} %-5p: %c - %m %n" />
</layout>
</appender>
<logger name="com.kh.app21">
<level value="debug" />
</logger>
<root>
<priority value="warn" />
<appender-ref ref="console" />
</root>
</log4j:configuration>
출력패턴
TestClass
import org.junit.Test;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public class TestClass {
@Test
public void Test() {
Logger logger = LoggerFactory.getLogger(TestClass.class);
System.out.println("test~~~");
logger.trace("trace");
logger.debug("debug");
logger.info("logger info~~~~");
logger.warn("warn");
logger.error("error");
}
}
어팬더 appender
- ConsoleAppender
- FileAppender
- DailyRollingFileAppender
- RollingFileAppender
FileAppender
log4j.xml
<appender name="faxml" class="org.apache.log4j.FileAppender">
<param name="file" value="myLogFile.xml" />
<param name="append" value="true" />
<layout class="org.apache.log4j.xml.XMLLayout"/>
</appender>
<appender name="fatxt" class="org.apache.log4j.FileAppender">
<param name="file" value="myLogFile.txt" />
<param name="append" value="true" />
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="%d{yyyy-MM-dd HH:mm:ss} %-5p: %c - %m %n" />
</layout>
</appender>
...
<root>
<priority value="warn" />
<appender-ref ref="fatxt" />
</root>
DailyRollingFileAppender
log4j.txt
<appender name="fatxtdaily" class="org.apache.log4j.DailyRollingFileAppender">
<param name="file" value="D:/logForSpring/dailyLog" />
<param name="append" value="true" />
<param name="DatePattern" value="yyyy-MM-dd" />
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="%d{yyyy-MM-dd HH:mm:ss} %-5p: %c - %m %n" />
</layout>
</appender>
<logger name="com.kh.app21">
<level value="debug" />
</logger>
<root>
<priority value="warn" />
<appender-ref ref="fatxtdaily" />
<appender-ref ref="console" />
</root>
✨컴퓨터 날짜를 바꾸면???
롬복으로!
package com.kh.app21;
import org.junit.Test;
import lombok.extern.slf4j.Slf4j;
@Slf4j
public class TestClass {
@Test
public void Test() {
System.out.println("test~~~");
log.trace("trace");
log.debug("debug");
log.info("logger info~~~~");
log.warn("warn");
log.error("error");
}
}