How can I configure system properties or logback configuration variables from typesafe config?

后端 未结 3 1616
死守一世寂寞
死守一世寂寞 2021-02-02 14:15

I have variables with defaults in my logback.xml configuration file, and I would like to be able to optionally set these variables from my typesafe config app

3条回答
  •  孤街浪徒
    2021-02-02 14:46

    You can use the PropertyDefiner interface that logback provides. Non trivial scaffolding but allows you to configure using XML instead of within your application. E.g.:

    package com.myapp;
    
    import ch.qos.logback.core.PropertyDefinerBase;
    import com.typesafe.config.ConfigFactory;
    
    public class TypesafeConfigPropertyDefiner extends PropertyDefinerBase {
    
        private String propertyName;
    
        @Override
        public String getPropertyValue() {
            return ConfigFactory.load().getString( propertyName );
        }
    
        public void setPropertyName( String propertyName ) {
            this.propertyName = propertyName;
        }
    }
    

    Then, in your logback.xml file:

    
        
            myapp.logging.loglevel
        
        
           ...
        
    
    

    Now, the above logback.xml file will read myapp.logging.loglevel from your typesafe config file (e.g. application.conf).

提交回复
热议问题