From b9cb96f9f30195c51fc3cc0d3f7b782e6ed7a543 Mon Sep 17 00:00:00 2001 From: Jeff Yemin Date: Fri, 27 Jan 2023 14:22:12 -0500 Subject: [PATCH 1/3] Support configuration of max document length for command logging JAVA-4841 --- .../src/main/com/mongodb/LoggerSettings.java | 143 ++++++++++++++++++ .../main/com/mongodb/MongoClientSettings.java | 36 ++++- .../connection/DefaultClusterFactory.java | 10 +- .../DefaultClusterableServerFactory.java | 11 +- .../connection/InternalStreamConnection.java | 12 +- .../InternalStreamConnectionFactory.java | 23 +-- .../LoadBalancedClusterableServerFactory.java | 6 +- .../connection/LoggingCommandEventSender.java | 12 +- .../com/mongodb/ClusterFixture.java | 4 +- .../CommandHelperSpecification.groovy | 3 +- .../connection/PlainAuthenticatorTest.java | 3 +- .../ServerMonitorSpecification.groovy | 3 +- .../connection/SingleServerClusterTest.java | 3 +- .../MongoClientSettingsSpecification.groovy | 14 +- .../AbstractConnectionPoolTest.java | 2 + ...gingCommandEventSenderSpecification.groovy | 9 +- .../reactivestreams/client/MongoClients.java | 2 +- .../org/mongodb/scala/LoggerSettings.scala | 40 +++++ .../client/internal/MongoClientImpl.java | 4 +- .../com/mongodb/client/unified/Entities.java | 1 + 20 files changed, 294 insertions(+), 47 deletions(-) create mode 100644 driver-core/src/main/com/mongodb/LoggerSettings.java create mode 100644 driver-scala/src/main/scala/org/mongodb/scala/LoggerSettings.scala diff --git a/driver-core/src/main/com/mongodb/LoggerSettings.java b/driver-core/src/main/com/mongodb/LoggerSettings.java new file mode 100644 index 00000000000..fa1d78f9f64 --- /dev/null +++ b/driver-core/src/main/com/mongodb/LoggerSettings.java @@ -0,0 +1,143 @@ +/* + * Copyright 2008-present MongoDB, Inc. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.mongodb; + +import com.mongodb.annotations.Immutable; + +import java.util.Objects; + +import static com.mongodb.assertions.Assertions.notNull; + +/** + * An immutable class representing settings for logging. + * + *

+ * The driver logs using SLF4J 1.0 API with a root logger of {@code org.mongodb.driver}. + *

+ * + * @since 4.9 + */ +@Immutable +public final class LoggerSettings { + private final int maxCommandLoggingDocumentLength; + /** + * Gets a builder for an instance of {@code LoggerSettings}. + * @return the builder + */ + public static Builder builder() { + return new Builder(); + } + + /** + * Creates a builder instance. + * + * @param loggerSettings existing LoggerSettings to default the builder settings on. + * @return a builder + */ + public static Builder builder(final LoggerSettings loggerSettings) { + return builder().applySettings(loggerSettings); + } + + /** + * A builder for an instance of {@code LoggerSettings}. + */ + public static final class Builder { + private int maxCommandLoggingDocumentLength = 1000; + private Builder() { + } + + /** + * Applies the loggerSettings to the builder + * + *

Note: Overwrites all existing settings

+ * + * @param loggerSettings the loggerSettings + * @return this + */ + public Builder applySettings(final LoggerSettings loggerSettings) { + notNull("loggerSettings", loggerSettings); + maxCommandLoggingDocumentLength = loggerSettings.maxCommandLoggingDocumentLength; + return this; + } + + /** + * Sets the max command logging document length. + * + * @param maxCommandLoggingDocumentLength the max command logging document length + * @return this + */ + public Builder maxCommandLoggingDocumentLength(final int maxCommandLoggingDocumentLength) { + this.maxCommandLoggingDocumentLength = maxCommandLoggingDocumentLength; + return this; + } + + /** + * Build an instance of {@code LoggerSettings}. + * @return the logger settings for this builder + */ + public LoggerSettings build() { + return new LoggerSettings(this); + } + } + + /** + * Gets the max command logging document length. + * + *

+ * When the driver logs a command and its reply via the {@code org.mongodb.driver.protocol.command} SFL4J logger, it truncates the + * them to the maximum length defined by this setting. + *

+ * + *

+ * Defaults to 1000 characters. + *

+ * + * @return the max command logging document length + */ + public int getMaxCommandLoggingDocumentLength() { + return maxCommandLoggingDocumentLength; + } + + + @Override + public boolean equals(final Object o) { + if (this == o) { + return true; + } + if (o == null || getClass() != o.getClass()) { + return false; + } + LoggerSettings that = (LoggerSettings) o; + return maxCommandLoggingDocumentLength == that.maxCommandLoggingDocumentLength; + } + + @Override + public int hashCode() { + return Objects.hash(maxCommandLoggingDocumentLength); + } + + @Override + public String toString() { + return "LoggerSettings{" + + "maxCommandLoggingDocumentLength=" + maxCommandLoggingDocumentLength + + '}'; + } + + private LoggerSettings(final Builder builder) { + maxCommandLoggingDocumentLength = builder.maxCommandLoggingDocumentLength; + } +} diff --git a/driver-core/src/main/com/mongodb/MongoClientSettings.java b/driver-core/src/main/com/mongodb/MongoClientSettings.java index f81b6a75b1f..718ea26def0 100644 --- a/driver-core/src/main/com/mongodb/MongoClientSettings.java +++ b/driver-core/src/main/com/mongodb/MongoClientSettings.java @@ -87,7 +87,7 @@ public final class MongoClientSettings { private final StreamFactoryFactory streamFactoryFactory; private final List commandListeners; private final CodecRegistry codecRegistry; - + private final LoggerSettings loggerSettings; private final ClusterSettings clusterSettings; private final SocketSettings socketSettings; private final SocketSettings heartbeatSocketSettings; @@ -170,6 +170,7 @@ public static final class Builder { private StreamFactoryFactory streamFactoryFactory; private List commandListeners = new ArrayList<>(); + private final LoggerSettings.Builder loggerSettingsBuilder = LoggerSettings.builder(); private final ClusterSettings.Builder clusterSettingsBuilder = ClusterSettings.builder(); private final SocketSettings.Builder socketSettingsBuilder = SocketSettings.builder(); private final ConnectionPoolSettings.Builder connectionPoolSettingsBuilder = ConnectionPoolSettings.builder(); @@ -208,6 +209,7 @@ private Builder(final MongoClientSettings settings) { streamFactoryFactory = settings.getStreamFactoryFactory(); autoEncryptionSettings = settings.getAutoEncryptionSettings(); contextProvider = settings.getContextProvider(); + loggerSettingsBuilder.applySettings(settings.getLoggerSettings()); clusterSettingsBuilder.applySettings(settings.getClusterSettings()); serverSettingsBuilder.applySettings(settings.getServerSettings()); socketSettingsBuilder.applySettings(settings.getSocketSettings()); @@ -263,6 +265,19 @@ public Builder applyConnectionString(final ConnectionString connectionString) { return this; } + /** + * Applies the {@link LoggerSettings.Builder} block and then sets the loggerSettings. + * + * @param block the block to apply to the LoggerSettins. + * @return this + * @see MongoClientSettings#getLoggerSettings() + * @since 4.9 + */ + public Builder applyToLoggerSettings(final Block block) { + notNull("block", block).apply(loggerSettingsBuilder); + return this; + } + /** * Applies the {@link ClusterSettings.Builder} block and then sets the clusterSettings. * @@ -769,6 +784,16 @@ public AutoEncryptionSettings getAutoEncryptionSettings() { return autoEncryptionSettings; } + /** + * Gets the logger settings. + * + * @return the logger settings + * @since 4.9 + */ + public LoggerSettings getLoggerSettings() { + return loggerSettings; + } + /** * Gets the cluster settings. * @@ -863,6 +888,7 @@ public boolean equals(final Object o) { && Objects.equals(streamFactoryFactory, that.streamFactoryFactory) && Objects.equals(commandListeners, that.commandListeners) && Objects.equals(codecRegistry, that.codecRegistry) + && Objects.equals(loggerSettings, that.loggerSettings) && Objects.equals(clusterSettings, that.clusterSettings) && Objects.equals(socketSettings, that.socketSettings) && Objects.equals(heartbeatSocketSettings, that.heartbeatSocketSettings) @@ -880,9 +906,9 @@ public boolean equals(final Object o) { @Override public int hashCode() { return Objects.hash(readPreference, writeConcern, retryWrites, retryReads, readConcern, credential, streamFactoryFactory, - commandListeners, codecRegistry, clusterSettings, socketSettings, heartbeatSocketSettings, connectionPoolSettings, - serverSettings, sslSettings, applicationName, compressorList, uuidRepresentation, serverApi, autoEncryptionSettings, - heartbeatSocketTimeoutSetExplicitly, heartbeatConnectTimeoutSetExplicitly, contextProvider); + commandListeners, codecRegistry, loggerSettings, clusterSettings, socketSettings, heartbeatSocketSettings, + connectionPoolSettings, serverSettings, sslSettings, applicationName, compressorList, uuidRepresentation, serverApi, + autoEncryptionSettings, heartbeatSocketTimeoutSetExplicitly, heartbeatConnectTimeoutSetExplicitly, contextProvider); } @Override @@ -897,6 +923,7 @@ public String toString() { + ", streamFactoryFactory=" + streamFactoryFactory + ", commandListeners=" + commandListeners + ", codecRegistry=" + codecRegistry + + ", loggerSettings=" + loggerSettings + ", clusterSettings=" + clusterSettings + ", socketSettings=" + socketSettings + ", heartbeatSocketSettings=" + heartbeatSocketSettings @@ -923,6 +950,7 @@ private MongoClientSettings(final Builder builder) { codecRegistry = builder.codecRegistry; commandListeners = builder.commandListeners; applicationName = builder.applicationName; + loggerSettings = builder.loggerSettingsBuilder.build(); clusterSettings = builder.clusterSettingsBuilder.build(); serverSettings = builder.serverSettingsBuilder.build(); socketSettings = builder.socketSettingsBuilder.build(); diff --git a/driver-core/src/main/com/mongodb/internal/connection/DefaultClusterFactory.java b/driver-core/src/main/com/mongodb/internal/connection/DefaultClusterFactory.java index e2aa34bb570..26c19ee7ec7 100644 --- a/driver-core/src/main/com/mongodb/internal/connection/DefaultClusterFactory.java +++ b/driver-core/src/main/com/mongodb/internal/connection/DefaultClusterFactory.java @@ -16,6 +16,7 @@ package com.mongodb.internal.connection; +import com.mongodb.LoggerSettings; import com.mongodb.MongoCompressor; import com.mongodb.MongoCredential; import com.mongodb.MongoDriverInformation; @@ -54,6 +55,7 @@ public Cluster createCluster(final ClusterSettings originalClusterSettings, fina final InternalConnectionPoolSettings internalConnectionPoolSettings, final StreamFactory streamFactory, final StreamFactory heartbeatStreamFactory, @Nullable final MongoCredential credential, + final LoggerSettings loggerSettings, @Nullable final CommandListener commandListener, @Nullable final String applicationName, @Nullable final MongoDriverInformation mongoDriverInformation, @@ -89,14 +91,14 @@ public Cluster createCluster(final ClusterSettings originalClusterSettings, fina if (clusterSettings.getMode() == ClusterConnectionMode.LOAD_BALANCED) { ClusterableServerFactory serverFactory = new LoadBalancedClusterableServerFactory(serverSettings, - connectionPoolSettings, internalConnectionPoolSettings, streamFactory, credential, commandListener, applicationName, - mongoDriverInformation != null ? mongoDriverInformation : MongoDriverInformation.builder().build(), compressorList, - serverApi); + connectionPoolSettings, internalConnectionPoolSettings, streamFactory, credential, loggerSettings, commandListener, + applicationName, mongoDriverInformation != null ? mongoDriverInformation : MongoDriverInformation.builder().build(), + compressorList, serverApi); return new LoadBalancedCluster(clusterId, clusterSettings, serverFactory, dnsSrvRecordMonitorFactory); } else { ClusterableServerFactory serverFactory = new DefaultClusterableServerFactory(serverSettings, connectionPoolSettings, internalConnectionPoolSettings, - streamFactory, heartbeatStreamFactory, credential, commandListener, applicationName, + streamFactory, heartbeatStreamFactory, credential, loggerSettings, commandListener, applicationName, mongoDriverInformation != null ? mongoDriverInformation : MongoDriverInformation.builder().build(), compressorList, serverApi); diff --git a/driver-core/src/main/com/mongodb/internal/connection/DefaultClusterableServerFactory.java b/driver-core/src/main/com/mongodb/internal/connection/DefaultClusterableServerFactory.java index 895f6373245..2c6d349c58f 100644 --- a/driver-core/src/main/com/mongodb/internal/connection/DefaultClusterableServerFactory.java +++ b/driver-core/src/main/com/mongodb/internal/connection/DefaultClusterableServerFactory.java @@ -16,6 +16,7 @@ package com.mongodb.internal.connection; +import com.mongodb.LoggerSettings; import com.mongodb.MongoCompressor; import com.mongodb.MongoCredential; import com.mongodb.MongoDriverInformation; @@ -46,6 +47,7 @@ public class DefaultClusterableServerFactory implements ClusterableServerFactory private final StreamFactory streamFactory; private final MongoCredentialWithCache credential; private final StreamFactory heartbeatStreamFactory; + private final LoggerSettings loggerSettings; private final CommandListener commandListener; private final String applicationName; private final MongoDriverInformation mongoDriverInformation; @@ -57,7 +59,9 @@ public DefaultClusterableServerFactory( final ServerSettings serverSettings, final ConnectionPoolSettings connectionPoolSettings, final InternalConnectionPoolSettings internalConnectionPoolSettings, final StreamFactory streamFactory, final StreamFactory heartbeatStreamFactory, - @Nullable final MongoCredential credential, @Nullable final CommandListener commandListener, + @Nullable final MongoCredential credential, + final LoggerSettings loggerSettings, + @Nullable final CommandListener commandListener, @Nullable final String applicationName, @Nullable final MongoDriverInformation mongoDriverInformation, final List compressorList, @Nullable final ServerApi serverApi) { this.serverSettings = serverSettings; @@ -66,6 +70,7 @@ public DefaultClusterableServerFactory( this.streamFactory = streamFactory; this.credential = credential == null ? null : new MongoCredentialWithCache(credential); this.heartbeatStreamFactory = heartbeatStreamFactory; + this.loggerSettings = loggerSettings; this.commandListener = commandListener; this.applicationName = applicationName; this.mongoDriverInformation = mongoDriverInformation; @@ -81,11 +86,11 @@ public ClusterableServer create(final Cluster cluster, final ServerAddress serve ServerMonitor serverMonitor = new DefaultServerMonitor(serverId, serverSettings, cluster.getClock(), // no credentials, compressor list, or command listener for the server monitor factory new InternalStreamConnectionFactory(clusterMode, true, heartbeatStreamFactory, null, applicationName, - mongoDriverInformation, emptyList(), null, serverApi), + mongoDriverInformation, emptyList(), loggerSettings, null, serverApi), clusterMode, serverApi, sdamProvider); ConnectionPool connectionPool = new DefaultConnectionPool(serverId, new InternalStreamConnectionFactory(clusterMode, streamFactory, credential, applicationName, - mongoDriverInformation, compressorList, commandListener, serverApi), + mongoDriverInformation, compressorList, loggerSettings, commandListener, serverApi), connectionPoolSettings, internalConnectionPoolSettings, sdamProvider); ServerListener serverListener = singleServerListener(serverSettings); SdamServerDescriptionManager sdam = new DefaultSdamServerDescriptionManager(cluster, serverId, serverListener, serverMonitor, diff --git a/driver-core/src/main/com/mongodb/internal/connection/InternalStreamConnection.java b/driver-core/src/main/com/mongodb/internal/connection/InternalStreamConnection.java index 8548d1465a0..862af0a9e31 100644 --- a/driver-core/src/main/com/mongodb/internal/connection/InternalStreamConnection.java +++ b/driver-core/src/main/com/mongodb/internal/connection/InternalStreamConnection.java @@ -16,6 +16,7 @@ package com.mongodb.internal.connection; +import com.mongodb.LoggerSettings; import com.mongodb.MongoClientException; import com.mongodb.MongoCompressor; import com.mongodb.MongoException; @@ -40,6 +41,7 @@ import com.mongodb.connection.Stream; import com.mongodb.connection.StreamFactory; import com.mongodb.event.CommandListener; +import com.mongodb.internal.VisibleForTesting; import com.mongodb.internal.async.SingleResultCallback; import com.mongodb.internal.diagnostics.logging.Logger; import com.mongodb.internal.diagnostics.logging.Loggers; @@ -125,6 +127,7 @@ public class InternalStreamConnection implements InternalConnection { private final AtomicBoolean opened = new AtomicBoolean(); private final List compressorList; + private final LoggerSettings loggerSettings; private final CommandListener commandListener; @Nullable private volatile Compressor sendCompressor; private final Map compressorMap; @@ -142,18 +145,20 @@ static Set getSecuritySensitiveHelloCommands() { return Collections.unmodifiableSet(SECURITY_SENSITIVE_HELLO_COMMANDS); } + @VisibleForTesting(otherwise = VisibleForTesting.AccessModifier.PRIVATE) public InternalStreamConnection(final ClusterConnectionMode clusterConnectionMode, final ServerId serverId, final ConnectionGenerationSupplier connectionGenerationSupplier, final StreamFactory streamFactory, final List compressorList, final CommandListener commandListener, final InternalConnectionInitializer connectionInitializer) { - this(clusterConnectionMode, false, serverId, connectionGenerationSupplier, streamFactory, compressorList, commandListener, - connectionInitializer); + this(clusterConnectionMode, false, serverId, connectionGenerationSupplier, streamFactory, compressorList, + LoggerSettings.builder().build(), commandListener, connectionInitializer); } public InternalStreamConnection(final ClusterConnectionMode clusterConnectionMode, final boolean isMonitoringConnection, final ServerId serverId, final ConnectionGenerationSupplier connectionGenerationSupplier, final StreamFactory streamFactory, final List compressorList, + final LoggerSettings loggerSettings, final CommandListener commandListener, final InternalConnectionInitializer connectionInitializer) { this.clusterConnectionMode = clusterConnectionMode; this.isMonitoringConnection = isMonitoringConnection; @@ -162,6 +167,7 @@ public InternalStreamConnection(final ClusterConnectionMode clusterConnectionMod this.streamFactory = notNull("streamFactory", streamFactory); this.compressorList = notNull("compressorList", compressorList); this.compressorMap = createCompressorMap(compressorList); + this.loggerSettings = loggerSettings; this.commandListener = commandListener; this.connectionInitializer = notNull("connectionInitializer", connectionInitializer); description = new ConnectionDescription(serverId); @@ -854,7 +860,7 @@ private CommandEventSender createCommandEventSender(final CommandMessage message final RequestContext requestContext) { if (!isMonitoringConnection && opened() && (commandListener != null || COMMAND_PROTOCOL_LOGGER.isRequired(DEBUG, getClusterId()))) { return new LoggingCommandEventSender(SECURITY_SENSITIVE_COMMANDS, SECURITY_SENSITIVE_HELLO_COMMANDS, description, - commandListener, requestContext, message, bsonOutput, COMMAND_PROTOCOL_LOGGER); + commandListener, requestContext, message, bsonOutput, COMMAND_PROTOCOL_LOGGER, loggerSettings); } else { return new NoOpCommandEventSender(); } diff --git a/driver-core/src/main/com/mongodb/internal/connection/InternalStreamConnectionFactory.java b/driver-core/src/main/com/mongodb/internal/connection/InternalStreamConnectionFactory.java index c60ba50a1cd..14312edef17 100644 --- a/driver-core/src/main/com/mongodb/internal/connection/InternalStreamConnectionFactory.java +++ b/driver-core/src/main/com/mongodb/internal/connection/InternalStreamConnectionFactory.java @@ -16,6 +16,7 @@ package com.mongodb.internal.connection; +import com.mongodb.LoggerSettings; import com.mongodb.MongoCompressor; import com.mongodb.MongoDriverInformation; import com.mongodb.ServerApi; @@ -38,6 +39,7 @@ class InternalStreamConnectionFactory implements InternalConnectionFactory { private final StreamFactory streamFactory; private final BsonDocument clientMetadataDocument; private final List compressorList; + private LoggerSettings loggerSettings; private final CommandListener commandListener; @Nullable private final ServerApi serverApi; @@ -48,20 +50,22 @@ class InternalStreamConnectionFactory implements InternalConnectionFactory { @Nullable final MongoCredentialWithCache credential, @Nullable final String applicationName, final MongoDriverInformation mongoDriverInformation, final List compressorList, - @Nullable final CommandListener commandListener, @Nullable final ServerApi serverApi) { + final LoggerSettings loggerSettings, @Nullable final CommandListener commandListener, @Nullable final ServerApi serverApi) { this(clusterConnectionMode, false, streamFactory, credential, applicationName, mongoDriverInformation, compressorList, - commandListener, serverApi); + loggerSettings, commandListener, serverApi); } + InternalStreamConnectionFactory(final ClusterConnectionMode clusterConnectionMode, final boolean isMonitoringConnection, - final StreamFactory streamFactory, - @Nullable final MongoCredentialWithCache credential, - @Nullable final String applicationName, final MongoDriverInformation mongoDriverInformation, - final List compressorList, - @Nullable final CommandListener commandListener, @Nullable final ServerApi serverApi) { + final StreamFactory streamFactory, + @Nullable final MongoCredentialWithCache credential, + @Nullable final String applicationName, final MongoDriverInformation mongoDriverInformation, + final List compressorList, + final LoggerSettings loggerSettings, @Nullable final CommandListener commandListener, @Nullable final ServerApi serverApi) { this.clusterConnectionMode = clusterConnectionMode; this.isMonitoringConnection = isMonitoringConnection; this.streamFactory = notNull("streamFactory", streamFactory); this.compressorList = notNull("compressorList", compressorList); + this.loggerSettings = loggerSettings; this.commandListener = commandListener; this.serverApi = serverApi; this.clientMetadataDocument = createClientMetadataDocument(applicationName, mongoDriverInformation); @@ -72,8 +76,9 @@ class InternalStreamConnectionFactory implements InternalConnectionFactory { public InternalConnection create(final ServerId serverId, final ConnectionGenerationSupplier connectionGenerationSupplier) { Authenticator authenticator = credential == null ? null : createAuthenticator(credential); return new InternalStreamConnection(clusterConnectionMode, isMonitoringConnection, serverId, connectionGenerationSupplier, - streamFactory, compressorList, commandListener, new InternalStreamConnectionInitializer(clusterConnectionMode, - authenticator, clientMetadataDocument, compressorList, serverApi)); + streamFactory, compressorList, loggerSettings, commandListener, + new InternalStreamConnectionInitializer(clusterConnectionMode, authenticator, clientMetadataDocument, compressorList, + serverApi)); } private Authenticator createAuthenticator(final MongoCredentialWithCache credential) { diff --git a/driver-core/src/main/com/mongodb/internal/connection/LoadBalancedClusterableServerFactory.java b/driver-core/src/main/com/mongodb/internal/connection/LoadBalancedClusterableServerFactory.java index d0cd40447d2..2ff282e9867 100644 --- a/driver-core/src/main/com/mongodb/internal/connection/LoadBalancedClusterableServerFactory.java +++ b/driver-core/src/main/com/mongodb/internal/connection/LoadBalancedClusterableServerFactory.java @@ -16,6 +16,7 @@ package com.mongodb.internal.connection; +import com.mongodb.LoggerSettings; import com.mongodb.MongoCompressor; import com.mongodb.MongoCredential; import com.mongodb.MongoDriverInformation; @@ -45,6 +46,7 @@ public class LoadBalancedClusterableServerFactory implements ClusterableServerFa private final InternalConnectionPoolSettings internalConnectionPoolSettings; private final StreamFactory streamFactory; private final MongoCredentialWithCache credential; + private final LoggerSettings loggerSettings; private final CommandListener commandListener; private final String applicationName; private final MongoDriverInformation mongoDriverInformation; @@ -55,6 +57,7 @@ public LoadBalancedClusterableServerFactory(final ServerSettings serverSettings, final ConnectionPoolSettings connectionPoolSettings, final InternalConnectionPoolSettings internalConnectionPoolSettings, final StreamFactory streamFactory, @Nullable final MongoCredential credential, + final LoggerSettings loggerSettings, @Nullable final CommandListener commandListener, @Nullable final String applicationName, final MongoDriverInformation mongoDriverInformation, final List compressorList, @Nullable final ServerApi serverApi) { @@ -63,6 +66,7 @@ public LoadBalancedClusterableServerFactory(final ServerSettings serverSettings, this.internalConnectionPoolSettings = internalConnectionPoolSettings; this.streamFactory = streamFactory; this.credential = credential == null ? null : new MongoCredentialWithCache(credential); + this.loggerSettings = loggerSettings; this.commandListener = commandListener; this.applicationName = applicationName; this.mongoDriverInformation = mongoDriverInformation; @@ -74,7 +78,7 @@ public LoadBalancedClusterableServerFactory(final ServerSettings serverSettings, public ClusterableServer create(final Cluster cluster, final ServerAddress serverAddress) { ConnectionPool connectionPool = new DefaultConnectionPool(new ServerId(cluster.getClusterId(), serverAddress), new InternalStreamConnectionFactory(ClusterConnectionMode.LOAD_BALANCED, streamFactory, credential, applicationName, - mongoDriverInformation, compressorList, commandListener, serverApi), + mongoDriverInformation, compressorList, loggerSettings, commandListener, serverApi), connectionPoolSettings, internalConnectionPoolSettings, EmptyProvider.instance()); connectionPool.ready(); diff --git a/driver-core/src/main/com/mongodb/internal/connection/LoggingCommandEventSender.java b/driver-core/src/main/com/mongodb/internal/connection/LoggingCommandEventSender.java index 613dcb841a3..eb811e5a94a 100644 --- a/driver-core/src/main/com/mongodb/internal/connection/LoggingCommandEventSender.java +++ b/driver-core/src/main/com/mongodb/internal/connection/LoggingCommandEventSender.java @@ -16,6 +16,7 @@ package com.mongodb.internal.connection; +import com.mongodb.LoggerSettings; import com.mongodb.MongoCommandException; import com.mongodb.RequestContext; import com.mongodb.connection.ClusterId; @@ -47,13 +48,13 @@ import static com.mongodb.internal.logging.StructuredLogMessage.Level.DEBUG; class LoggingCommandEventSender implements CommandEventSender { - private static final int MAX_COMMAND_DOCUMENT_LENGTH_TO_LOG = 1000; private static final double NANOS_PER_MILLI = 1_000_000.0d; private final ConnectionDescription description; @Nullable private final CommandListener commandListener; private final RequestContext requestContext; private final StructuredLogger logger; + private final LoggerSettings loggerSettings; private final long startTimeNanos; private final CommandMessage message; private final String commandName; @@ -63,11 +64,12 @@ class LoggingCommandEventSender implements CommandEventSender { LoggingCommandEventSender(final Set securitySensitiveCommands, final Set securitySensitiveHelloCommands, final ConnectionDescription description, @Nullable final CommandListener commandListener, final RequestContext requestContext, final CommandMessage message, - final ByteBufferBsonOutput bsonOutput, final StructuredLogger logger) { + final ByteBufferBsonOutput bsonOutput, final StructuredLogger logger, final LoggerSettings loggerSettings) { this.description = description; this.commandListener = commandListener; this.requestContext = requestContext; this.logger = logger; + this.loggerSettings = loggerSettings; this.startTimeNanos = System.nanoTime(); this.message = message; this.commandDocument = message.getCommandDocument(bsonOutput); @@ -207,12 +209,14 @@ private void appendCommonLogFragment(final List entries, final StringBuil entries.add(new Entry("requestId", message.getId())); } - private static String getTruncatedJsonCommand(final BsonDocument commandDocument) { + private String getTruncatedJsonCommand(final BsonDocument commandDocument) { StringWriter writer = new StringWriter(); try (BsonReader bsonReader = commandDocument.asBsonReader()) { JsonWriter jsonWriter = new JsonWriter(writer, - JsonWriterSettings.builder().outputMode(JsonMode.RELAXED).maxLength(MAX_COMMAND_DOCUMENT_LENGTH_TO_LOG).build()); + JsonWriterSettings.builder().outputMode(JsonMode.RELAXED) + .maxLength(loggerSettings.getMaxCommandLoggingDocumentLength()) + .build()); jsonWriter.pipe(bsonReader); diff --git a/driver-core/src/test/functional/com/mongodb/ClusterFixture.java b/driver-core/src/test/functional/com/mongodb/ClusterFixture.java index 772269ef2c3..945c2aba10d 100644 --- a/driver-core/src/test/functional/com/mongodb/ClusterFixture.java +++ b/driver-core/src/test/functional/com/mongodb/ClusterFixture.java @@ -378,7 +378,7 @@ private static Cluster createCluster(final MongoCredential credential, final Str return new DefaultClusterFactory().createCluster(ClusterSettings.builder().hosts(asList(getPrimary())).build(), ServerSettings.builder().build(), ConnectionPoolSettings.builder().maxSize(1).build(), InternalConnectionPoolSettings.builder().build(), - streamFactory, streamFactory, credential, null, null, null, + streamFactory, streamFactory, credential, LoggerSettings.builder().build(), null, null, null, Collections.emptyList(), getServerApi()); } @@ -390,7 +390,7 @@ private static Cluster createCluster(final ConnectionString connectionString, fi streamFactory, new SocketStreamFactory(SocketSettings.builder().readTimeout(5, SECONDS).build(), getSslSettings(connectionString)), connectionString.getCredential(), - null, null, null, + LoggerSettings.builder().build(), null, null, null, connectionString.getCompressorList(), getServerApi()); } diff --git a/driver-core/src/test/functional/com/mongodb/internal/connection/CommandHelperSpecification.groovy b/driver-core/src/test/functional/com/mongodb/internal/connection/CommandHelperSpecification.groovy index 1e7eaab5dfb..db367d4b7c8 100644 --- a/driver-core/src/test/functional/com/mongodb/internal/connection/CommandHelperSpecification.groovy +++ b/driver-core/src/test/functional/com/mongodb/internal/connection/CommandHelperSpecification.groovy @@ -16,6 +16,7 @@ package com.mongodb.internal.connection +import com.mongodb.LoggerSettings import com.mongodb.MongoCommandException import com.mongodb.ServerAddress import com.mongodb.connection.ClusterConnectionMode @@ -48,7 +49,7 @@ class CommandHelperSpecification extends Specification { def setup() { connection = new InternalStreamConnectionFactory(ClusterConnectionMode.SINGLE, new NettyStreamFactory(SocketSettings.builder().build(), getSslSettings()), - getCredentialWithCache(), null, null, [], null, getServerApi()) + getCredentialWithCache(), null, null, [], LoggerSettings.builder().build(), null, getServerApi()) .create(new ServerId(new ClusterId(), getPrimary())) connection.open() } diff --git a/driver-core/src/test/functional/com/mongodb/internal/connection/PlainAuthenticatorTest.java b/driver-core/src/test/functional/com/mongodb/internal/connection/PlainAuthenticatorTest.java index 6356186ab41..82e83b3b568 100644 --- a/driver-core/src/test/functional/com/mongodb/internal/connection/PlainAuthenticatorTest.java +++ b/driver-core/src/test/functional/com/mongodb/internal/connection/PlainAuthenticatorTest.java @@ -16,6 +16,7 @@ package com.mongodb.internal.connection; +import com.mongodb.LoggerSettings; import com.mongodb.MongoCredential; import com.mongodb.MongoSecurityException; import com.mongodb.ServerAddress; @@ -53,7 +54,7 @@ public void setUp() { source = System.getProperty("org.mongod.test.source"); password = System.getProperty("org.mongodb.test.password"); internalConnection = new InternalStreamConnectionFactory(ClusterConnectionMode.SINGLE, streamFactory, null, null, - null, Collections.emptyList(), null, getServerApi()).create(new ServerId(new ClusterId(), + null, Collections.emptyList(), LoggerSettings.builder().build(), null, getServerApi()).create(new ServerId(new ClusterId(), new ServerAddress(host))); connectionDescription = new ConnectionDescription(new ServerId(new ClusterId(), new ServerAddress())); } diff --git a/driver-core/src/test/functional/com/mongodb/internal/connection/ServerMonitorSpecification.groovy b/driver-core/src/test/functional/com/mongodb/internal/connection/ServerMonitorSpecification.groovy index b786b9cc0c6..5f9f940c98b 100644 --- a/driver-core/src/test/functional/com/mongodb/internal/connection/ServerMonitorSpecification.groovy +++ b/driver-core/src/test/functional/com/mongodb/internal/connection/ServerMonitorSpecification.groovy @@ -16,6 +16,7 @@ package com.mongodb.internal.connection +import com.mongodb.LoggerSettings import com.mongodb.MongoSocketException import com.mongodb.OperationFunctionalSpecification import com.mongodb.ServerAddress @@ -224,7 +225,7 @@ class ServerMonitorSpecification extends OperationFunctionalSpecification { new InternalStreamConnectionFactory(SINGLE, new SocketStreamFactory(SocketSettings.builder() .connectTimeout(500, TimeUnit.MILLISECONDS) .build(), - getSslSettings()), getCredentialWithCache(), null, null, [], null, + getSslSettings()), getCredentialWithCache(), null, null, [], LoggerSettings.builder().build(), null, getServerApi()), getClusterConnectionMode(), getServerApi(), SameObjectProvider.initialized(sdam)) serverMonitor.start() diff --git a/driver-core/src/test/functional/com/mongodb/internal/connection/SingleServerClusterTest.java b/driver-core/src/test/functional/com/mongodb/internal/connection/SingleServerClusterTest.java index 27b652b07f5..3ce2efc9dd7 100644 --- a/driver-core/src/test/functional/com/mongodb/internal/connection/SingleServerClusterTest.java +++ b/driver-core/src/test/functional/com/mongodb/internal/connection/SingleServerClusterTest.java @@ -16,6 +16,7 @@ package com.mongodb.internal.connection; +import com.mongodb.LoggerSettings; import com.mongodb.ReadPreference; import com.mongodb.ServerAddress; import com.mongodb.connection.ClusterConnectionMode; @@ -67,7 +68,7 @@ private void setUpCluster(final ServerAddress serverAddress) { ConnectionPoolSettings.builder().maxSize(1).build(), InternalConnectionPoolSettings.builder().build(), streamFactory, streamFactory, getCredential(), - null, null, null, + LoggerSettings.builder().build(), null, null, null, Collections.emptyList(), getServerApi())); } diff --git a/driver-core/src/test/unit/com/mongodb/MongoClientSettingsSpecification.groovy b/driver-core/src/test/unit/com/mongodb/MongoClientSettingsSpecification.groovy index 12f9a581d32..bf8c5d7d0ab 100644 --- a/driver-core/src/test/unit/com/mongodb/MongoClientSettingsSpecification.groovy +++ b/driver-core/src/test/unit/com/mongodb/MongoClientSettingsSpecification.groovy @@ -47,6 +47,7 @@ class MongoClientSettingsSpecification extends Specification { settings.getReadPreference() == ReadPreference.primary() settings.getCommandListeners().isEmpty() settings.getApplicationName() == null + settings.getLoggerSettings() == LoggerSettings.builder().build(); settings.clusterSettings == ClusterSettings.builder().build() settings.connectionPoolSettings == ConnectionPoolSettings.builder().build() settings.socketSettings == SocketSettings.builder().build() @@ -185,6 +186,7 @@ class MongoClientSettingsSpecification extends Specification { .readConcern(ReadConcern.LOCAL) .applicationName('app1') .addCommandListener(commandListener) + .applyToLoggerSettings(builder -> builder.maxCommandLoggingDocumentLength(10)) .applyToClusterSettings(new Block() { @Override void apply(final ClusterSettings.Builder builder) { @@ -464,7 +466,7 @@ class MongoClientSettingsSpecification extends Specification { def actual = MongoClientSettings.Builder.declaredFields.grep { !it.synthetic } *.name.sort() def expected = ['applicationName', 'autoEncryptionSettings', 'clusterSettingsBuilder', 'codecRegistry', 'commandListeners', 'compressorList', 'connectionPoolSettingsBuilder', 'contextProvider', 'credential', - 'heartbeatConnectTimeoutMS', 'heartbeatSocketTimeoutMS', + 'heartbeatConnectTimeoutMS', 'heartbeatSocketTimeoutMS', 'loggerSettingsBuilder', 'readConcern', 'readPreference', 'retryReads', 'retryWrites', 'serverApi', 'serverSettingsBuilder', 'socketSettingsBuilder', 'sslSettingsBuilder', 'streamFactoryFactory', 'uuidRepresentation', 'writeConcern'] @@ -478,11 +480,11 @@ class MongoClientSettingsSpecification extends Specification { // A regression test so that if anymore methods are added then the builder(final MongoClientSettings settings) should be updated def actual = MongoClientSettings.Builder.declaredMethods.grep { !it.synthetic } *.name.sort() def expected = ['addCommandListener', 'applicationName', 'applyConnectionString', 'applyToClusterSettings', - 'applyToConnectionPoolSettings', 'applyToServerSettings', 'applyToSocketSettings', 'applyToSslSettings', - 'autoEncryptionSettings', 'build', 'codecRegistry', 'commandListenerList', 'compressorList', 'contextProvider', - 'credential', 'heartbeatConnectTimeoutMS', 'heartbeatSocketTimeoutMS', 'readConcern', 'readPreference', - 'retryReads', 'retryWrites', 'serverApi', 'streamFactoryFactory', 'uuidRepresentation', - 'writeConcern'] + 'applyToConnectionPoolSettings', 'applyToLoggerSettings', 'applyToServerSettings', 'applyToSocketSettings', + 'applyToSslSettings', 'autoEncryptionSettings', 'build', 'codecRegistry', 'commandListenerList', + 'compressorList', 'contextProvider', 'credential', 'heartbeatConnectTimeoutMS', 'heartbeatSocketTimeoutMS', + 'readConcern', 'readPreference', 'retryReads', 'retryWrites', 'serverApi', 'streamFactoryFactory', + 'uuidRepresentation', 'writeConcern'] then: actual == expected } diff --git a/driver-core/src/test/unit/com/mongodb/internal/connection/AbstractConnectionPoolTest.java b/driver-core/src/test/unit/com/mongodb/internal/connection/AbstractConnectionPoolTest.java index 347b3b67fb0..4d178f95f8d 100644 --- a/driver-core/src/test/unit/com/mongodb/internal/connection/AbstractConnectionPoolTest.java +++ b/driver-core/src/test/unit/com/mongodb/internal/connection/AbstractConnectionPoolTest.java @@ -18,6 +18,7 @@ import com.mongodb.ClusterFixture; import com.mongodb.JsonTestServerVersionChecker; +import com.mongodb.LoggerSettings; import com.mongodb.MongoDriverInformation; import com.mongodb.MongoInterruptedException; import com.mongodb.MongoTimeoutException; @@ -183,6 +184,7 @@ public void setUp() { poolOptions.getString("appName", new BsonString(fileName + ": " + description)).getValue(), MongoDriverInformation.builder().build(), Collections.emptyList(), + LoggerSettings.builder().build(), new TestCommandListener(), ClusterFixture.getServerApi()), settings, internalSettings, sdamProvider)); diff --git a/driver-core/src/test/unit/com/mongodb/internal/connection/LoggingCommandEventSenderSpecification.groovy b/driver-core/src/test/unit/com/mongodb/internal/connection/LoggingCommandEventSenderSpecification.groovy index a10755bede0..9cc42168de1 100644 --- a/driver-core/src/test/unit/com/mongodb/internal/connection/LoggingCommandEventSenderSpecification.groovy +++ b/driver-core/src/test/unit/com/mongodb/internal/connection/LoggingCommandEventSenderSpecification.groovy @@ -16,6 +16,7 @@ package com.mongodb.internal.connection +import com.mongodb.LoggerSettings import com.mongodb.MongoInternalException import com.mongodb.MongoNamespace import com.mongodb.ReadPreference @@ -61,7 +62,7 @@ class LoggingCommandEventSenderSpecification extends Specification { isDebugEnabled() >> debugLoggingEnabled } def sender = new LoggingCommandEventSender([] as Set, [] as Set, connectionDescription, commandListener, - IgnorableRequestContext.INSTANCE, message, bsonOutput, new StructuredLogger(logger)) + IgnorableRequestContext.INSTANCE, message, bsonOutput, new StructuredLogger(logger), LoggerSettings.builder().build()) when: sender.sendStartedEvent() @@ -103,7 +104,7 @@ class LoggingCommandEventSenderSpecification extends Specification { isDebugEnabled() >> true } def sender = new LoggingCommandEventSender([] as Set, [] as Set, connectionDescription, commandListener, - IgnorableRequestContext.INSTANCE, message, bsonOutput, new StructuredLogger(logger)) + IgnorableRequestContext.INSTANCE, message, bsonOutput, new StructuredLogger(logger), LoggerSettings.builder().build()) when: sender.sendStartedEvent() sender.sendSucceededEventForOneWayCommand() @@ -156,7 +157,7 @@ class LoggingCommandEventSenderSpecification extends Specification { isDebugEnabled() >> true } def sender = new LoggingCommandEventSender([] as Set, [] as Set, connectionDescription, null, null, message, bsonOutput, - new StructuredLogger(logger)) + new StructuredLogger(logger), LoggerSettings.builder().build()) when: sender.sendStartedEvent() @@ -186,7 +187,7 @@ class LoggingCommandEventSenderSpecification extends Specification { isDebugEnabled() >> true } def sender = new LoggingCommandEventSender(['createUser'] as Set, [] as Set, connectionDescription, null, - IgnorableRequestContext.INSTANCE, message, bsonOutput, new StructuredLogger(logger)) + IgnorableRequestContext.INSTANCE, message, bsonOutput, new StructuredLogger(logger), LoggerSettings.builder().build()) when: sender.sendStartedEvent() diff --git a/driver-reactive-streams/src/main/com/mongodb/reactivestreams/client/MongoClients.java b/driver-reactive-streams/src/main/com/mongodb/reactivestreams/client/MongoClients.java index c34f85073ac..744a69ec266 100644 --- a/driver-reactive-streams/src/main/com/mongodb/reactivestreams/client/MongoClients.java +++ b/driver-reactive-streams/src/main/com/mongodb/reactivestreams/client/MongoClients.java @@ -147,7 +147,7 @@ private static Cluster createCluster(final MongoClientSettings settings, return new DefaultClusterFactory().createCluster(settings.getClusterSettings(), settings.getServerSettings(), settings.getConnectionPoolSettings(), InternalConnectionPoolSettings.builder().prestartAsyncWorkManager(true).build(), - streamFactory, heartbeatStreamFactory, settings.getCredential(), + streamFactory, heartbeatStreamFactory, settings.getCredential(), settings.getLoggerSettings(), getCommandListener(settings.getCommandListeners()), settings.getApplicationName(), mongoDriverInformation, settings.getCompressorList(), settings.getServerApi()); } diff --git a/driver-scala/src/main/scala/org/mongodb/scala/LoggerSettings.scala b/driver-scala/src/main/scala/org/mongodb/scala/LoggerSettings.scala new file mode 100644 index 00000000000..38a68924a99 --- /dev/null +++ b/driver-scala/src/main/scala/org/mongodb/scala/LoggerSettings.scala @@ -0,0 +1,40 @@ +/* + * Copyright 2008-present MongoDB, Inc. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.mongodb.scala + +import com.mongodb.{LoggerSettings => JLoggerSettings} + +/** + * Settings for the logger. + * + * @since 4.9 + */ +object LoggerSettings { + + /** + * Get a builder for this class. + * + * @return a new Builder for creating LoggerSettings. + */ + def builder(): Builder = JLoggerSettings.builder() + + /** + * ClusterSettings builder type + */ + type Builder = JLoggerSettings.Builder + +} diff --git a/driver-sync/src/main/com/mongodb/client/internal/MongoClientImpl.java b/driver-sync/src/main/com/mongodb/client/internal/MongoClientImpl.java index 1369f837f11..52dc02a41ee 100644 --- a/driver-sync/src/main/com/mongodb/client/internal/MongoClientImpl.java +++ b/driver-sync/src/main/com/mongodb/client/internal/MongoClientImpl.java @@ -228,8 +228,8 @@ private static Cluster createCluster(final MongoClientSettings settings, return new DefaultClusterFactory().createCluster(settings.getClusterSettings(), settings.getServerSettings(), settings.getConnectionPoolSettings(), InternalConnectionPoolSettings.builder().build(), getStreamFactory(settings, false), getStreamFactory(settings, true), - settings.getCredential(), getCommandListener(settings.getCommandListeners()), settings.getApplicationName(), - mongoDriverInformation, settings.getCompressorList(), settings.getServerApi()); + settings.getCredential(), settings.getLoggerSettings(), getCommandListener(settings.getCommandListeners()), + settings.getApplicationName(), mongoDriverInformation, settings.getCompressorList(), settings.getServerApi()); } private static StreamFactory getStreamFactory(final MongoClientSettings settings, final boolean isHeartbeat) { diff --git a/driver-sync/src/test/functional/com/mongodb/client/unified/Entities.java b/driver-sync/src/test/functional/com/mongodb/client/unified/Entities.java index 0a0f6af8575..e3f3d523a97 100644 --- a/driver-sync/src/test/functional/com/mongodb/client/unified/Entities.java +++ b/driver-sync/src/test/functional/com/mongodb/client/unified/Entities.java @@ -357,6 +357,7 @@ private void initClient(final BsonDocument entity, final String id, } clientSettingsBuilder.applicationName(id); + clientSettingsBuilder.applyToLoggerSettings(builder -> builder.maxCommandLoggingDocumentLength(10_000)); TestServerListener testClusterListener = new TestServerListener(); clientSettingsBuilder.applyToServerSettings(builder -> builder.addServerListener(testClusterListener)); From 0f6f05a0337c9bce47f90d7dd0b5e7a47034eb78 Mon Sep 17 00:00:00 2001 From: Jeff Yemin Date: Wed, 1 Feb 2023 08:19:59 -0500 Subject: [PATCH 2/3] Rename and improve javadoc --- .../src/main/com/mongodb/LoggerSettings.java | 39 ++++++++++--------- .../connection/LoggingCommandEventSender.java | 2 +- .../MongoClientSettingsSpecification.groovy | 2 +- .../com/mongodb/client/unified/Entities.java | 2 +- 4 files changed, 24 insertions(+), 21 deletions(-) diff --git a/driver-core/src/main/com/mongodb/LoggerSettings.java b/driver-core/src/main/com/mongodb/LoggerSettings.java index fa1d78f9f64..fe1fb19ae33 100644 --- a/driver-core/src/main/com/mongodb/LoggerSettings.java +++ b/driver-core/src/main/com/mongodb/LoggerSettings.java @@ -26,14 +26,16 @@ * An immutable class representing settings for logging. * *

- * The driver logs using SLF4J 1.0 API with a root logger of {@code org.mongodb.driver}. + * The driver logs using the SLF4J 1.0 API with a root logger of {@code org.mongodb.driver}. See + * Logging Fundamentals + * for additional information. *

* * @since 4.9 */ @Immutable public final class LoggerSettings { - private final int maxCommandLoggingDocumentLength; + private final int maxDocumentLength; /** * Gets a builder for an instance of {@code LoggerSettings}. * @return the builder @@ -56,7 +58,7 @@ public static Builder builder(final LoggerSettings loggerSettings) { * A builder for an instance of {@code LoggerSettings}. */ public static final class Builder { - private int maxCommandLoggingDocumentLength = 1000; + private int maxDocumentLength = 1000; private Builder() { } @@ -70,18 +72,19 @@ private Builder() { */ public Builder applySettings(final LoggerSettings loggerSettings) { notNull("loggerSettings", loggerSettings); - maxCommandLoggingDocumentLength = loggerSettings.maxCommandLoggingDocumentLength; + maxDocumentLength = loggerSettings.maxDocumentLength; return this; } /** - * Sets the max command logging document length. + * Sets the max document length. * - * @param maxCommandLoggingDocumentLength the max command logging document length + * @param maxDocumentLength the max document length * @return this + * @see #getMaxDocumentLength() */ - public Builder maxCommandLoggingDocumentLength(final int maxCommandLoggingDocumentLength) { - this.maxCommandLoggingDocumentLength = maxCommandLoggingDocumentLength; + public Builder maxDocumentLength(final int maxDocumentLength) { + this.maxDocumentLength = maxDocumentLength; return this; } @@ -95,21 +98,21 @@ public LoggerSettings build() { } /** - * Gets the max command logging document length. + * Gets the max length of the extended JSON representation of a BSON document within a log message. * *

- * When the driver logs a command and its reply via the {@code org.mongodb.driver.protocol.command} SFL4J logger, it truncates the - * them to the maximum length defined by this setting. + * For example, when the driver logs a command or its reply via the {@code org.mongodb.driver.protocol.command} SFL4J logger, it + * truncates its JSON representation to the maximum length defined by this setting. *

* *

* Defaults to 1000 characters. *

* - * @return the max command logging document length + * @return the max document length */ - public int getMaxCommandLoggingDocumentLength() { - return maxCommandLoggingDocumentLength; + public int getMaxDocumentLength() { + return maxDocumentLength; } @@ -122,22 +125,22 @@ public boolean equals(final Object o) { return false; } LoggerSettings that = (LoggerSettings) o; - return maxCommandLoggingDocumentLength == that.maxCommandLoggingDocumentLength; + return maxDocumentLength == that.maxDocumentLength; } @Override public int hashCode() { - return Objects.hash(maxCommandLoggingDocumentLength); + return Objects.hash(maxDocumentLength); } @Override public String toString() { return "LoggerSettings{" - + "maxCommandLoggingDocumentLength=" + maxCommandLoggingDocumentLength + + "maxDocumentLength=" + maxDocumentLength + '}'; } private LoggerSettings(final Builder builder) { - maxCommandLoggingDocumentLength = builder.maxCommandLoggingDocumentLength; + maxDocumentLength = builder.maxDocumentLength; } } diff --git a/driver-core/src/main/com/mongodb/internal/connection/LoggingCommandEventSender.java b/driver-core/src/main/com/mongodb/internal/connection/LoggingCommandEventSender.java index eb811e5a94a..8c489051192 100644 --- a/driver-core/src/main/com/mongodb/internal/connection/LoggingCommandEventSender.java +++ b/driver-core/src/main/com/mongodb/internal/connection/LoggingCommandEventSender.java @@ -215,7 +215,7 @@ private String getTruncatedJsonCommand(final BsonDocument commandDocument) { try (BsonReader bsonReader = commandDocument.asBsonReader()) { JsonWriter jsonWriter = new JsonWriter(writer, JsonWriterSettings.builder().outputMode(JsonMode.RELAXED) - .maxLength(loggerSettings.getMaxCommandLoggingDocumentLength()) + .maxLength(loggerSettings.getMaxDocumentLength()) .build()); jsonWriter.pipe(bsonReader); diff --git a/driver-core/src/test/unit/com/mongodb/MongoClientSettingsSpecification.groovy b/driver-core/src/test/unit/com/mongodb/MongoClientSettingsSpecification.groovy index bf8c5d7d0ab..12540978c9a 100644 --- a/driver-core/src/test/unit/com/mongodb/MongoClientSettingsSpecification.groovy +++ b/driver-core/src/test/unit/com/mongodb/MongoClientSettingsSpecification.groovy @@ -186,7 +186,7 @@ class MongoClientSettingsSpecification extends Specification { .readConcern(ReadConcern.LOCAL) .applicationName('app1') .addCommandListener(commandListener) - .applyToLoggerSettings(builder -> builder.maxCommandLoggingDocumentLength(10)) + .applyToLoggerSettings(builder -> builder.maxDocumentLength(10)) .applyToClusterSettings(new Block() { @Override void apply(final ClusterSettings.Builder builder) { diff --git a/driver-sync/src/test/functional/com/mongodb/client/unified/Entities.java b/driver-sync/src/test/functional/com/mongodb/client/unified/Entities.java index e3f3d523a97..0dd1041117b 100644 --- a/driver-sync/src/test/functional/com/mongodb/client/unified/Entities.java +++ b/driver-sync/src/test/functional/com/mongodb/client/unified/Entities.java @@ -357,7 +357,7 @@ private void initClient(final BsonDocument entity, final String id, } clientSettingsBuilder.applicationName(id); - clientSettingsBuilder.applyToLoggerSettings(builder -> builder.maxCommandLoggingDocumentLength(10_000)); + clientSettingsBuilder.applyToLoggerSettings(builder -> builder.maxDocumentLength(10_000)); TestServerListener testClusterListener = new TestServerListener(); clientSettingsBuilder.applyToServerSettings(builder -> builder.addServerListener(testClusterListener)); From 8df64694600080c7245e628ece51c1fedcc8942a Mon Sep 17 00:00:00 2001 From: Jeff Yemin Date: Wed, 1 Feb 2023 08:22:15 -0500 Subject: [PATCH 3/3] Scala format issue --- .../src/main/scala/org/mongodb/scala/LoggerSettings.scala | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/driver-scala/src/main/scala/org/mongodb/scala/LoggerSettings.scala b/driver-scala/src/main/scala/org/mongodb/scala/LoggerSettings.scala index 38a68924a99..93a04bf9cd3 100644 --- a/driver-scala/src/main/scala/org/mongodb/scala/LoggerSettings.scala +++ b/driver-scala/src/main/scala/org/mongodb/scala/LoggerSettings.scala @@ -16,7 +16,7 @@ package org.mongodb.scala -import com.mongodb.{LoggerSettings => JLoggerSettings} +import com.mongodb.{ LoggerSettings => JLoggerSettings } /** * Settings for the logger.