Neo4j registers server extension while running from JUnit test in IntelliJ IDEA

I am using community version Neo4j 3.2.1 and IntelliJ IDEA Ultimate 2017.1 with JUnit 4.12 and Java 8.

I am getting an object org.neo4j.logging.Log

in a procedure class using:

@Context
public Log log;

      

and then i use it in methods:

log.info("Info message...");

      

This works great when starting neo4j and calling the extension, but no logs are displayed when running from Intellij using the server instance created in the JUnit test.

My test code looks like this:

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)");

        }
    }

}

      

The log file seems to be sent to the directory where the server instance is running, which is created at runtime but then deleted immediately after.

Is it possible to specify the location of the log file and view the logs printed in the IntelliJ run / debug console?

+3


source to share


1 answer


I would have the same problem (Visual Studio Code / Maven / Neo4J CE 3.2.0) and solved with the following workaround:

Define the folder in which the neo4j server instance should be started and set as configuration items for the path rules for the logs and startup logs:

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")
  ...

      

Implement a method to back up log data on the current server instance (after each test method in the class):



@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();
          }
  });
});
}

      

This method localizes the neo4j instance folder (as a "serverPath" subfolder) and looks for the neo4j.log file. If such a file exists, it will be copied to the logs folder with a different file name.

In my opinion this should be a more elegant solution, but I haven't found one yet.

+2


source







All Articles