Should multiple Propertybackback Logback implementations be created to create multiple properties?

I can use Logback PropertyDefiner to access one property from the logback.xml config file. If I have 3 properties to access, I am currently using 3 separate PropertyDefiner implementations (one for each property).

Is there a way to access multiple properties from a single PropertyDefiner implementation? Or maybe there is another interface that supports multiple properties?

I want to be able to use plugin properties of different values ​​based on environment (dev, ist, uat, perf, prod) for different logging configurations (context name, log levels, application file names, file sizes, etc ..).

I found this question , which is similar, but did not answer the question of how to access multiple properties.

+3


source to share


2 answers


Create a PropertyDefiner class . PropertyDefinerBase is already implemented.

package foo.bar;

import java.util.HashMap;
import java.util.Map;

import ch.qos.logback.core.PropertyDefinerBase;

public class LoggingPropertiesDefiner extends PropertyDefinerBase {

  private static Map<String, String> properties = new HashMap<>();

  static {
    properties.put("encoderPattern", "%d{yyyy-MM-dd HH:mm:ss.SSS} [%thread] %-5level %logger - %message%n");
    properties.put("maxFileSize", "50MB");
    properties.put("rootLogLevel", "INFO");
  }

  private String propertyLookupKey;

  public void setPropertyLookupKey(String propertyLookupKey) {
    this.propertyLookupKey = propertyLookupKey;
  }

  @Override
  public String getPropertyValue() {
    //TODO In the real world, get properties from a properties loader.
    return properties.get(propertyLookupKey);
  }
}

      

The logback.xml uses the same PropertyDefiner class for each property. Provide a different search key for each:



<define name="encoderPattern" class="foo.bar.LoggingPropertiesDefiner">
    <propertyLookupKey>encoderPattern</propertyLookupKey>
</define>

<define name="maxFileSize" class="foo.bar.LoggingPropertiesDefiner">
    <propertyLookupKey>maxFileSize</propertyLookupKey>
</define>

<define name="rootLogLevel" class="foo.bar.LoggingPropertiesDefiner">
    <propertyLookupKey>rootLogLevel</propertyLookupKey>
</define>

      

Link to property names in logback.xml file:

<root level="${rootLogLevel}">
    <appender-ref ref="FILE"/>
</root>

      

+4


source


You can use resource resource support from log

logback.properties

mode=prod    

      

logback.xml

<?xml version="1.0" encoding="UTF-8"?>
<configuration scan="true">
    <property resource="logback.properties" />

    <if condition='property("mode").equals("prod")'>
        <then>
            <include file="logback-prod.xml" />
        </then>
    </if>
    <if condition='property("mode").equals("dev")'>
        <then>
            <include resource="logback-dev.xml" />
        </then>
    </if>
</configuration>

      

Logback-prod.xml



<?xml version="1.0" encoding="UTF-8"?>

<configuration>

    <!--Daily rolling file appender -->
    <appender name="file" class="ch.qos.logback.core.rolling.RollingFileAppender">
          <File>${MYAPP_HOME}/myApp.log</File>
          <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
          <FileNamePattern>myApp.%d{yyyy-MM-dd}.log</FileNamePattern>
          <MaxHistory>2</MaxHistory>
          </rollingPolicy>
          <layout class="ch.qos.logback.classic.PatternLayout">
          <Pattern>%date %level [%thread] %logger{10} [%file:%line] %msg%n</Pattern>
          </layout>
    </appender> 

    <root level="ERROR">
        <appender-ref ref="file" />
    </root>
</configuration>

      

Logback-dev.xml

<?xml version="1.0" encoding="UTF-8"?>
<configuration>

    <!--Daily rolling file appender -->
    <appender name="file" class="ch.qos.logback.core.rolling.RollingFileAppender">
          <File>${MYAPP_HOME}/myApp.log</File>
          <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
          <FileNamePattern>myApp.%d{yyyy-MM-dd}.log</FileNamePattern>
          <MaxHistory>2</MaxHistory>
          </rollingPolicy>
          <layout class="ch.qos.logback.classic.PatternLayout">
          <Pattern>%date %level [%thread] %logger{10} [%file:%line] %msg%n</Pattern>
          </layout>
    </appender> 


    <root level="TRACE">
        <appender-ref ref="file" />
    </root>
</configuration>

      

More detailed explanation here .

Does this help solve your problem?

0


source







All Articles