스프링16_로깅

charl hi·2022년 2월 3일
0

Spring

목록 보기
17/25

로깅

  • 콘솔로그 찍는 대신에 로깅
  • 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개가 다 나온다!!
	<!-- Application Loggers -->
	<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/">

	<!-- Appenders -->
	<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>
	
	<!-- Application Loggers -->
	<logger name="com.kh.app21">
		<level value="debug" />
	</logger>
		
  	<!-- Root 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

	<!-- FileAppender 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>
	
	<!-- FileAppender txt -->
	<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 Logger -->
	<root>
		<priority value="warn" />
		<appender-ref ref="fatxt" />
	</root>




DailyRollingFileAppender

log4j.txt

	<!-- DailyRollingFileAppender 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>
	
	<!-- Application Loggers -->
	<logger name="com.kh.app21">
		<level value="debug" />
	</logger>
	
	<!-- Root 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() {
		
		//직접 만들기
//		Logger logger = LoggerFactory.getLogger(TestClass.class);
		
		//롬복 -> @Slf4j
		
		System.out.println("test~~~");
		
		log.trace("trace");
		log.debug("debug");
		log.info("logger info~~~~");
		log.warn("warn");
		log.error("error");
		
	}
}



0개의 댓글