기본 콘텐츠로 건너뛰기

스프링부트, logging

Logging

  • 스프링부트는 Java Util Logging, Log4j2, Logback을 기본적으로 제공한다.
  • 내부적으로 apache common logging 을 사용하나 변경이 가능하다
  • 만약 ‘Starters’를 사용한다면 Logback이 사용된다.

Log Format

시간/로그레벨/process id/ 구분자(—)/ thread 명/ logger 명 / 메세지

2014-03-05 10:57:51.112  INFO 45469 --- [ main] org.apache.catalina.core.StandardEngine  : Starting Servlet Engine: Apache Tomcat/7.0.52
2014-03-05 10:57:51.253  INFO 45469 --- [ost-startStop-1] o.a.c.c.C.[Tomcat].[localhost].[/] : Initializing Spring embedded WebApplicationContext
2014-03-05 10:57:51.253  INFO 45469 --- [ost-startStop-1] o.s.web.context.ContextLoader  : Root WebApplicationContext: initialization completed in 1358 ms

Color-coded Output

로그에 색을 입힐 수도 있다. spring.output.ansi.enabled 값을 설정해주면 된다.

Enum Constants

Enum Constant Description
ALWAYS Enable ANSI-colored output.
DETECT Try to detect whether ANSI coloring capabilities are available.
NEVER Disable ANSI-colored output.

File Output

기본적으로 스프링 부트는 파일에는 로그를 남기지 않고 콘솔에만 표시한다. 만약 파일로 남기고 싶다면 logging.file or logging.path 의 프로퍼티값을 설정해야한다.

Logging properties

logging.file logging.path Example Description
(none) (none) 콘솔만 표시
Specific file (none) my.log 현재디렉토리에 my.log라는 이름으로 생성
(none) Specific directory /var/log 명기된 폴더의 spring.log라는 이름으로 생성
  • 기본적으로 로그 파일사이즈가 10mb가 되면 rotate를 실행한다. logging.file.max-size 값을 통해 수정할 수 있다.

Profile-specific Configuration

springProfile 태그를 사용하면 logback 설정파일에서 복수개의 프로파일을 설정할 수 있다.
그리고 Environment 내의 프로퍼티들을 개별적으로 설정할 수도 있다.

<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>

<springProperty scope="context" name="fluentHost" source="myapp.fluentd.host"
  defaultValue="localhost"/>
<appender name="FLUENT" class="ch.qos.logback.more.appenders.DataFluentAppender">
 <remoteHost>${fluentHost}</remoteHost>
 ...
</appender>

댓글