有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java Neo4j在IntelliJ IDEA的JUnit测试中运行时在服务器扩展中进行日志记录

我正在使用Neo4j 3.2.1社区版和IntelliJ IDEA Ultimate 2017.1以及JUnit 4.12和Java 8

我使用以下方法获取过程类中的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运行/调试控制台的日志


共 (1) 个答案

  1. # 1 楼答案

    我发现了相同的问题(Visual Studio代码/Maven/Neo4J CE 3.2.0),并通过以下解决方法解决了它:

    定义文件夹,因为必须运行neo4j服务器实例,并将日志和运行目录的路径设置为规则配置项:

    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。日志文件。如果存在这样的文件,它将以另一个文件名复制到日志文件夹

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