简体   繁体   中英

Log4j2 routing appender JavaScript config error

I am using lombok's log4j2 logger and I need to config routing appender based on ThreadContext map. Route keys are determined with a script. Here is the whole log4j2.xml config file:

<Appenders>
    <Console name="Console" target="SYSTEM_OUT">
        <PatternLayout pattern="%highlight{%d{yyyy-MM-dd HH:mm:ss} [%t] %-5level %c{1}: %msg%n%throwable}"/>
    </Console>

    <RollingFile name="GeneralRollingFile" filename="log/test-log.log"
                 filepattern="log/test-log-%d{yyyy-MM-dd HH:mm:ss}.log">
        <PatternLayout pattern="%d{yyyy-MM-dd HH:mm:ss} [%t] %-5level %c{1}: %msg%n%throwable"/>
        <Policies>
            <SizeBasedTriggeringPolicy size="25 MB"/>
        </Policies>
        <DefaultRolloverStrategy max="20"/>
    </RollingFile>

    <Routing name="Routing">

        <Routes>
            <Script name="RoutingInit" language="JavaScript">
                <![CDATA[
                if (logEvent.getContextMap().containsKey("operation-1")) {
                    return "operation-1";
                } else if (logEvent.getContextMap().containsKey("operation-2")) {
                    return "operation-2";
                } else {
                    return "general";
                }
            ]]>
            </Script>

            <Route key="general" ref="GeneralRollingFile"/>

            <Route key="operation-1"> 
                <RollingFile name="operation-1-rolling"
                             fileName="log/operation-1/${ctx:operation-1}.log"
                             filePattern="log/operation-1/${ctx:operation-1}-%d{yyyy-MM-dd HH:mm:ss}.log">

                    <PatternLayout pattern="%d{yyyy-MM-dd HH:mm:ss} [%t] %-5level %c{1}: %msg%n%throwable"/>
                    <Policies>
                        <SizeBasedTriggeringPolicy size="25 MB"/>
                    </Policies>
                    <DefaultRolloverStrategy max="20"/>
                </RollingFile>
            </Route>

            <Route key="operation-2"> 
                <RollingFile name="operation-2-rolling"
                             fileName="log/operation-2/${ctx:operation-2}.log"
                             filePattern="log/operation-2/${ctx:operation-2}-%d{yyyy-MM-dd HH:mm:ss}.log">

                    <PatternLayout pattern="%d{yyyy-MM-dd HH:mm:ss} [%t] %-5level %c{1}: %msg%n%throwable"/>
                    <Policies>
                        <SizeBasedTriggeringPolicy size="25 MB"/>
                    </Policies>
                    <DefaultRolloverStrategy max="20"/>
                </RollingFile>
            </Route>

        </Routes>


    </Routing>

</Appenders>

<Loggers>
    <Root level="trace" additivity="false">
        <AppenderRef ref="Console"/>
        <AppenderRef ref="Routing"/>
    </Root>
</Loggers>

However, I am getting the following error:

 main ERROR Error running script RoutingInit javax.script.ScriptException: <eval>:2:24 Invalid return statement
                    return "operation-1";

Log4j2 documentation gives us a similar script example, which does not work for me ether. I am fairly new to JS, but this code seems as a valid script. Am I getting something wrong?

Thanks in advance.

(Diclaimer: I have never used log4j2, nor embedding Javascript in Java, etc. Anyway, I looked around when trying to answer your question...)

Doesn't it looks similar to https://stackoverflow.com/a/38034571/118587 ?

In that sense, if I understand properly what they suggest is that you shouldn't use return if it's not a function and instead the returned value will be the value of the last expression, so (without checking) I can take a guess and say the following will work:

        <Script name="RoutingInit" language="JavaScript">
            <![CDATA[
            var ret = "general";
            if (logEvent.getContextMap().containsKey("operation-1")) {
                ret = "operation-1";
            } else if (logEvent.getContextMap().containsKey("operation-2")) {
                ret = "operation-2";
            }
            ret;
        ]]>
        </Script>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM