4
votes

Here is my sample logback-spring.xml

<?xml version="1.0" encoding="UTF-8"?>
<configuration>
    <property name="LOG_FILE" value="some file path here"/>
    <property name="LOG_FILE_MAX_SIZE" value="50MB" />
    <property name="LOG_FILE_MAX_HISTORY" value="30" />
    <include resource="org/springframework/boot/logging/logback/defaults.xml" />
    <include resource="org/springframework/boot/logging/logback/file-appender.xml" />
    <include resource="org/springframework/boot/logging/logback/console-appender.xml" />

<springProfile name="!test &amp; !prod">
    <logger name="com.myapp" level="DEBUG" />
    <root level="INFO">
        <appender-ref ref="CONSOLE"/>
    </root> 
</springProfile>

<springProfile name="test">
    <logger name="com.myapp" level="DEBUG" />
    <root level="WARN">
        <appender-ref ref="FILE" />
    </root>
</springProfile>

<springProfile name="prod">
    <logger name="com.myapp" level="INFO" />
    <root level="WARN">
        <appender-ref ref="FILE"/>
    </root>
</springProfile>

My intent is to log contents only to FILE for test & prod profile, however for any other profile(i.e., localhost), I would want my logs to get only in CONSOLE.

With the above setting, if i start my spring boot app(version 2.1.1.RELEASE) with localhost profile, Its getting logged only in CONSOLE as expected, however if i use test or prod profile, it logs the content both in CONSOLE as well as FILE.

Do you see any problem in the logback xml ?

3

3 Answers

3
votes

As far as I know Spring does not allow logical expressions in the profile selection. One can have just say !test but not a combination of such. So in your case the condition will trigger if test is not active or prod is not active which means for test active or prod active.

What you can do is to have an enumeration of other profiles for which you actually want to log to console. E. g. localhost.

0
votes

Here is an example you can follow where you can choose for appender type for various environment like only to FILE for test & prod profile and only stdout or console for local environment.

       <springProfile name="dev,test,local">
    <property name="LOG_PATH" value="C:/<folder_name>/" />
</springProfile>
  <!--if production environment is linux -->
   <springProfile name="prod">
    <property name="LOG_PATH" value="/opt/<folder_name>/" />
   </springProfile>

    <!-- make sure the pattern tag expression don't break in the middle-->
   <appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
     <encoder class="ch.qos.logback.classic.encoder.PatternLayoutEncoder">
        <pattern>%clr(%d{yyyy-MM-dd HH:mm:ss.SSS}){faint} %clr(%5p) %clr(${PID:- }) 
   {magenta} %clr(---){faint} %clr([%15.15t]){faint} %clr(%-40.40logger{39}){cyan} 
     %clr(:){faint} %m%n%wEx</pattern>
    <encoder class="ch.qos.logback.classic.encoder.PatternLayoutEncoder">
        <pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n
        </pattern>
    </encoder>
   </appender>

   <appender name="file"
    class="ch.qos.logback.core.rolling.RollingFileAppender">
    <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
        <fileNamePattern>${LOG_PATH}projectLog.%d{dd-MM-yyyy}.log
        </fileNamePattern>
        <!-- <maxHistory>30</maxHistory> -->
        <totalSizeCap>3GB</totalSizeCap>
    </rollingPolicy>

   </appender>

  <springProfile name="local">
  <logger name="org.springframework" level="info" additivity="false">
        <appender-ref ref="STDOUT" />
    </logger>
   <root level="info">
        <appender-ref ref="STDOUT" />
    </root>
 </springProfile>
 <springProfile name="test,dev,prod">
 <logger name="org.springframework" level="error" additivity="false">
        <appender-ref ref="file" />
    </logger>
 <root level="info">
        <appender-ref ref="file" />
    </root>
  </springProfile>
0
votes
<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 ,for SpringBoot 1.5.4 replace with [name="dev, staging"]-->
</springProfile>

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

https://docs.spring.io/spring-boot/docs/2.1.8.RELEASE/reference/html/boot-features-logging.html