본문 바로가기
개발/Spring Boot

4. log4j2 설정

by 카리3 2021. 2. 7.

1. pom.xml 설정

		<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>
		 
		<!-- log -->
		<dependency>
		    <groupId>org.springframework.boot</groupId>
		    <artifactId>spring-boot-starter-log4j2</artifactId>
		</dependency>

2. /resoruce/log4j2.xml

<?xml version="1.0" encoding="UTF-8"?>
<Configuration status="info" monitorInterval="30">
    <Properties>
        <Property name="LOG_FORMAT">%d{yyyy-MM-dd HH:mm:ss} %p %m%n</Property>
    </Properties>

    <Appenders>
        <Console name="console" target="SYSTEM_OUT" follow="true">
            <PatternLayout pattern="${LOG_FORMAT}"/>
        </Console>
        <File name="file" fileName="./logs/file/test.log">
            <PatternLayout pattern="%d %5p [%c] %m%n" />
        </File>
    </Appenders>
    
    

    <Loggers>
        <Root level="info">
            <AppenderRef ref="console"/>
            <AppenderRef ref="file" />
        </Root>
        <logger name="com.kangong" level="trace" additivity="false" >
            <AppenderRef ref="console" />
        </logger>
        <!-- 스프링 프레임워크에서 찍는건 level을 info로 설정 -->
        <logger name="org.springframework" level="info" additivity="false" >
            <AppenderRef ref="console" />
           <!--   <AppenderRef ref="file" /> -->
        </logger>
    </Loggers>
</Configuration>

3. Log 출력

package com.kangong.sample.controller;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import com.kangong.sample.service.SampleService;

@RestController
public class SampleRestController {

	// (1) Logger Name이 "com.kangong.sample.controller.com.kangong.sample.controller.SampleRestController"인 Logger 설정을 따르는 Logger 객체 생성
	private Logger logger = LogManager.getLogger();	
	// (2) 위와 동일 
	Logger logger2 = LogManager.getLogger(SampleRestController.class);  
	// (3) Logger Name이 "ktw"인 Logger설정을 따르는 Logger 객체 생성
	Logger logger3 = LogManager.getLogger("ktw");  
	
	/**
	 * <Loggers>
	  <!-- attribute: name(Logger명), level(Log Level), additivity(중복로깅여부, true or false) -->
	  <!-- element: AppenderRef(Appender명) -->
	  <Logger name="ktw.lsh" level="INFO" additivity="false">
	   <AppenderRef ref="console"/>  
	  </Logger>
	  <Logger name="ktw" level="DEBUG" additivity="false">
	   <AppenderRef ref="console"/>  
	  </Logger>
	  <Rootlevel="ERROR">
	   <AppenderRef ref="console"/>
	  </Root>
	 </Loggers>
	 * 
	 */
	
	@Autowired
	SampleService sampleService;
	
	@GetMapping("/hello")
	public String[] hello() {		
		
		logger.info("hello2==============="+sampleService.gettNow());
		return new String[] {"Hello","World",sampleService.gettNow()};
	}

}

1) 호출한 Logger Name과 동일한 Logger가 있는 경우, 해당 Logger 설정을 따른다.
2) 동일한 Logger는 없지만, Parent Logger가 존재하는 경우, Parent Logger 설정을 따른다.
3) Parent Logger도 존재하지 않는 경우, Root Logger 설정을 따른다.

'개발 > Spring Boot' 카테고리의 다른 글

6.thymeleaf 설정  (0) 2021.02.11
5. Lombok 설정  (0) 2021.02.11
3. jsp 연동  (0) 2021.02.07
2. MySql 연결  (0) 2021.02.06
1.Spring Boot 환경 셋팅  (0) 2021.02.06