Log4j with java scripting in XML configuration

Viewed 56

I am trying to use the Scripts in the configuration of Log4j2 to return a value that will used as a header in the HTTP appender

https://logging.apache.org/log4j/2.x/manual/configuration.html#Scripts

Here is my log4j2http.xml

<?xml version="1.0" encoding="UTF-8"?>
<Configuration status="warn" name="MyApp">
  <Scripts>
    <Script name="selector" language="java">
      <![CDATA[
            var result;
            result=1;
            result;
            ]]>
    </Script>
  </Scripts>
  <Appenders>
    <Http name="Http" url="https://myurl.com/ingest" method="POST">
      <JsonLayout properties="true" />
      <KeyValuePair key="resourceId:" value="{deviceId:&quot;588&quot;}" />
      <Property name="Authorization" value="{result}" />
      <Property name="Accept" value="application/json" />
      <Property name="Version" value="2" />
    </Http>

  </Appenders>
  <Loggers>
    <Root level="error">
      <AppenderRef ref="Http" />
    </Root>
  </Loggers>
</Configuration>

And my app

public class App {

    static Logger logger = LogManager.getLogger("App");

    public static void main(String[] args) {
        
        System.setProperty("log4j2.Script.enableLanguages","java");
        System.out.println(System.getProperties());
        LoggerContext context = (org.apache.logging.log4j.core.LoggerContext) LogManager.getContext(false);
        File file = new File("log4j2http.xml"); 
        context.setConfigLocation(file.toURI());
        logger.debug("Debug Message Logged !!!");
        logger.info("Info Message Logged !!!");
        logger.error("Error Message Logged !!!");
    }
}

Im getting this error

2022-08-08 08:27:36,051 main ERROR No ScriptEngine found for language java. Available languages are:

The error seems to point to the ScriptEngine not being initialized properly because the Available languages are blank. What is the proper way to initialize the engine?

0 Answers
Related