我在我的应用程序中使用log4j进行日志记录。我希望根据文件系统的使用情况删除压缩文件。因此,一旦文件系统的使用率达到80%,它就应该开始删除旧的压缩文件。Log4j没有任何可以立即执行此操作的appender,但它有可以使用的ScriptCondition。在log4j文档中,我只找到了groovy脚本(我们的系统上没有安装Groovy)。它还提到JavaScript,Nashorn也可以工作。我写了下面的脚本只是为了看看它是否运行。
<configuration status="trace" name="REST Servlet Logging Configuration">
<Properties>
<Property name="xx">xx</Property>
</Properties>
<appenders>
<RollingFile name="xx"
fileName="xx"
filePattern="xx.log.%i.gz" append="true"
bufferedIO="true" immediateFlush="false" fileOwner="xx"
fileGroup="xx" filePermissions="rw-r-----">
<Policies>
<SizeBasedTriggeringPolicy size="4 MB" />
</Policies>
<DefaultRolloverStrategy max="100" fileIndex="min">
<Delete basePath="${xx}" maxDepth="1">
<ScriptCondition>
<Script name="superstitious" language="javascript"><![CDATA[
var exec = require('child_process');
exec("df -h / | tail -1 | tr -s ' ' | cut -d' ' -f4", (error, stdout, stderr) => {
if(error) {
console.log('error: ${error.message}');
return;
}
if(stderr) {
console.log('stderr: ${stderr}');
return;
}
console.log('stdout: ${stdout}');
statusLogger.trace("stdout in trace :${stdout}");
});
statusLogger.trace("running javascript");
result;
]]>
</Script>
</ScriptCondition>
</Delete>
</DefaultRolloverStrategy>
</RollingFile>
在日志中,我可以看到提到了javascript,但错误地,我得到了nashorn异常。
05:58:46 UTC 2021 DEBUG createScript(name="superstitious", language="javascript", scriptText="var exec = require('child_process');
2021-11-02T05:58:47.197Z exec("df -h / | tail -1 | tr -s ' ' | cut -d' ' -f4", (error, stdout, stderr) => {
2021-11-02T05:58:47.197Z if(error) {
2021-11-02T05:58:47.197Z console.log('error: ${error.message}');
2021-11-02T05:58:47.197Z return;
2021-11-02T05:58:47.197Z }
2021-11-02T05:58:47.197Z if(stderr) {
2021-11-02T05:58:47.197Z console.log('stderr: ${stderr}');
2021-11-02T05:58:47.197Z return;
2021-11-02T05:58:47.197Z }
2021-11-02T05:58:47.197Z console.log('stdout: ${stdout}');
2021-11-02T05:58:47.197Z statusLogger.trace("stdout in trace :${stdout}");
2021-11-02T05:58:47.197Z });
但我遇到以下异常
2021-11-02T06:00:41.581Z 2021-02-11 06:00:41,575 Log4j2-TF-1-RollingFileManager-1 ERROR Error running script superstitious javax.script.ScriptException: <eval>:3:11 Expected : but found (
2021-11-02T06:00:41.581Z if(error) {
2021-11-02T06:00:41.582Z ^ in <eval> at line number 3 at column number 11
2021-11-02T06:00:41.582Z at jdk.nashorn.api.scripting.NashornScriptEngine.throwAsScriptException(NashornScriptEngine.java:482)
2021-11-02T06:00:41.582Z at jdk.nashorn.api.scripting.NashornScriptEngine.compileImpl(NashornScriptEngine.java:549)
2021-11-02T06:00:41.582Z at jdk.nashorn.api.scripting.NashornScriptEngine.compileImpl(NashornScriptEngine.java:536)
2021-11-02T06:00:41.582Z at jdk.nashorn.api.scripting.NashornScriptEngine.evalImpl(NashornScriptEngine.java:414)
2021-11-02T06:00:41.590Z at jdk.nashorn.api.scripting.NashornScriptEngine.eval(NashornScriptEngine.java:167)
2021-11-02T06:00:41.590Z at javax.script.AbstractScriptEngine.eval(AbstractScriptEngine.java:244)
2021-11-02T06:00:41.590Z at org.apache.logging.log4j.core.script.ScriptManager$MainScriptRunner.execute(ScriptManager.java:239)
2021-11-02T06:00:41.590Z at org.apache.logging.log4j.core.script.ScriptManager$ThreadLocalScriptRunner.execute(ScriptManager.java:269)
2021-11-02T06:00:41.590Z at org.apache.logging.log4j.core.script.ScriptManager$1.run(ScriptManager.java:177)
2021-11-02T06:00:41.590Z at java.security.AccessController.doPrivileged(AccessController.java:678)
2021-11-02T06:00:41.590Z at org.apache.logging.log4j.core.script.ScriptManager.execute(ScriptManager.java:174)
2021-11-02T06:00:41.590Z at org.apache.logging.log4j.core.appender.rolling.action.ScriptCondition.selectFilesToDelete(ScriptCondition.java:81)
2021-11-02T06:00:41.590Z at org.apache.logging.log4j.core.appender.rolling.action.DeleteAction.callScript(DeleteAction.java:98)
2021-11-02T06:00:41.590Z at org.apache.logging.log4j.core.appender.rolling.action.DeleteAction.executeScript(DeleteAction.java:86)
2021-11-02T06:00:41.590Z at org.apache.logging.log4j.core.appender.rolling.action.DeleteAction.execute(DeleteAction.java:82)
2021-11-02T06:00:41.590Z at org.apache.logging.log4j.core.appender.rolling.action.CompositeAction.execute(CompositeAction.java:74)
2021-11-02T06:00:41.590Z at org.apache.logging.log4j.core.appender.rolling.RollingFileManager$AsyncAction.execute(RollingFileManager.java:486)
2021-11-02T06:00:41.590Z at org.apache.logging.log4j.core.appender.rolling.action.AbstractAction.run(AbstractAction.java:66)
2021-11-02T06:00:41.590Z at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1160)
2021-11-02T06:00:41.590Z at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:635)
2021-11-02T06:00:41.590Z at java.lang.Thread.run(Thread.java:822)
2021-11-02T06:00:41.590Z Caused by: jdk.nashorn.internal.runtime.ParserException: <eval>:3:11 Expected : but found (
2021-11-02T06:00:41.590Z if(error) {
2021-11-02T06:00:41.590Z ^
2021-11-02T06:00:41.590Z at jdk.nashorn.internal.parser.AbstractParser.error(AbstractParser.java:306)
2021-11-02T06:00:41.590Z at jdk.nashorn.internal.parser.AbstractParser.error(AbstractParser.java:291)
2021-11-02T06:00:41.590Z at jdk.nashorn.internal.parser.AbstractParser.expectDontAdvance(AbstractParser.java:362)
2021-11-02T06:00:41.590Z at jdk.nashorn.internal.parser.AbstractParser.expect(AbstractParser.java:349)
2021-11-02T06:00:41.590Z at jdk.nashorn.internal.parser.Parser.propertyAssignment(Parser.java:2309)
2021-11-02T06:00:41.590Z at jdk.nashorn.internal.parser.Parser.objectLiteral(Parser.java:2167)
我不知道为什么它试图编译它作为nashorn脚本时,我清楚地提到javascript。我是新的脚本,所以不知道什么是错的。
根据的建议,我将=〉更改为函数,并得到另一个异常
2021-12-02T06:56:30.192Z 2021-02-12 06:56:30,185 Log4j2-TF-1-RollingFileManager-2 ERROR Error running script superstitious javax.script.ScriptException: ReferenceError: "require" is not defined in <eval> at line number 1
2021-12-02T06:56:30.192Z at jdk.nashorn.api.scripting.NashornScriptEngine.throwAsScriptException(NashornScriptEngine.java:482)
2021-12-02T06:56:30.192Z at jdk.nashorn.api.scripting.NashornScriptEngine.evalImpl(NashornScriptEngine.java:438)
2021-12-02T06:56:30.192Z at jdk.nashorn.api.scripting.NashornScriptEngine.access$300(NashornScriptEngine.java:85)
2021-12-02T06:56:30.192Z at jdk.nashorn.api.scripting.NashornScriptEngine$3.eval(NashornScriptEngine.java:526)
2021-12-02T06:56:30.192Z at javax.script.CompiledScript.eval(CompiledScript.java:103)
2021-12-02T06:56:30.192Z at org.apache.logging.log4j.core.script.ScriptManager$MainScriptRunner.execute(ScriptManager.java:232)
2021-12-02T06:56:30.192Z at org.apache.logging.log4j.core.script.ScriptManager$ThreadLocalScriptRunner.execute(ScriptManager.java:269)
2021-12-02T06:56:30.192Z at org.apache.logging.log4j.core.script.ScriptManager$1.run(ScriptManager.java:177)
2021-12-02T06:56:30.192Z at java.security.AccessController.doPrivileged(AccessController.java:678)
2021-12-02T06:56:30.192Z at org.apache.logging.log4j.core.script.ScriptManager.execute(ScriptManager.java:174)
2021-12-02T06:56:30.192Z at org.apache.logging.log4j.core.appender.rolling.action.ScriptCondition.selectFilesToDelete(ScriptCondition.java:81)
2021-12-02T06:56:30.192Z at org.apache.logging.log4j.core.appender.rolling.action.DeleteAction.callScript(DeleteAction.java:98)
2021-12-02T06:56:30.192Z at org.apache.logging.log4j.core.appender.rolling.action.DeleteAction.executeScript(DeleteAction.java:86)
2021-12-02T06:56:30.192Z at org.apache.logging.log4j.core.appender.rolling.action.DeleteAction.execute(DeleteAction.java:82)
2021-12-02T06:56:30.192Z at org.apache.logging.log4j.core.appender.rolling.action.CompositeAction.execute(CompositeAction.java:74)
2021-12-02T06:56:30.192Z at org.apache.logging.log4j.core.appender.rolling.RollingFileManager$AsyncAction.execute(RollingFileManager.java:486)
2021-12-02T06:56:30.192Z at org.apache.logging.log4j.core.appender.rolling.action.AbstractAction.run(AbstractAction.java:66)
2021-12-02T06:56:30.192Z at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1160)
2021-12-02T06:56:30.192Z at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:635)
2021-12-02T06:56:30.192Z at java.lang.Thread.run(Thread.java:822)
2021-12-02T06:56:30.192Z Caused by: <eval>:1 ReferenceError: "require" is not defined
2021-12-02T06:56:30.192Z at jdk.nashorn.internal.runtime.ECMAErrors.error(ECMAErrors.java:69)
2021-12-02T06:56:30.192Z at jdk.nashorn.internal.runtime.ECMAErrors.referenceError(ECMAErrors.java:331)
2021-12-02T06:56:30.192Z at jdk.nashorn.internal.runtime.ECMAErrors.referenceError(ECMAErrors.java:303)
2021-12-02T06:56:30.192Z at jdk.nashorn.internal.objects.Global.__noSuchProperty__(Global.java:1454)
2021-12-02T06:56:30.192Z at java.lang.invoke.DirectHandle.invokeExact_thunkArchetype_L(DirectHandle.java:302)
2021-12-02T06:56:30.192Z at java.lang.invoke.AsTypeHandle.invokeExact_thunkArchetype_X(AsTypeHandle.java:49)
2021-12-02T06:56:30.192Z at java.lang.invoke.BruteArgumentMoverHandle.invokeExact_thunkArchetype_X(BruteArgumentMoverHandle.java:404)
2021-12-02T06:56:30.192Z at jdk.nashorn.internal.scripts.Script$1$^eval_.:program(<eval>:1)
2021-12-02T06:56:30.192Z at java.lang.invoke.DirectHandle.invokeExact_thunkArchetype_L(DirectHandle.java:302)
2021-12-02T06:56:30.192Z at jdk.nashorn.internal.runtime.ScriptFunctionData.invoke(ScriptFunctionData.java:649)
2021-12-02T06:56:30.192Z at jdk.nashorn.internal.runtime.ScriptFunction.invoke(ScriptFunction.java:506)
2021-12-02T06:56:30.192Z at jdk.nashorn.internal.runtime.ScriptRuntime.apply(ScriptRuntime.java:405)
2021-12-02T06:56:30.192Z at jdk.nashorn.api.scripting.NashornScriptEngine.evalImpl(NashornScriptEngine.java:433)
1条答案
按热度按时间ttisahbt1#
我认为这是因为您尝试使用箭头函数,它是ECMAScript 6构造,而Nashorn默认使用ECMAScript 5.1。
您可以尝试使用
function (error, stdout, stderr) { … }
而不是(error, stdout, stderr) => { … }
,我相信这会起作用。TL;DR:
Nashorn确实支持ES6的一个不错的子集,但需要使用嵌入式通常不支持的标志来启用它,因为您需要在
javax.script
API之外使用Nashorn自己的APINashornScriptEngineFactory.getScriptEngine(String...)
,您可以在其中传递这些标志,因此,FWIW,如果你要求的语言是“ecmascript-6”或类似的,我们提供一个ES6默认引擎是有意义的。我会考虑这个问题。