首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在IntelliJ IDEA中从JUnit测试中运行时,在服务器扩展中进行Neo4j登录

在IntelliJ IDEA中从JUnit测试中运行时,在服务器扩展中进行Neo4j登录
EN

Stack Overflow用户
提问于 2017-07-11 19:47:09
回答 1查看 227关注 0票数 3

我使用的是Neo4j 3.2.1社区版和IntelliJ IDEA旗舰版2017.1以及JUnit 4.12和Java8。

我使用以下命令获取procedure类中的org.neo4j.logging.Log对象:

代码语言:javascript
复制
@Context
public Log log;

然后我在方法中使用它:

代码语言:javascript
复制
log.info("Info message...");

这在运行neo4j和调用扩展时工作得很好,但是当使用在JUnit测试中创建的服务器实例从Intellij内部运行时,日志是不可见的。

我的测试代码如下所示:

代码语言:javascript
复制
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运行/调试控制台?

EN

回答 1

Stack Overflow用户

发布于 2017-07-25 19:43:14

我遇到了同样的问题(Visual Studio Code / Maven / Neo4J CE 3.2.0),并使用以下变通方法解决了它:

定义文件夹,因为必须运行ne4j-server实例,并将log-和run -directories的路径设置为规则配置项:

代码语言:javascript
复制
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")
  ...

实现在当前服务器实例中备份日志数据的方法(在类中的每个测试方法之后):

代码语言:javascript
复制
@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文件。如果存在这样的文件,它将被复制到具有另一个文件名的logs文件夹中。

在我看来,它肯定存在一个更优雅的解决方案,但我还没有找到它。

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/45033699

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档