본문 바로가기

Java/Spring Boot

[Spring Boot] 로깅 - 커스터마이징

1. 커스터마이징한 로그 설정 파일 사용하기

  • Logback : logback-spring.xml
  • Log4J2 : log4j2-spring.xml
  • JUL : logging-properties (추천x)
  • Logback extensions
    - Profile-specific Configuartion (프로파일 별 로그레벨 지정) : <springProfile name="프로파일"> 
    - Environment Properties 들도 사용 가능 : <springProperty>

logback 설정 파일 안에서 Profile이나 Environment variable을 사용할 수 있으려면
logback.xml이 아닌 logback-spring.xml을 사용해야 함 
(logback.xml은 너무 일찍 로딩이 되기 때문에 사용 불가)

 

 

# logback-spring.xml

<?xml version="1.0" encoding="UTF-8"?>
<configuration>
    <include resource="org/springframework/boot/logging/logback/defaults.xml"/>
    <include resource="org/springframework/boot/logging/logback/console-appender.xml" />
    <root level="INFO">
        <appender-ref ref="CONSOLE" />
    </root>
    <logger name="kvp.hyelin.springboot" level="DEBUG"/>
</configuration>

 

 

# <springProfile>

 

<springProfile name="staging">
    <!-- configuration to be enabled when the "staging" profile is active -->
</springProfile>

<springProfile name="dev | staging">
    <!-- configuration to be enabled when the "dev" or "staging" profiles are active -->
</springProfile>

<springProfile name="!production">
    <!-- configuration to be enabled when the "production" profile is not active -->
</springProfile>

 

 

 

 

 

 

 

 

2. 로거를 Log4j2로 변경하기

 

spring-boot-starter 내에 logging 제외시키고

<dependency>
   <groupId>org.springframework.boot</groupId>
   <artifactId>spring-boot-starter</artifactId>
   <exclusions>
      <exclusion>
         <groupId>org.springframework.boot</groupId>
         <artifactId>spring-boot-starter-logging</artifactId>
      </exclusion>
   </exclusions>
</dependency>

 

log4j2 의존성 추가

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-log4j2</artifactId>
</dependency>
 

 

 

 

 

 

 

 

 

 

 

 

https://www.inflearn.com/course/%EC%8A%A4%ED%94%84%EB%A7%81%EB%B6%80%ED%8A%B8/dashboard

 

스프링 부트 개념과 활용 - 인프런 | 강의

스프링 부트의 원리 및 여러 기능을 코딩을 통해 쉽게 이해하고 보다 적극적으로 사용할 수 있는 방법을 학습합니다., - 강의 소개 | 인프런...

www.inflearn.com

https://docs.spring.io/spring-boot/docs/current/reference/html/howto.html#howto.logging

 

“How-to” Guides

Spring Boot has no mandatory logging dependency, except for the Commons Logging API, which is typically provided by Spring Framework’s spring-jcl module. To use Logback, you need to include it and spring-jcl on the classpath. The recommended way to do th

docs.spring.io

https://docs.spring.io/spring-boot/docs/current/reference/html/features.html#features.profiles.profile-specific-configuration-files

 

Core Features

Spring Boot lets you externalize your configuration so that you can work with the same application code in different environments. You can use a variety of external configuration sources, include Java properties files, YAML files, environment variables, an

docs.spring.io

 

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

[Spring Boot] Spring-Boot-Devtools  (0) 2022.03.25
[Spring Boot] Test  (0) 2022.03.23
[Spring Boot] 로깅 - Spring Boot 기본 로거 설정  (0) 2022.03.22
[Spring Boot] 프로파일  (0) 2022.03.13
[Spring Boot] 외부 설정  (0) 2022.03.13