让事务事件处理程序对通过Embedded API进行的更改进行日志记录。
import org.neo4j.graphdb.Node;
import org.neo4j.graphdb.event.TransactionData;
import org.neo4j.graphdb.event.TransactionEventHandler;
import org.neo4j.kernel.impl.util.StringLogger;
import org.neo4j.server.logging.Logger;
import java.util.logging.Level;
public class WarehouseTransactionEventHandler implements TransactionEventHandler<String> {
private static Logger log = Logger
.getLogger(WarehouseTransactionEventHandler.class);
public void afterCommit(TransactionData data, String state) {
Iterable<Node> createdNodes = data.createdNodes();
for(Node n: createdNodes) {
log.info("AfterCommit ########: created node:" + n.getId());
log.log(Level.ALL,"AfterCommit ########: created node:" + n.getId());
}
}
public void afterRollback(TransactionData data, String state) {
// TODO Auto-generated method stub
}
public String beforeCommit(TransactionData data) throws Exception {
Iterable<Node> createdNodes = data.createdNodes();
for(Node n: createdNodes) {
log.info("BeforeCommit ######: created node:" + n.getId());
log.log(Level.ALL,"BeforeCommit ######: created node:" + n.getId());
}
return null;
}
}
然后我在我的代码中注册它来完成写作
GraphDatabaseService graphDb = new EmbeddedGraphDatabase(DB_PATH)
graphDb.registerTransactionEventHandler(new WarehouseTransactionEventHandler())
// do stuff
graphDb.shutdown();
因此,当我运行我的代码时,似乎正在运行
INFO: AfterCommit ########: created node:14
Jun 26, 2013 12:24:22 PM org.neo4j.server.logging.Logger log
INFO: AfterCommit ########: created node:15
Jun 26, 2013 12:24:22 PM org.neo4j.server.logging.Logger log
INFO: AfterCommit ########: created node:17
Jun 26, 2013 12:24:22 PM org.neo4j.server.logging.Logger log
INFO: AfterCommit ########: created node:16
Jun 26, 2013 12:24:22 PM org.neo4j.server.logging.Logger log
INFO: AfterCommit ########: created node:19
Jun 26, 2013 12:24:22 PM org.neo4j.server.logging.Logger log
INFO: AfterCommit ########: created node:18
Jun 26, 2013 12:24:22 PM org.neo4j.server.logging.Logger log
INFO: AfterCommit ########: created node:21
Jun 26, 2013 12:24:22 PM org.neo4j.server.logging.Logger log
INFO: AfterCommit ########: created node:20
Jun 26, 2013 12:24:22 PM org.neo4j.server.logging.Logger log
INFO: AfterCommit ########: created node:22
但是我已经浏览了日志,找不到任何这些AfterCommit消息。除了设置java.util.logging.FileHandler.limit=10000000
有什么想法?谢谢!
答案 0 :(得分:1)
您是否尝试使用log4j
,它可以和我一起使用。
在您应用的开头初始化logger
@,它会将您的消息记录在您希望的任何位置..就像这样:
static {
try {
String logFileName = "neo4j.log";
RollingFileAppender fa = new RollingFileAppender();
fa.setName("NEO4JLOGGER");
fa.setFile(logFileName);
fa.setLayout(new PatternLayout(
"[%d{dd.MM.yyyy HH:mm:ss.SSS}] %5p %c{1}:%L - %m%n"));
fa.setAppend(true);
fa.activateOptions();
Logger.getLogger("CLASSES NEEDED TO BE LOGGED").setLevel(Level.ALL);
Logger.getLogger("CLASSES NEEDED TO BE LOGGED").addAppender(fa);
} catch (Exception ex) {
logger.error("Intialize Logger :: Failed... \n ", ex);
}
}