我正在使用JBoss 7.1并希望使用自定义处理程序作为模块。问题是Handler没有添加到我的记录器中。
standalone.xml:
...
<subsystem xmlns="urn:jboss:domain:logging:1.1">
...
<custom-handler name="CUSTOM" class="my.company.logging.handler.MyHandler" module="my.company.logging.handler">
<level name="INFO"/>
<formatter>
<pattern-formatter pattern="%d{HH:mm:ss,SSS} %-5p [%c] (%t) %s%E%n"/>
</formatter>
</custom-handler>
...
<logger category="my.company">
<level name="INFO"/>
<handlers>
<handler name="CUSTOM"/>
</handlers>
</logger>
<root-logger>
<level name="INFO"/>
<handlers>
<handler name="CONSOLE"/>
<handler name="FILE"/>
</handlers>
</root-logger>
</subsystem>
...
使用org.jboss.logging.Logger的演示类: (快速入门中的示例类:“jboss-as-log4j”)
package my.company.logging.test;
@SessionScoped
@Named
public class LogDemo implements Serializable {
private final Logger logger;
private String text;
public LogDemo() {
logger = Logger.getLogger(getClass());
}
public String getText() {
return text;
}
public void setText(String text) {
this.text = text;
}
public void log() {
logger.info(this.getText());
this.setText(null);
}
}
这样就不会向我的处理程序发送任何日志。
我还尝试将Handler添加到RootLogger中,但它适用于所有Log但不适用于来自我自己的Logger的日志。
有人知道问题可能是什么吗?
电贺 马丁
编辑:
MyHandler的:
package my.company.logging.handler;
public class MyHandler extends Handler {
private MongoClient mongoClient;
private DB db;
public MyHandler() {
System.out.println("new MyHandler");
if (connect()) {
System.out.println("Mongo connected!");
} else {
System.out.println("Mongo not connected!");
}
}
private boolean connect() {
try {
this.mongoClient = new MongoClient();
this.db = mongoClient.getDB("TestLogging");
return true;
} catch (UnknownHostException e) {
return false;
}
}
@Override
public void close() throws SecurityException {
mongoClient.close();
}
@Override
public void flush() {
}
@Override
public void publish(LogRecord record) {
DBCollection collection = db.getCollection(record.getLoggerName());
final BasicDBObject logRecord = new BasicDBObject("message",
record.getMessage());
logRecord.put("level", record.getLevel().toString());
Object[] parameters = record.getParameters();
for (int i = 0; i < parameters.length; i++) {
logRecord.put("Parameter" + i, parameters[i].toString());
}
System.out.println("...");
String result = collection.insert(logRecord).getError();
if (result != null)
System.out.println("something went wrong: " + result);
else
System.out.println("ok");
}
}
注意: 当我将此处理程序添加到standalone.xml中的根记录器时,它适用于每个模块执行我自己的战争。
配置是否仅适用于JBoss中的模块,还是应该适用于war?