4

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 ?

opuser1
  • 427
  • 7
  • 29

3 Answers3

3

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.

Ondrej Burkert
  • 6,617
  • 2
  • 32
  • 27
  • Yes, i eventually listed all of them to get it working. I was following the documentation https://docs.spring.io/spring-boot/docs/current/reference/html/boot-features-logging.html#_profile_specific_configuration mentioning that it can be a expression but somehow couldn't get it working. – opuser1 Feb 06 '19 at 23:16
  • Feel free to mark the answer as correct or helpful if it helped you. I'd say the documentation is a bit misleading in this case since it says you can do `dev | staging` or you can do `!production` but it won't tell you you cannot do both. We also got bitten by this some time ago... – Ondrej Burkert Feb 07 '19 at 10:53
  • Documentation does mention that we can use such expression `production & (eu-central | eu-west)` – opuser1 Feb 07 '19 at 15:28
1

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>
techasutos
  • 121
  • 6
0
<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

dawen
  • 19
  • 3