Skip to content

Make log.Logger configurable at Session creation #635

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: 5.0
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 14 additions & 1 deletion neo4j/driver_with_context.go
Original file line number Diff line number Diff line change
Expand Up @@ -364,13 +364,17 @@ func (d *driverWithContext) NewSession(ctx context.Context, config SessionConfig
}
}

if config.Logger == nil {
config.Logger = d.log
}

d.mut.Lock()
defer d.mut.Unlock()
if d.pool == nil {
return &erroredSessionWithContext{
err: &UsageError{Message: "Trying to create session on closed driver"}}
}
return newSessionWithContext(ctx, d.config, config, d.router, d.pool, d.cache, d.log, reAuthToken)
return newSessionWithContext(ctx, d.config, config, d.router, d.pool, d.cache, reAuthToken)
}

func (d *driverWithContext) VerifyConnectivity(ctx context.Context) error {
Expand Down Expand Up @@ -672,6 +676,13 @@ func ExecuteQueryWithBoltLogger(boltLogger log.BoltLogger) ExecuteQueryConfigura
}
}

// ExecuteQueryWithLogger configures neo4j.ExecuteQuery to log messages with the provided Logger.
func ExecuteQueryWithLogger(logger log.Logger) ExecuteQueryConfigurationOption {
return func(configuration *ExecuteQueryConfiguration) {
configuration.Logger = logger
}
}

// ExecuteQueryWithTransactionConfig configures neo4j.ExecuteQuery with additional transaction configuration.
func ExecuteQueryWithTransactionConfig(configurers ...func(*TransactionConfig)) ExecuteQueryConfigurationOption {
return func(configuration *ExecuteQueryConfiguration) {
Expand All @@ -693,6 +704,7 @@ type ExecuteQueryConfiguration struct {
Database string
BookmarkManager BookmarkManager
BoltLogger log.BoltLogger
Logger log.Logger
TransactionConfigurers []func(*TransactionConfig)
Auth *AuthToken
}
Expand All @@ -713,6 +725,7 @@ func (c *ExecuteQueryConfiguration) toSessionConfig() SessionConfig {
DatabaseName: c.Database,
BookmarkManager: c.BookmarkManager,
BoltLogger: c.BoltLogger,
Logger: c.Logger,
Auth: c.Auth,
}
}
Expand Down
8 changes: 7 additions & 1 deletion neo4j/session_with_context.go
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,11 @@ type SessionConfig struct {
// Possible to use custom logger (implement log.BoltLogger interface) or
// use neo4j.ConsoleBoltLogger.
BoltLogger log.BoltLogger
// Logging target that the session will use to log messages.
//
// Possible to use a custom logger (implement log.Logger interface) or
// use neo4j.ConsoleLogger.
Logger log.Logger
// ImpersonatedUser sets the Neo4j user that the session will be acting as.
// If not set, the user configured for the driver will be used.
//
Expand Down Expand Up @@ -230,9 +235,10 @@ func newSessionWithContext(
router sessionRouter,
pool sessionPool,
cache *homedb.Cache,
logger log.Logger,
token *idb.ReAuthToken,
) *sessionWithContext {
logger := sessConfig.Logger

logId := log.NewId()
logger.Debugf(log.Session, logId, "Created")

Expand Down
7 changes: 4 additions & 3 deletions neo4j/session_with_context_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,8 +57,8 @@ func TestSession(outer *testing.T) {
router := RouterFake{}
pool := PoolFake{}
cache, _ := homedb.NewCache(100)
sessConfig := SessionConfig{AccessMode: AccessModeRead, BoltLogger: boltLogger}
sess := newSessionWithContext(ctx, &conf, sessConfig, &router, &pool, cache, logger, reAuthToken)
sessConfig := SessionConfig{AccessMode: AccessModeRead, BoltLogger: boltLogger, Logger: logger}
sess := newSessionWithContext(ctx, &conf, sessConfig, &router, &pool, cache, reAuthToken)
sess.throttleTime = time.Millisecond * 1
return &router, &pool, sess
}
Expand All @@ -69,7 +69,8 @@ func TestSession(outer *testing.T) {
router := RouterFake{}
pool := PoolFake{}
cache, _ := homedb.NewCache(100)
sess := newSessionWithContext(ctx, &conf, sessConfig, &router, &pool, cache, logger, reAuthToken)
sessConfig.Logger = logger
sess := newSessionWithContext(ctx, &conf, sessConfig, &router, &pool, cache, reAuthToken)
sess.throttleTime = time.Millisecond * 1
return &router, &pool, sess
}
Expand Down