0

I am having some trouble writing the ScriptFilter as the resources for this on the Internet/StackOverflow are quite limited. I hope someone from the community will be able to guide me :)

I want to filter out certain logs depending on the environment the application has been deployed in. We configure the environment by setting the SPRING_ARGS_PORT in the Bash script in a Jenkins build job, as follows:

export SPRING_ARGS_PORT="--server.port=$PORT --env=dev1"

In the above Bash script, we deployed the application to the dev1 environment.

The application jar file is then run with java -jar application.jar --SPRING_ARGS_PORT=$SPRING_ARGS_PORT.

The log4j2.xml file I've written as follows:

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


<Console name="prodOutput" target="SYSTEM_OUT">
        <PatternLayout pattern="..."/>
        <Filters>
            <ScriptFilter onMatch="DENY" onMismatch="NEUTRAL">
            <Script name="checkLoggerOutType" language="JavaScript"><![CDATA[
                var value = "${env:SPRING_ARGS_PORT}";
                var envIndex = value.indexOf("--env=");
                if (envIndex >= 0 && value.substring(envIndex + "--env=".length) == "dev1") {
                    return true;
                } else {
                    return false;
                }
            ]]></Script>
        </ScriptFilter>
        </Filters>
</Console>


</configuration>

What I'm trying to accomplish is to filter out logs when the environment is "dev1" (as defined in SPRING_ARGS_PORT), but it doesn't seem to work as I'm still seeing the logs

Would anyone be able to help point out what I'm doing wrong or missing? Thank you :)

The StackOverflow posts I referenced for the syntax of writing a ScriptFilter were the following:

I'm guessing I got the syntax right by comparing against these 2 posts, perhaps my logic is wrong or I'm missing something else?

waffledood
  • 193
  • 8

1 Answers1

1

I figured it out, the JavaScript code should be written as follows:

<Script name="checkLoggerOutType" language="JavaScript"><![CDATA[
    var value = "${env:SPRING_ARGS_PORT}";
    var envIndex = value.indexOf("--env=");
    if (envIndex >= 0 && value.substring(envIndex + "--env=".length) == "dev1") {
        true;
    } else {
        false;
    }
]]></Script>

Note the difference is that the boolean values should not be returned, they should be written plainly as such.

This format was clearly present in the first StackOverflow post I referenced, but I had missed that small detail.

Hope this is helpful to anyone who might be looking into using the ScriptFilter for log4j

waffledood
  • 193
  • 8