Class VariableSubstitutionsHelper

java.lang.Object
ch.qos.logback.core.spi.ContextAwareBase
ch.qos.logback.core.model.util.VariableSubstitutionsHelper
All Implemented Interfaces:
ContextAware, ContextAwarePropertyContainer, PropertyContainer

Helper methods to deal with properties.

This class acts as a small container for substitution properties and delegates actual variable substitution to OptionHelper.substVars(java.lang.String, ch.qos.logback.core.spi.PropertyContainer). It also offers a convenience method to mask confidential property values (for example passwords) by returning a blurred placeholder.

Since:
1.5.1
  • Field Details

  • Constructor Details

    • VariableSubstitutionsHelper

      Create a helper backed by an empty property map.
      Parameters:
      context - the logback context to associate with this helper; may be null
    • VariableSubstitutionsHelper

      public VariableSubstitutionsHelper(Context context, Map<String,String> otherMap)
      Create a helper pre-populated with the contents of otherMap. The provided map is copied and further modifications do not affect the original map.
      Parameters:
      context - the logback context to associate with this helper; may be null
      otherMap - initial properties to copy; if null an empty map is created
  • Method Details

    • subst

      public String subst(String ref)
      Perform variable substitution on the provided reference string.

      Returns null if ref is null. On parse errors the original input string is returned and an error is logged.

      Specified by:
      subst in interface ContextAwarePropertyContainer
      Parameters:
      ref - the string possibly containing variables to substitute
      Returns:
      the string with substitutions applied, or null if ref was null
    • sanitizeIfConfidential

      public String sanitizeIfConfidential(String ref, String substituted)
      Return a blurred placeholder for confidential properties.

      If the property name ref contains any of the case-insensitive substrings "password", "secret" or "confidential" this method returns a fixed blurred string ("******"). Otherwise, the supplied substituted value is returned unchanged.

      Parameters:
      ref - the property name to inspect; must not be null
      substituted - the substituted value to return when the property is not confidential
      Returns:
      a blurred placeholder when the property appears confidential, otherwise substituted
      Throws:
      IllegalArgumentException - when ref is null
    • addSubstitutionProperty

      public void addSubstitutionProperty(String key, String value)
      Add or overwrite a substitution property.

      Null keys or values are ignored. Values are trimmed before storing to avoid surprises caused by leading or trailing whitespace.

      Specified by:
      addSubstitutionProperty in interface PropertyContainer
      Parameters:
      key - the property name; ignored if null
      value - the property value; ignored if null
    • getProperty

      public String getProperty(String key)
      Retrieve a property value by name.
      Specified by:
      getProperty in interface PropertyContainer
      Parameters:
      key - the property name
      Returns:
      the property value or null if not present
    • getCopyOfPropertyMap

      Return a shallow copy of the internal property map.

      The returned map is a copy and modifications to it do not affect the internal state of this helper.

      Specified by:
      getCopyOfPropertyMap in interface PropertyContainer
      Returns:
      a copy of the property map