为什么我看不到Mule日志文件中java组件中发生的错误?另外假设我在java中有system.out.print,如何在日志文件中看到输出?
答案 0 :(得分:1)
我假设你正在编写自己的Java组件来实现可调用。
要在Java组件的日志中写入内容,您需要获取一个记录器,例如:
final static Logger logger = Logger.getLogger(MyCallableClass.class);
请参阅https://www.mkyong.com/logging/log4j-hello-world-example/
然后,您可以在函数中创建日志条目,例如:
logger.error("This is an example of a log entry with Error Level.");
对于您的其他问题,除非您在组件中使用try catch块,否则组件中抛出的异常应该传播到mule流,但是在Mule Logs中您还会看到异常是由Java引起的零件。
我建议您附上有关Java组件正在执行的操作的更多信息以及日志,以便我们能够更好地了解您遇到的问题。
那就是说,我创建了一个快速示例(见下文)来帮助你。当您运行并点击http端点时,您应该看到记录器中打印的消息是组件内部的错误,然后抛出一个自定义异常,它将在与我们的表达式匹配的catch异常策略块中捕获:
<强>流量:强>
<flow name="flow-that-calls-component-with-exception">
<http:inbound-endpoint exchange-pattern="request-response" host="localhost" port="8089" doc:name="HTTP" />
<component class="com.example.MyCallableClass" doc:name="Java" />
<choice-exception-strategy>
<catch-exception-strategy when="exception.causedBy(com.example.MyCustomException)">
<logger level="ERROR" message="My Custom Exception was thrown" />
<set-payload value="My Custom Exception was thrown." />
</catch-exception-strategy>
<catch-exception-strategy>
<logger level="ERROR" message="Another exception was thrown" />
<set-payload value="Another exception was thrown." />
</catch-exception-strategy>
</choice-exception-strategy>
</flow>
Java组件:
package com.example;
import org.apache.log4j.Logger;
import org.mule.api.MuleEventContext;
import org.mule.api.lifecycle.Callable;
public class MyCallableClass implements Callable {
final static Logger logger = Logger.getLogger(MyCallableClass.class);
@Override
public Object onCall(MuleEventContext eventContext) throws Exception {
logger.error("This is an example of a log entry with Error Level.");
//Our Java component will throw an exception so that we can see that we can catch it in a flow
throw new MyCustomException();
// throw new Exception("This is an exception that will be thrown in a flow");
// return eventContext.getMessage().getPayload();
}
}
自定义例外:
package com.example;
public class MyCustomException extends Exception {
/**
*
*/
private static final long serialVersionUID = -5738957517051661541L;
}
答案 1 :(得分:0)