cancel
Showing results for 
Search instead for 
Did you mean: 

Join the community at Nodes 2022, our free virtual event on November 16 - 17.

Logging queries executed in java driver

murali_venugopa
Node Link

Hi,
Im using neo4j java driver version 4.3.3. im not sure how to print the query being executed with values. Let consider the below usecase.

the query is as follows,

MATCH (p:Person {name : "John"}) RETURN p

the java code is as follows,

String query = "MATCH (p:Person {name : $name}) RETURN";
try (Session session = this.driver.session()) {
   try (Transaction tx = session.beginTransaction()) {
      tx.run (query,  parameters(name , "John"));
      tx.commit();
   }

}

now printing the String variable query don't have values. i tried if i can interpolate the parameters with the string.
still not sure how to achieve that. please help me resolve this issue. because logging the queries being executed is crucial to the application.

Regards,
Murali V

2 REPLIES 2

murali_venugopa
Node Link

any inputs? i still cannot solve this problem. 

Hi @murali_venugopa ,

Even down to the query.log into the DB, you will see the parametrized query plus parameters. Up in your application layer, you can create you own interpolation of query and parameters in order to log so. Something like:

 

 

public void logQuery(String query, Map<String, Object params){
   for(Map.Entry entry : params.asMap().entrySet())
	query = query.replace('$'+entry.getKey().toString(),entry.getValue().toString());
				
    logger.debug(query);
}


public void run(String... args) {
   String query = "MATCH (p:Person {name : $name}) RETURN p";
   try (Session session = this.driver.session()) {
      try (Transaction tx = session.beginTransaction()) {
         Value params = parameters("name" , "John");
	 tx.run (query,  params);
         tx.commit();
	 logQuery(query, params)
      }
   }
}

 

 

Oh, y’all wanted a twist, ey?