cancel
Showing results for 
Search instead for 
Did you mean: 

How to log to neo4j.log in an Unmanaged Extension

david_gordon
Node Clone

As part of the major changes in 3.0, the way to log to the user log, now neo4j.log (in server mode), has changed. To log within an Unmanaged Extension is quite straightforward:

Include this package: import org.neo4j.logging.Log;

In the method for the Unmanaged Extension, include @Context Log log as an argument:

@GET
@Path("/friendOfFriend/{userId}")
public Response getFofs(@PathParam("userId") String userId, @Context GraphDatabaseService db, @Context Log log) throws IOException {

Now log using the appropriate method for the log object:

       // Method variables

        try (Transaction tx = db.beginTx()) {
           	// Get the user node
            final Node user = db.findNode(Labels.Person, "userId");
            // Let's write that to neo4j.log!
            log.info("Found user node: " + user.toString());
            
            // Code to find fofs, and build result set formatted
        }

        // Return results, which are contained in method variable "results"
        return Response.ok().entity(objectMapper.writeValueAsString(results)).build();
        // Let's write to neo4j.log again!
        log.debug("We are all done!");
}

We get a log that contains lines like this:

2016-12-05 17:33:21.223+0000 INFO  Found user node: Node[63564]
2016-12-05 17:33:21.345+0000 DEBUG  We are all done!
4 REPLIES 4

Okay, got that rolling no problem, but what about writing to a different filename?
It looks like Configuration Settings allow for changing the path and filename of an existing logger, but what about rolling my own?

I'm assuming I'll have to implement my own Logger and PrintWriter for that, but I though I'd ask.

Hi Tony,

I don't know how off the top of my head, but I am asking around and will come back with what I find out.

Thanks, Dave

Hi Tony,

Advice from the experts is that you need to open a new log file. If you want to have a similar format as neo4j.log, look at https://neo4j.com/docs/java-reference/current/javadocs/org/neo4j/logging/FormattedLog.html which is the one we use for neo4j.log and debug.log

I hope that helps!

Thanks, Dave G

Thank you David, that is very helpful!

-Tony