스프링로그설정 (1)


스프링 프로젝트에서 logback을 이용하여 환경별로 로그 설정을 다르게 할 때 

<if>, <elseif> 등의 태그를 이용할 수 있는 줄 알고 아래처럼 설정을 해보았다.

<if condition="property('myproject.profile').equals('live')">
<logger name="com.myproject" level="debug">
<appender-ref ref="STDOUT"/>
</logger>
<logger name="org.springframework" level="WARN"/>
<root level="info"/>
</if>
<elseif condition="property('myproject.profile').equals('dev')">
<logger name="com.myproject" level="debug">
<appender-ref ref="STDOUT"/>
</logger>
<logger name="org.springframework" level="WARN"/>
<root level="info"/>
</elseif>
<else condition="property('myproject.profile').equals('local')">
<root level="debug">
<appender-ref ref="STDOUT" />
</root>
</else>

위 처럼 설정했는데 앱 기동 시 아래처럼 logback에서 에러로그를 찍었다.


14:46:24,662 |-INFO in ch.qos.logback.core.joran.action.AppenderAction - Naming appender as [STDOUT]

14:46:24,668 |-WARN in ch.qos.logback.core.ConsoleAppender[STDOUT] - This appender no longer admits a layout as a sub-component, set an encoder instead.

14:46:24,668 |-WARN in ch.qos.logback.core.ConsoleAppender[STDOUT] - To ensure compatibility, wrapping your layout in LayoutWrappingEncoder.

14:46:24,668 |-WARN in ch.qos.logback.core.ConsoleAppender[STDOUT] - See also http://logback.qos.ch/codes.html#layoutInsteadOfEncoder for details

14:46:24,673 |-ERROR in ch.qos.logback.core.joran.conditional.IfAction - Could not find Janino library on the class path. Skipping conditional processing.

14:46:24,673 |-ERROR in ch.qos.logback.core.joran.conditional.IfAction - See also http://logback.qos.ch/codes.html#ifJanino

14:46:24,673 |-ERROR in ch.qos.logback.core.joran.spi.Interpreter@31:64 - no applicable action for [logger], current ElementPath  is [[configuration][if][logger]]

14:46:24,674 |-ERROR in ch.qos.logback.core.joran.spi.Interpreter@32:42 - no applicable action for [appender-ref], current ElementPath  is [[configuration][if][logger][appender-ref]]


원인은 명확해보였다. Janino라는 라이브러리가 없어서였다.

<if>, <elseif> 등의 태그문법을 Janino 라이브러리에서 해석을 해주는데 해당 라이브러리가 없어서 logback 설정파일해석을 제대로 못한 상태였다.

그래서 현재일자 가장 최신버전으로 의존성을 추가했다.

<dependency>
<groupId>org.codehaus.janino</groupId>
<artifactId>janino</artifactId>
<version>3.0.14</version>
</dependency>


그런데 또 오류가 났다 ㅠㅠ

 Failed to parse condition [property('myproject.profile').equals('live')] org.codehaus.commons.compiler.CompileException: Line 1, Column 45: Closing single quote missing

at org.codehaus.commons.compiler.CompileException: Line 1, Column 45: Closing single quote missing


조건으로 준게 파싱오류가 났단다. 그래서 컨디션을 아래처럼 바꾸었다.

<if condition='property("myproject.profile").equals("local")'>

쌍따옴표와 홑따옴표를 바꾼거다.

그리고 if-else if - else 를 하기위해선 nested if를 써야했다.

하지만 nested <if> 를 쓰기엔 안쪽으로 인덴트 되는 게 많아져서 가독성이 떨어진다.

어차피 조건을 equals로 비교하고 있으니 

결과적으로 if문만 써서 profile값을 비교해주어도 if-elseif의 효과는 볼 수 있으므로 아래처럼 바꿨다.

<if condition='property("myproject.profile").equals("live")'>
<then>
<logger name="com.myproject" level="debug">
<appender-ref ref="ELASTIC"/>
</logger>
<logger name="org.springframework" level="WARN"/>
<root level="info"/>
</then>
</if>
<if condition='property("myproject.profile").equals("dev")'>
<then>
<logger name="com.myproject" level="debug">
<appender-ref ref="ELASTIC"/>
</logger>
<logger name="org.springframework" level="WARN"/>
<root level="info"/>
</then>
</if>
<if condition='property("myproject.profile").equals("local")'>
<then>
<logger name="com.myproject" level="debug">
<appender-ref ref="STDOUT"/>
</logger>
<root level="info">
<appender-ref ref="STDOUT"/>
</root>
</then>
</if>

이제 잘 동작한다.