我正在使用龙目岛的log4j2记录器,我需要配置基于ThreadContext映射的路由附加器。路由键由脚本确定。下面是整个log4j2.xml配置文件:
<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>
但是,我得到了以下错误:
main ERROR Error running script RoutingInit javax.script.ScriptException: <eval>:2:24 Invalid return statement
return "operation-1";Log4j2文档给出了一个类似的脚本示例,但它不适用于我。我是JS的新手,但这段代码似乎是一个有效的脚本。我是不是搞错了?
提前谢谢。
发布于 2017-01-27 18:44:04
(我从来没有使用过log4j2,也没有在Java中嵌入Javascript,等等。不管怎样,我在回答你的问题时环顾了一下……)
它看起来不像https://stackoverflow.com/a/38034571/118587吗?
从这个意义上说,如果我正确理解了他们的建议,那就是如果return不是一个函数,那么你就不应该使用它,相反,返回的值将是最后一个表达式的值,所以(不用检查)我可以猜测并说下面的方法是可行的:
<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>https://stackoverflow.com/questions/41790290
复制相似问题