Search code examples
spring-bootlog4j2graylog

Script support is not enabled - Spring boot 2.7.5


We are using Log4j2 with a script in the configuration (log4j2.xml) of the appenders to use with graylog.

<Appenders>
    <ScriptAppenderSelector name="SelectGelf">
        <Script language="groovy">
          (...)
        </Script>

        <AppenderSet>
           <Gelf...>
           </Gelf>
        </AppenderSet>
    </ScriptAppenderSelector>

When we upgraded to Spring boot 2.7.5, these errors start occurring, because, AFAIK, the scripts are disabled by default.

main ERROR Script support is not enabled
main ERROR Null object returned for ScriptAppenderSelector in Appenders.

Adding this in the main() method of the Application, solves the problem but it seems a little hacky and besides that we will need to do this modification in all applications:

System.setProperty("log4j2.Script.enableLanguages","groovy");

Our Log4j2 configurations are in a separate library that we use in all the spring boot projects.

Is there an alternative method to solve this? If possible, doing it within the log library, without affecting other projects that utilize the library. Thank you!


Solution

  • You can use any of the available property sources (cf. documentation) to enabled scripting.

    For example you can include a log4j2.component.properties file in your configuration JAR with content:

    log4j2.Script.enableLanguages = groovy
    

    Remark: if you choose to use the Spring property source it will work only once Spring's Environment is loaded, so in a log4j2-spring.xml configuration file, not the default log4j2.xml file.