我在为Oracle SOA Suite开发自定义log4j2记录器方面做得很好。 这个类是一个胖子或自成一体的罐子。 它有一个log4j2.xml配置文件,带有logger和appender
<?xml version="1.0" encoding="UTF-8"?>
<configuration status="error" name="JSONLogger">
<Properties>
<Property name="log-path">/var/tmp</Property>
</Properties>
<Appenders>
<Console name="Console" target="SYSTEM_OUT" level="FATAL">
<PatternLayout pattern="%m%n" />
</Console>
<RollingRandomAccessFile name="RollingFile" fileName="${log-path}/jsonlogger.json"
filePattern="${log-path}/%d{yyyyMMdd}_jsonlogger-%i.json"
immediateFlush="false">
<PatternLayout>
<pattern>%m%n</pattern>
</PatternLayout>
<Policies>
<TimeBasedTriggeringPolicy />
<SizeBasedTriggeringPolicy size="10 MB" />
</Policies>
<DefaultRolloverStrategy max="4"/>
</RollingRandomAccessFile>
<Async name="Async">
<AppenderRef ref="RollingFile"/>
</Async>
</Appenders>
<Loggers>
<Logger name="JSONLogger" level="trace" additivity="false">
<AppenderRef ref="Async" />
</Logger>
<Root level="fatal">
<AppenderRef ref="Console" />
</Root>
</Loggers>
运行JUnit测试时,记录器会按预期写入jsonlogger.json文件。
在我的weblogic服务器上,我已部署了log4j2.xml配置文件,并将java属性log4j.configurationFile设置为log4j2.xml文件的位置。 当我将jar部署到我的Weblogic 12c服务器时,记录器在从故障策略调用时工作正常。只有它记录到错误的文件。
它将此记录到weblogic诊断文件
<Nov 11, 2016, 5:18:34,278 PM CET> <Error> <JSONLogger> <BEA-000000> <{"LOB":"*** TEST ****","Service":"B","Proces":"C","OptionalFields":{"melding":"Service unavailable for not available for a prolonged period","FaultPolicyID":"RemoteFaults","FaultType":"bpel","Partnerlink":"SimpleSyncUnavail","PortType":"{http://xmlns.oracle.com/JSONLogger/reddippedSimpleUnavail/BPELProcess}BPELProcess"},"level":"ERROR","thread_name":"[ACTIVE] ExecuteThread: '58' for queue: 'weblogic.kernel.Default (self-tuning)'","class":"com.reddipped.jsonlogger.JSONLogger","logger_name":"JSONLogger","@timestamp":"2016-11-11 17:18:34.270+0100"}>
而不是这个到log4j2配置中指定的json文件
{"LOB":"*** TEST ****","Service":"B","Proces":"C","OptionalFields":{"melding":"Service unavailable for not available for a prolonged period","FaultPolicyID":"RemoteFaults","FaultType":"bpel","Partnerlink":"SimpleSyncUnavail","PortType":"{http://xmlns.oracle.com/JSONLogger/reddippedSimpleUnavail/BPELProcess}BPELProcess"},"level":"ERROR","thread_name":"[ACTIVE] ExecuteThread: '58' for queue: 'weblogic.kernel.Default (self-tuning)'","class":"com.reddipped.jsonlogger.JSONLogger","logger_name":"JSONLogger","@timestamp":"2016-11-11 17:18:34.270+0100"}
因此可能使用/config/fmwconfig/servers//logging.xml中定义的odl-logger。
任何调试/解决此问题的方法?
- 更新1 -
我添加了一些调试语句来确定在运行时是否从故障策略执行时使用了正确的配置文件。
try {
System.out.println("**PNE** CLASS LOGGER PATH " + LOGGER.getClass().getProtectionDomain().getCodeSource().getLocation().toURI().getPath()) ;
} catch (Exception e) {
System.out.println("**PNE** CLASS LOGGER PATH FAiled") ;
}
System.out.println("**PNE** Config Location : " + org.apache.logging.log4j.core.LoggerContext.getContext().getConfiguration()) ;
Map<String, Appender> appenders = org.apache.logging.log4j.core.LoggerContext.getContext().getConfiguration().getAppenders() ;
for (String appender : appenders.keySet()) {
System.out.println("**PNE** APPENDER " + appender);
System.out.println("**PNE** APPENDER " + appenders.get(appender).toString());
}
System.out.println("**PNE** SYSTEM PROPERTY " + System.getProperty("log4j.configurationFile") ) ;
System.out.println("**PNE** :-) JSONLogger DEBUG " + lob + ":" + service + ":" + proces ) ;
LOGGER.error()
.field("LOB", lob)
.field("Service",service)
.field("Proces", proces)
.map("OptionalFields", optionalFields).log() ;
确实加载了预期的log4j配置文件,并从记录器上下文中检索了appender。
**PNE** Config Location : XmlConfiguration[location=/u01/data/domains/soa12c/log4j2.xml]
**PNE** APPENDER RollingFile
**PNE** APPENDER RollingFile
**PNE** APPENDER Async
**PNE** APPENDER Async
**PNE** APPENDER Console
**PNE** APPENDER Console
**PNE** SYSTEM PROPERTY /u01/data/domains/soa12c/log4j2.xml
**PNE** :-) JSONLogger DEBUG Unknown:Unknown:Unknown
<Nov 12, 2016, 10:00:40,184 AM CET> <Error> <JSONLogger> <BEA-000000> <{"LOB":"Unknown","Service":"Unknown","Proces":"Unknown","OptionalFields":{"melding":"Service unavailable for not available for a prolonged period","FaultPolicyID":"RemoteFaults","FaultType":"bpel","Partnerlink":"SimpleSyncUnavail","PortType":"{http://xmlns.oracle.com/JSONLogger/reddippedSimpleUnavail/BPELProcess}BPELProcess"},"level":"ERROR","thread_name":"[ACTIVE] ExecuteThread: '0' for queue: 'weblogic.kernel.Default (self-tuning)'","class":"com.reddipped.jsonlogger.JSONLogger","logger_name":"JSONLogger","@timestamp":"2016-11-12 10:00:40.184+0100"}>
**>>PNE** JSONLOGGERUnknownUnknownUnknownUnknown{melding=Service unavailable for not available for a prolonged period, FaultPolicyID=RemoteFaults, FaultType=bpel, Partnerlink=SimpleSyncUnavail, PortType={http://xmlns.oracle.com/JSONLogger/reddippedSimpleUnavail/BPELProcess}BPELProcess}
让我感到困惑的是,实际的日志记录仍记录在诊断日志中。
- 更新2 -
如前所述,日志不会发送到诊断日志,而是发送到server.out文件。因此log4j记录似乎已经路由到控制台而不是记录器。
log4j2.xml确实包含错误,Console appender具有非法属性&#34; level&#34;。删除此文件后,将创建文件/var/tmp/jsonlogger.json,但保持为空。记录仍然发送到server.out。
- 更新3 -
JUL是否拦截了日志记录?如果是这样,我如何确定它是否被截获?
答案 0 :(得分:1)
SLF4J用于通过其中一个包含的依赖项进行日志记录抽象。 Oracle SOA Suite 12c已在类路径中具有SLF4J绑定。 SLF4J API旨在仅与一个日志框架绑定。由于Oracle SOA Suite使用SLF4J绑定,因此我认为没有任何可能将SLF4j用于我自己的绑定。
因为我不需要SLF4J的灵活性,我从我的项目中删除了SLF4J实现,并使用了没有抽象层的log4j2。