12.2.1. Query logging

This section describes Neo4j support for query logging.

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

Query logging is enabled by default and is controlled by the setting dbms.logs.query.enabled.

Configuration options are:

Option Default Description

off

 

Will completely disable logging.

info

 

Will log at the end of queries that have either succeeded or failed. The dbms.logs.query.threshold parameter is used to determine the threshold for logging a query. If the execution of a query takes a longer time than this threshold, it will be logged. Setting the threshold to 0 will result in all queries being logged.

verbose

Yes

Will log all queries at both start and finish, regardless of dbms.logs.query.threshold.

12.2.1.1. Log configuration

The name of the log file is query.log and it resides in the logs directory (see Section 4.2, “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.allocation_logging_enabled

false

Log allocated bytes for the executed queries being logged.

dbms.logs.query.enabled

verbose

Log executed queries.

dbms.logs.query.page_logging_enabled

false

Log page hits and page faults for the executed queries being logged.

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 set to info).

dbms.logs.query.time_logging_enabled

false

Log detailed time information for the executed queries being logged.

Example 12.1. Configure for simple query logging

In this example we set query logging to info, but leave all other query log parameters at their defaults.

dbms.logs.query.enabled=info

Below is an example of the query log with this basic configuration:

2017-11-22 14:31 ... INFO  9 ms: bolt-session	bolt	johndoe	neo4j-javascript/1.4.1		client/127.0.0.1:59167	...
2017-11-22 14:31 ... INFO  0 ms: bolt-session	bolt	johndoe	neo4j-javascript/1.4.1		client/127.0.0.1:59167	...
2017-11-22 14:32 ... INFO  3 ms: server-session	http	127.0.0.1	/db/data/cypher	neo4j - CALL dbms.procedures() - {}
2017-11-22 14:32 ... INFO  1 ms: server-session	http	127.0.0.1	/db/data/cypher	neo4j - CALL dbms.showCurrentUs...
2017-11-22 14:32 ... INFO  0 ms: bolt-session	bolt	johndoe	neo4j-javascript/1.4.1		client/127.0.0.1:59167	...
2017-11-22 14:32 ... INFO  0 ms: bolt-session	bolt	johndoe	neo4j-javascript/1.4.1		client/127.0.0.1:59167	...
2017-11-22 14:32 ... INFO  2 ms: bolt-session	bolt	johndoe	neo4j-javascript/1.4.1		client/127.0.0.1:59261	...
Example 12.2. Configure for query logging with more details

In this example we turn query logging on, and also enable some additional logging.

dbms.logs.query.parameter_logging_enabled=true
dbms.logs.query.time_logging_enabled=true
dbms.logs.query.allocation_logging_enabled=true
dbms.logs.query.page_logging_enabled=true

Below is an example of the query log with these configuration parameters enabled:

2017-11-22 12:38 ... INFO  3 ms: bolt-session	bolt	johndoe	neo4j-javascript/1.4.1                         ...
2017-11-22 22:38 ... INFO  61 ms: (planning: 0, cpu: 58, waiting: 0) - 6164496 B - 0 page hits, 1 page faults  ...
2017-11-22 12:38 ... INFO  78 ms: (planning: 40, cpu: 74, waiting: 0) - 6347592 B - 0 page hits, 0 page faults ...
2017-11-22 12:38 ... INFO  44 ms: (planning: 9, cpu: 25, waiting: 0) - 1311384 B - 0 page hits, 0 page faults  ...
2017-11-22 12:38 ... INFO  6 ms: (planning: 2, cpu: 6, waiting: 0) - 420872 B - 0 page hits, 0 page faults -   ...

12.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 tx.setMetaData. This is typically done programmatically, but can be illustrated as follows, using cypher-shell.

Example 12.3. Attach metadata to a query

Start a transaction and call tx.setMetaData with a list of meta data.

neo4j> :begin
neo4j# CALL tx.setMetaData({ 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 tx.setMetaData({ User: 'jsmith', AppServer: 'app03.dc01.company.com'}); - {} - {}
... CALL dbms.procedures() YIELD name RETURN COUNT(name); - {} - {User: 'jsmith', AppServer: 'app03.dc01.company.com'}