我正在使用 Neo4j 3.2.1 社区版和带有 JUnit 4.12 和 Java 8 的 IntelliJ IDEA Ultimate 2017.1。
我在过程类中获取 org.neo4j.logging.Log
对象使用:
@Context
public Log log;
然后在方法中使用它:
log.info("Info message...");
这在运行 neo4j 和调用扩展时工作得很好,但是当使用在 JUnit 测试中创建的服务器实例从 Intellij 内部运行时,日志不可见。
我的测试代码是这样的:
package graphEngine;
import GraphComponents.TestGraphQueries;
import org.junit.Rule;
import org.junit.Test;
import org.neo4j.driver.v1.Config;
import org.neo4j.driver.v1.Driver;
import org.neo4j.driver.v1.GraphDatabase;
import org.neo4j.driver.v1.Session;
import org.neo4j.harness.junit.Neo4jRule;
public class ComputeTest {
@Rule
public Neo4jRule neo4j = new Neo4jRule()
.withProcedure(Compute.class)
.withProcedure(TestGraphQueries.class);
@Test
public void shouldFindMatchingSystems() throws Throwable {
try(Driver driver = GraphDatabase.driver( neo4j.boltURI() , Config.build()
.withEncryptionLevel( Config.EncryptionLevel.NONE ).toConfig() );
Session session = driver.session() ) {
session.run("CALL graphEngine.loadGraph");
session.run("CALL graphEngine.compute(8)");
}
}
}
日志文件似乎转到运行服务器实例的目录,该目录在执行期间创建,但随后立即删除。
有没有一种方法可以设置日志文件的位置并查看打印到 IntelliJ 运行/调试控制台的日志?
最佳答案
我遇到了同样的问题(Visual Studio Code/Maven/Neo4J CE 3.2.0)并通过以下解决方法解决了它:
定义文件夹,因为必须运行 neo4j-server 实例并将日志和运行目录的路径设置为规则配置项:
public class TestClass {
...
private static final String basePath = "C:\\Development\\myworkspaces\\myproject\\run";
private static final String serverPath = basePath + "\\neo4j";
private static final String logsPath = basePath + "\\logs";
...
@Rule
public Neo4jRule neo4j = new Neo4jRule(
new File(
TestClass.serverPath
)
)
...
.withConfig("dbms.directories.logs", TestClass.logsPath)
.withConfig("dbms.directories.run", TestClass.basePath)
.withConfig("dbms.logs.debug.level", "DEBUG")
...
实现一个方法来备份当前服务器实例中的日志数据(在类中的每个测试方法之后):
@After
public void backupLogMessages() {
File serverFolder = new File(serverPath);
Arrays.stream(serverFolder.listFiles())
.filter(f -> f.isDirectory())
.forEach(d ->
{
Optional<File> logFile =
Arrays.stream(d.listFiles())
.filter(fnd -> !fnd.isDirectory())
.filter(ff -> ff.getName().indexOf("neo4j") != -1)
.findFirst();
logFile.ifPresent(lf ->
{
String parentFolderName
= lf.getParent()
.substring(lf.getParent().lastIndexOf("\\")+1);
Path logPath
= FileSystems
.getDefault()
.getPath(TestClass.logsPath);
String absolutePath
= logPath
.toAbsolutePath().toString();
try {
FileUtils.copyFile(
lf,
new File(absolutePath
+ "\\" + parentFolderName
+ "_neo4j.log")
);
} catch (IOException ioe) {
ioe.printStackTrace();
}
});
});
}
此方法本地化 neo4j 实例的文件夹(作为“serverPath”的子文件夹)并在其中查找 neo4j.log 文件。如果存在这样的文件,它将被复制到具有另一个文件名的日志文件夹中。
在我看来一定存在更优雅的解决方案,但我还没有找到。
关于java - Neo4j 在 IntelliJ IDEA 的 JUnit 测试中运行时登录服务器扩展,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45033699/