Unable to use Spring Property Placeholders in logback.xml

后端 未结 5 1417
有刺的猬
有刺的猬 2020-12-04 22:08

I have a Spring Boot console app using Logback. All of the properties (for the app as well as for Logback) are externalized into a standard application.properties file in th

相关标签:
5条回答
  • 2020-12-04 22:21

    ${...} is not "Spring EL" in Spring; they are property placeholders.

    I think you are confusing logback "variables" with Spring "Property Placeholders".

    They just happen to use the same syntax ${...}.

    logback knows nothing about the Spring property placeholder mechanism and vice-versa. You need to configure your logback variables according to the logback documentation and not in application.properties / application.yml which is strictly a Spring (boot) concept.

    EDIT:

    After a quick look at the logback docs, adding

    <property resource="application.properties" />
    

    to the logback.xml should work.

    0 讨论(0)
  • 2020-12-04 22:32

    The solutions above work mostly for bootrap.properties. However, the only way to use properties from remote Spring Config Server in logback config I've currently found, is to apply them programatically:

    @Component
    public class LoggerConfiguration implements ApplicationListener<EnvironmentChangeEvent> {
    
        @Autowired protected Environment environment;
    
        @Override
        public void onApplicationEvent(EnvironmentChangeEvent event) {
            // enviroment here has already loaded all properties and you may alter logback config programatically
            ch.qos.logback.classic.Logger rootLogger = (ch.qos.logback.classic.Logger) LoggerFactory.getLogger(Logger.ROOT_LOGGER_NAME);
        }
    
    
    }
    

    Here is a good example how to customize logback with new appender this way.

    0 讨论(0)
  • 2020-12-04 22:37

    As answered above you can access the spring boot properties using the <springProperty> element...but a thing to keep in mind is that the logback configuration file must be named logback-spring.xml, it doesn't work if you name the file logback.xml (I'm using spring-boot 1.3.5.RELEASE)

    0 讨论(0)
  • 2020-12-04 22:38

    There is a way to map Spring properties to Logback properties and use them in the Conditions:

    logback-spring.xml
    
    <?xml version="1.0" encoding="UTF-8"?>
    <configuration>
    <include resource="org/springframework/boot/logging/logback/defaults.xml"/>
    <springProperty scope="context" name="ACTIVE_PROFILE" source="spring.profiles.active"/>
    <!--   defined in the application-prod.properties/>-->
    <springProperty scope="context" name="SPRING_WRITER_DISABLED" source="writer.disabled"/>
    <property name="LOGBACK_WRITER_DISABLED" value="${SPRING_WRITER_DISABLED}"/>
    
    <if condition='property("LOGBACK_WRITER_DISABLED").equals("false")'>
        <then>
            <appender name="testappender" class="ch.qos.logback.core.rolling.RollingFileAppender">
                <file>${ACTIVE_PROFILE}/${HOSTNAME}/testappender.log</file>
                <rollingPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedRollingPolicy">
                    <fileNamePattern>
                        {ACTIVE_PROFILE}/${HOSTNAME}/testappender-%d{yyyy-MM-dd}.%i.log
                    </fileNamePattern>
                    <maxFileSize>300MB</maxFileSize>
                    <maxHistory>3</maxHistory>
                    <totalSizeCap>1GB</totalSizeCap>
                </rollingPolicy>
                <encoder>
                    <pattern>%msg%n</pattern>
                </encoder>
            </appender>
            <logger name="testappender" level="INFO" additivity="false">
                <appender-ref ref="testappender"/>
            </logger>
        </then>
    </if>
    </configuration>
    
    0 讨论(0)
  • 2020-12-04 22:39

    Since Spring Boot 1.3 you have a better way of getting spring properties into your logback-spring.xml configuration:

    Now you can just add a "springProperty" element.

    <springProperty name="destination" source="my.loggger.extradest"/>
    <appender name="FILE" class="ch.qos.logback.core.FileAppender">
        <file>${destination}</file>
            ...
        </file>
    </appender>
    

    https://github.com/spring-projects/spring-boot/commit/055ace37f006120b0006956b03c7f358d5f3729f

    edit: thanks to Anders

    .........

    0 讨论(0)
提交回复
热议问题