8.2.1. Query logging

This chapter describes Neo4j support for query logging.

Neo4j can be configured to log queries executed in the database.

Query logging must be enabled by setting the dbms.logs.query.enabled parameter to true. The parameter dbms.logs.query.threshold determines the threshold for logging a query, i.e. if the execution of a query takes a longer time than this threshold, it will be logged. Setting dbms.logs.query.threshold to 0 will result in all queries being logged.

8.2.1.1. Log configuration

The name of the log file is query.log and it resides in the logs directory (see Section 3.1, “File locations”).

Rotation of the query log can be configured in the neo4j.conf configuration file. The following parameters are available:

Parameter name Default value Description

dbms.logs.query.enabled

false

Log executed queries that take longer than the configured threshold, dbms.logs.query.threshold.

dbms.logs.query.parameter_logging_enabled

true

Log parameters for executed queries that take longer than the configured threshold.

dbms.logs.query.rotation.keep_number

7

Sets number of historical log files kept.

dbms.logs.query.rotation.size

20M

Sets the file size at which the query log will auto-rotate.

dbms.logs.query.threshold

0

If the execution of query takes a longer time than this threshold, the query is logged (provided query logging is enabled).

Below is an example of the query log:

2016-10-27 14:31 ... INFO  0 ms: bolt-session	bolt	johndoe	neo4j-javascript/0.0.0-dev		client/127.0.0.1:59167	...
2016-10-27 14:31 ... INFO  9 ms: bolt-session	bolt	johndoe	neo4j-javascript/0.0.0-dev		client/127.0.0.1:59167	...
2016-10-27 14:31 ... INFO  0 ms: bolt-session	bolt	johndoe	neo4j-javascript/0.0.0-dev		client/127.0.0.1:59167	...
2016-10-27 14:32 ... INFO  3 ms: server-session	http	127.0.0.1	/db/data/cypher	neo4j - CALL dbms.procedures() - {}
2016-10-27 14:32 ... INFO  1 ms: server-session	http	127.0.0.1	/db/data/cypher	neo4j - CALL dbms.showCurrentUs...
2016-10-27 14:32 ... INFO  0 ms: bolt-session	bolt	johndoe	neo4j-javascript/0.0.0-dev		client/127.0.0.1:59167	...
2016-10-27 14:32 ... INFO  0 ms: bolt-session	bolt	johndoe	neo4j-javascript/0.0.0-dev		client/127.0.0.1:59167	...
2016-10-27 14:32 ... INFO  2 ms: bolt-session	bolt	johndoe	neo4j-javascript/0.0.0-dev		client/127.0.0.1:59261	...

8.2.1.2. Attach metadata to a query

It is possible to attach metadata to a query and have it printed in the query log, using the built-in procedure dbms.setTXMetaData. This is typically done programmatically, but can be illustrated as follows, using cypher-shell.

Start a transaction and call dbms.setTXMetaData with a list of meta data.

neo4j> :begin
neo4j# CALL dbms.setTXMetaData({ User: 'jsmith', AppServer: 'app03.dc01.company.com'});
neo4j# CALL dbms.procedures() YIELD name RETURN COUNT(name);
COUNT(name)
39
neo4j# :commit

Below are the corresponding results in the query log:

... CALL dbms.setTXMetaData({ User: 'jsmith', AppServer: 'app03.dc01.company.com'}); - {} - {}
... CALL dbms.procedures() YIELD name RETURN COUNT(name); - {} - {User: 'jsmith', AppServer: 'app03.dc01.company.com'}