Neo4j event handling plugin + logging

76 views Asked by At

I am new to neo4j and I want to develop a plugin that registers changes in database to a log, for now i only want log transaction data. I am using maven with java 17 to build the jar and Docker to deploy neo4j with the latest image (5.10.0).

The plugin looks like this:

Class to handle logging events:

public class Neo4jTriggersPlugin implements TransactionEventListener<Object> {
    private final Log log;


    public Neo4jTriggersPlugin(DatabaseManagementService dbms, LogService lgsvc) {
        this.log = lgsvc.getUserLog(Neo4jTriggersPlugin.class);
    }

    @Override
    public Object beforeCommit(TransactionData transactionData, Transaction transaction, GraphDatabaseService graphDatabaseService) throws Exception {
        return null;
    }

    @Override
    public void afterCommit(TransactionData transactionData, Object o, GraphDatabaseService graphDatabaseService) {
        log.info("Transaction committed" + transactionData.toString());
    }

    @Override
    public void afterRollback(TransactionData transactionData, Object o, GraphDatabaseService graphDatabaseService) {
    }

public class LifecycleManagement extends ExtensionFactory<LifecycleManagement.Dependencies> {
    private Log logger;
    @Override
    public Lifecycle newInstance(ExtensionContext extensionContext, final Dependencies dependencies) {
        return new LifecycleAdapter() {
            LogService logService = dependencies.log();
            private Neo4jTriggersPlugin triggersPlugin;

            @Override
            public void start() {
                System.out.println("STARTING trigger watcher");
                triggersPlugin = new Neo4jTriggersPlugin(dependencies.getGraphDatabaseService(), logService);
                dependencies.getGraphDatabaseService().registerTransactionEventListener("neo4j", triggersPlugin);
            }

            @Override
            public void shutdown() {
                System.out.println("STOPPING trigger watcher");
                dependencies.getGraphDatabaseService().unregisterTransactionEventListener("neo4j",triggersPlugin);
            }
        };
    }

    interface Dependencies {
        DatabaseManagementService getGraphDatabaseService();
        LogService log();
    }

    public LifecycleManagement(Log log) {
        super(ExtensionType.DATABASE, "registerTransactionEventHandler");
        this.logger = null;
    }

I found example here. I went through neo4j documentation about logging, procedures, extensions etc.

After I compile and build the .jar of the above plugin, I copy it into var/lib/neo4j/plugins/ directory and restart the container. Then I run some cypher queries and I do not see any info written in a log. Also I have mounted the /logs and /data/database as volumes.

I would appreciate if someone could explain how this is done corretly or point me to a good source of information. Thanks.

2

There are 2 answers

0
cybersam On

As a workaround, your plugin can invoke the extended APOC apoc.log procedures (e.g., apoc.log.info) to log to neo4j.log on the server.

Also, instead of writing your own commit-triggered plugin, you may want to use APOC's trigger procedures instead.

0
Christophe Willemsen On