Class Log4j2PropertiesEditor

java.lang.Object
org.wso2.carbon.logging.service.util.Log4j2PropertiesEditor

public final class Log4j2PropertiesEditor extends Object
  • Method Details

    • readAllLines

      public static ArrayList<String> readAllLines(File file) throws IOException
      Reads all lines from the specified file using UTF-8 encoding.
      Parameters:
      file - the properties file to read
      Returns:
      an ArrayList containing all lines from the file
      Throws:
      IOException - if an I/O error occurs reading from the file
    • getKeyValuesOfAppender

      public static Map<String,String> getKeyValuesOfAppender(File file, String appenderName) throws IOException
      Returns a map of key-value pairs for all properties belonging to the given appender.
      Parameters:
      file - the log4j2.properties file to read
      appenderName - the name of the appender whose properties should be extracted
      Returns:
      a LinkedHashMap containing all key-value pairs for the specified appender
      Throws:
      IOException - if an I/O error occurs reading from the file
    • getProperty

      public static String getProperty(File file, String key) throws IOException
      Return the property value for a given key (first occurrence) or null if not found.
      Throws:
      IOException
    • writeUpdatedAppender

      public static void writeUpdatedAppender(File file, String appenderName, Map<String,String> newProps, boolean merge) throws IOException
      Updates the properties for the given appender in the log4j2.properties file.

      The newProps map specifies the properties to apply to the appender. If a property value is "__REMOVE__" (the literal string) or null, that property will be removed from the appender's configuration in the file.

      The merge parameter controls how the update is performed:

      • If merge is true, the properties in newProps are merged with the existing properties for the appender. Properties with values of "__REMOVE__" or null are deleted. All other properties in newProps are added or updated, while properties not mentioned in newProps are left unchanged.
      • If merge is false, the entire appender block is replaced with only the properties specified in newProps (except those with values of "__REMOVE__" or null, which are omitted).
      Parameters:
      file - the log4j2.properties file to update
      appenderName - the name of the appender to update
      newProps - map of properties to apply; values of "__REMOVE__" or null mark properties for deletion
      merge - if true, merges new properties with existing ones; if false, replaces the entire appender block
      Throws:
      IOException - if an I/O error occurs reading from or writing to the file