neo4j / neo4j-go-driver

Neo4j Bolt Driver for Go
Apache License 2.0
485 stars 68 forks source link

Running a query with `ExecuteQuery` doesn't get logged #504

Closed stefano-ottolenghi closed 1 year ago

stefano-ottolenghi commented 1 year ago

MWE

package main

import (
    "context"
    "fmt"
    "github.com/neo4j/neo4j-go-driver/v5/neo4j"
)

func main() {
    ctx := context.Background()
    dbUri := "neo4j://localhost"
    dbUser := "neo4j"
    dbPassword := "verysecret"
    useConsoleLogger := func(level neo4j.LogLevel) func(config *neo4j.Config) {
        return func(config *neo4j.Config) {
            config.Log = neo4j.ConsoleLogger(level)
        }
    }
    driver, _ := neo4j.NewDriverWithContext(dbUri, neo4j.BasicAuth(dbUser, dbPassword, ""), useConsoleLogger(neo4j.DEBUG))
    defer driver.Close(ctx)
    driver.VerifyConnectivity(ctx)

    result, err := neo4j.ExecuteQuery(ctx, driver,
        "MERGE (p:Person {name: $name}) RETURN p",
        map[string]any{
            "name": "Alice",
        }, neo4j.EagerResultTransformer,
        neo4j.ExecuteQueryWithDatabase("neo4j"))
    if err != nil {
        panic(err)
    }
}

the output of which is

stefano@stefano-XPS:~/virtualenvs/go-neo4j$ go run hello.go 
2023-06-13 06:03:50.548   INFO  [pool 1] Created
2023-06-13 06:03:50.548   INFO  [router 1] Created {context: map[address:localhost:7687]}
2023-06-13 06:03:50.548   INFO  [driver 1] Created { target: localhost:7687 }
2023-06-13 06:03:50.548  DEBUG  [session 2] Created
2023-06-13 06:03:50.548   INFO  [router 1] Reading routing table from initial router: localhost:7687
2023-06-13 06:03:50.548  DEBUG  [pool 1] Trying to borrow connection from [localhost:7687]
2023-06-13 06:03:50.548   INFO  [pool 1] Connecting to localhost:7687
2023-06-13 06:03:50.553   INFO  [bolt5 bolt-24@localhost:7687] Connected
2023-06-13 06:03:50.553   INFO  [bolt5 bolt-24@localhost:7687] Retrieving routing table
2023-06-13 06:03:50.554  DEBUG  [pool 1] Returning connection to localhost:7687 {alive:true}
2023-06-13 06:03:50.554  DEBUG  [bolt5 bolt-24@localhost:7687] Resetting connection internal state
2023-06-13 06:03:50.554  DEBUG  [router 1] New routing table for 'neo4j', TTL 300
2023-06-13 06:03:50.554  DEBUG  [session 2] Resolved home database, uses db 'neo4j'
2023-06-13 06:03:50.554  DEBUG  [pool 1] Trying to borrow connection from [localhost:7687]
2023-06-13 06:03:50.555  DEBUG  [pool 1] Returning connection to localhost:7687 {alive:true}
2023-06-13 06:03:50.555  DEBUG  [bolt5 bolt-24@localhost:7687] Resetting connection internal state
2023-06-13 06:03:50.555  DEBUG  [router 1] Cleaning up
2023-06-13 06:03:50.555  DEBUG  [session 2] Closed
2023-06-13 06:03:50.555  DEBUG  [session 3] Created
2023-06-13 06:03:50.555  DEBUG  [session 3] connection acquisition timeout is 1m0s, resolved deadline is: 2023-06-13 06:04:50.55521489 +0200 CEST m=+60.006744701
2023-06-13 06:03:50.555  DEBUG  [pool 1] Trying to borrow connection from [localhost:7687]
2023-06-13 06:03:50.560  DEBUG  [pool 1] Returning connection to localhost:7687 {alive:true}
2023-06-13 06:03:50.560  DEBUG  [bolt5 bolt-24@localhost:7687] Resetting connection internal state
2023-06-13 06:03:50.560  DEBUG  [router 1] Cleaning up
2023-06-13 06:03:50.560  DEBUG  [session 3] Closed
2023-06-13 06:03:50.560   INFO  [bolt5 bolt-24@localhost:7687] Close
2023-06-13 06:03:50.560   INFO  [pool 1] Closed
2023-06-13 06:03:50.560   INFO  [driver 1] Closed

i.e. there is no logging about the query just run (although it does run, as result is non empty).

fbiville commented 1 year ago

For Bolt logs (inc. the query being run), you need neo4j.ConsoleBoltLogger():

result, err := neo4j.ExecuteQuery(ctx, driver,
        "MERGE (p:Person {name: $name}) RETURN p",
        map[string]any{
            "name": "Alice",
        }, neo4j.EagerResultTransformer,
        neo4j.ExecuteQueryWithDatabase("neo4j"),
        neo4j.ExecuteQueryWithBoltLogger(neo4j.ConsoleBoltLogger()))
    if err != nil {
        panic(err)
    }

Let me update this README section: https://github.com/neo4j/neo4j-go-driver#bolt-tracing

stefano-ottolenghi commented 1 year ago

Is there any reason why we don't want them mix? At least Python and Javascript have only one type of console logging, logging all.

fbiville commented 1 year ago

@stefano-ottolenghi Bolt logging is more verbose and mostly meant for troubleshooting. Enabling it on a per-session basis was a deliberate decision.