This is an automated email from the ASF dual-hosted git repository. ggregory pushed a commit to branch release-2.x in repository https://gitbox.apache.org/repos/asf/logging-log4j2.git
commit 77b61063648551949d09cf8127330db1ee2af51e Author: Gary Gregory <[email protected]> AuthorDate: Wed Feb 9 08:47:24 2022 -0500 Sort members. --- .../core/appender/nosql/NoSqlDatabaseManager.java | 230 ++++++++++----------- 1 file changed, 115 insertions(+), 115 deletions(-) diff --git a/log4j-core/src/main/java/org/apache/logging/log4j/core/appender/nosql/NoSqlDatabaseManager.java b/log4j-core/src/main/java/org/apache/logging/log4j/core/appender/nosql/NoSqlDatabaseManager.java index abd0da2..1dad261 100644 --- a/log4j-core/src/main/java/org/apache/logging/log4j/core/appender/nosql/NoSqlDatabaseManager.java +++ b/log4j-core/src/main/java/org/apache/logging/log4j/core/appender/nosql/NoSqlDatabaseManager.java @@ -38,8 +38,63 @@ import org.apache.logging.log4j.util.ReadOnlyStringMap; * @param <W> A type parameter for reassuring the compiler that all operations are using the same {@link NoSqlObject}. */ public final class NoSqlDatabaseManager<W> extends AbstractDatabaseManager { + /** + * Encapsulates data that {@link NoSQLDatabaseManagerFactory} uses to create managers. + */ + private static final class FactoryData extends AbstractDatabaseManager.AbstractFactoryData { + private final NoSqlProvider<?> provider; + private final KeyValuePair[] additionalFields; + + protected FactoryData(final Configuration configuration, final int bufferSize, final NoSqlProvider<?> provider, final KeyValuePair[] additionalFields) { + super(configuration, bufferSize, null); // no layout + this.provider = Objects.requireNonNull(provider, "provider"); + this.additionalFields = additionalFields; // null OK + } + } + + /** + * Creates managers. + */ + private static final class NoSQLDatabaseManagerFactory implements ManagerFactory<NoSqlDatabaseManager<?>, FactoryData> { + @Override + @SuppressWarnings("unchecked") + public NoSqlDatabaseManager<?> createManager(final String name, final FactoryData data) { + Objects.requireNonNull(data, "data"); + return new NoSqlDatabaseManager(name, data.getBufferSize(), data.provider, data.additionalFields, data.getConfiguration()); + } + } + private static final NoSQLDatabaseManagerFactory FACTORY = new NoSQLDatabaseManagerFactory(); + /** + * Creates a NoSQL manager for use within the {@link NoSqlAppender}, or returns a suitable one if it already exists. + * + * @param name The name of the manager, which should include connection details and hashed passwords where possible. + * @param bufferSize The size of the log event buffer. + * @param provider A provider instance which will be used to obtain connections to the chosen NoSQL database. + * @return a new or existing NoSQL manager as applicable. + * @deprecated Use {@link #getNoSqlDatabaseManager(String, int, NoSqlProvider, KeyValuePair[], Configuration)}. + */ + @Deprecated + public static NoSqlDatabaseManager<?> getNoSqlDatabaseManager(final String name, final int bufferSize, final NoSqlProvider<?> provider) { + return AbstractDatabaseManager.getManager(name, new FactoryData(null, bufferSize, provider, null), FACTORY); + } + + /** + * Creates a NoSQL manager for use within the {@link NoSqlAppender}, or returns a suitable one if it already exists. + * + * @param name The name of the manager, which should include connection details and hashed passwords where possible. + * @param bufferSize The size of the log event buffer. + * @param provider A provider instance which will be used to obtain connections to the chosen NoSQL database. + * @param additionalFields Additional fields. + * @param configuration TODO + * @return a new or existing NoSQL manager as applicable. + */ + public static NoSqlDatabaseManager<?> getNoSqlDatabaseManager(final String name, final int bufferSize, final NoSqlProvider<?> provider, + final KeyValuePair[] additionalFields, final Configuration configuration) { + return AbstractDatabaseManager.getManager(name, new FactoryData(configuration, bufferSize, provider, additionalFields), FACTORY); + } + private final NoSqlProvider<NoSqlConnection<W, ? extends NoSqlObject<W>>> provider; private NoSqlConnection<W, ? extends NoSqlObject<W>> connection; @@ -53,15 +108,29 @@ public final class NoSqlDatabaseManager<W> extends AbstractDatabaseManager { this.additionalFields = additionalFields; } - @Override - protected void startupInternal() { - // nothing to see here + private NoSqlObject<W> buildMarkerEntity(final Marker marker) { + final NoSqlObject<W> entity = this.connection.createObject(); + entity.set("name", marker.getName()); + + final Marker[] parents = marker.getParents(); + if (parents != null) { + @SuppressWarnings("unchecked") + final NoSqlObject<W>[] parentEntities = new NoSqlObject[parents.length]; + for (int i = 0; i < parents.length; i++) { + parentEntities[i] = buildMarkerEntity(parents[i]); + } + entity.set("parents", parentEntities); + } + return entity; } @Override - protected boolean shutdownInternal() { - // NoSQL doesn't use transactions, so all we need to do here is simply close the client - return Closer.closeSilently(this.connection); + protected boolean commitAndClose() { + // all NoSQL drivers auto-commit (since NoSQL doesn't generally use the concept of transactions). + // also, all our NoSQL drivers use internal connection pooling and provide clients, not connections. + // thus, we should not be closing the client until shutdown as NoSQL is very different from SQL. + // see LOG4J2-591 and LOG4J2-676 + return true; } @Override @@ -73,20 +142,28 @@ public final class NoSqlDatabaseManager<W> extends AbstractDatabaseManager { } } - @Override - protected void writeInternal(final LogEvent event, final Serializable serializable) { - if (!this.isRunning() || this.connection == null || this.connection.isClosed()) { - throw new AppenderLoggingException("Cannot write logging event; NoSQL manager not connected to the database."); - } + private NoSqlObject<W> convertAdditionalField(KeyValuePair field) { + final NoSqlObject<W> object = connection.createObject(); + object.set("key", field.getKey()); + object.set("value", getStrSubstitutor().replace(field.getValue())); + return object; + } - final NoSqlObject<W> entity = this.connection.createObject(); - if (serializable instanceof MapMessage) { - setFields((MapMessage<?, ?>) serializable, entity); - } else { - setFields(event, entity); + private NoSqlObject<W>[] convertStackTrace(final StackTraceElement[] stackTrace) { + final NoSqlObject<W>[] stackTraceEntities = this.connection.createList(stackTrace.length); + for (int i = 0; i < stackTrace.length; i++) { + stackTraceEntities[i] = this.convertStackTraceElement(stackTrace[i]); } - setAdditionalFields(entity); - this.connection.insertObject(entity); + return stackTraceEntities; + } + + private NoSqlObject<W> convertStackTraceElement(final StackTraceElement element) { + final NoSqlObject<W> elementEntity = this.connection.createObject(); + elementEntity.set("className", element.getClassName()); + elementEntity.set("methodName", element.getMethodName()); + elementEntity.set("fileName", element.getFileName()); + elementEntity.set("lineNumber", element.getLineNumber()); + return elementEntity; } private void setAdditionalFields(final NoSqlObject<W> entity) { @@ -95,18 +172,6 @@ public final class NoSqlDatabaseManager<W> extends AbstractDatabaseManager { } } - private NoSqlObject<W> convertAdditionalField(KeyValuePair field) { - final NoSqlObject<W> object = connection.createObject(); - object.set("key", field.getKey()); - object.set("value", getStrSubstitutor().replace(field.getValue())); - return object; - } - - private void setFields(final MapMessage<?, ?> mapMessage, final NoSqlObject<W> noSqlObject) { - // Map without calling org.apache.logging.log4j.message.MapMessage#getData() which makes a copy of the map. - mapMessage.forEach((key, value) -> noSqlObject.set(key, value)); - } - private void setFields(final LogEvent event, final NoSqlObject<W> entity) { entity.set("level", event.getLevel()); entity.set("loggerName", event.getLoggerName()); @@ -172,100 +237,35 @@ public final class NoSqlDatabaseManager<W> extends AbstractDatabaseManager { } } - private NoSqlObject<W> buildMarkerEntity(final Marker marker) { - final NoSqlObject<W> entity = this.connection.createObject(); - entity.set("name", marker.getName()); - - final Marker[] parents = marker.getParents(); - if (parents != null) { - @SuppressWarnings("unchecked") - final NoSqlObject<W>[] parentEntities = new NoSqlObject[parents.length]; - for (int i = 0; i < parents.length; i++) { - parentEntities[i] = buildMarkerEntity(parents[i]); - } - entity.set("parents", parentEntities); - } - return entity; + private void setFields(final MapMessage<?, ?> mapMessage, final NoSqlObject<W> noSqlObject) { + // Map without calling org.apache.logging.log4j.message.MapMessage#getData() which makes a copy of the map. + mapMessage.forEach((key, value) -> noSqlObject.set(key, value)); } @Override - protected boolean commitAndClose() { - // all NoSQL drivers auto-commit (since NoSQL doesn't generally use the concept of transactions). - // also, all our NoSQL drivers use internal connection pooling and provide clients, not connections. - // thus, we should not be closing the client until shutdown as NoSQL is very different from SQL. - // see LOG4J2-591 and LOG4J2-676 - return true; - } - - private NoSqlObject<W>[] convertStackTrace(final StackTraceElement[] stackTrace) { - final NoSqlObject<W>[] stackTraceEntities = this.connection.createList(stackTrace.length); - for (int i = 0; i < stackTrace.length; i++) { - stackTraceEntities[i] = this.convertStackTraceElement(stackTrace[i]); - } - return stackTraceEntities; - } - - private NoSqlObject<W> convertStackTraceElement(final StackTraceElement element) { - final NoSqlObject<W> elementEntity = this.connection.createObject(); - elementEntity.set("className", element.getClassName()); - elementEntity.set("methodName", element.getMethodName()); - elementEntity.set("fileName", element.getFileName()); - elementEntity.set("lineNumber", element.getLineNumber()); - return elementEntity; - } - - /** - * Creates a NoSQL manager for use within the {@link NoSqlAppender}, or returns a suitable one if it already exists. - * - * @param name The name of the manager, which should include connection details and hashed passwords where possible. - * @param bufferSize The size of the log event buffer. - * @param provider A provider instance which will be used to obtain connections to the chosen NoSQL database. - * @return a new or existing NoSQL manager as applicable. - * @deprecated Use {@link #getNoSqlDatabaseManager(String, int, NoSqlProvider, KeyValuePair[], Configuration)}. - */ - @Deprecated - public static NoSqlDatabaseManager<?> getNoSqlDatabaseManager(final String name, final int bufferSize, final NoSqlProvider<?> provider) { - return AbstractDatabaseManager.getManager(name, new FactoryData(null, bufferSize, provider, null), FACTORY); + protected boolean shutdownInternal() { + // NoSQL doesn't use transactions, so all we need to do here is simply close the client + return Closer.closeSilently(this.connection); } - /** - * Creates a NoSQL manager for use within the {@link NoSqlAppender}, or returns a suitable one if it already exists. - * - * @param name The name of the manager, which should include connection details and hashed passwords where possible. - * @param bufferSize The size of the log event buffer. - * @param provider A provider instance which will be used to obtain connections to the chosen NoSQL database. - * @param additionalFields Additional fields. - * @param configuration TODO - * @return a new or existing NoSQL manager as applicable. - */ - public static NoSqlDatabaseManager<?> getNoSqlDatabaseManager(final String name, final int bufferSize, final NoSqlProvider<?> provider, - final KeyValuePair[] additionalFields, final Configuration configuration) { - return AbstractDatabaseManager.getManager(name, new FactoryData(configuration, bufferSize, provider, additionalFields), FACTORY); + @Override + protected void startupInternal() { + // nothing to see here } - /** - * Encapsulates data that {@link NoSQLDatabaseManagerFactory} uses to create managers. - */ - private static final class FactoryData extends AbstractDatabaseManager.AbstractFactoryData { - private final NoSqlProvider<?> provider; - private final KeyValuePair[] additionalFields; - - protected FactoryData(final Configuration configuration, final int bufferSize, final NoSqlProvider<?> provider, final KeyValuePair[] additionalFields) { - super(configuration, bufferSize, null); // no layout - this.provider = Objects.requireNonNull(provider, "provider"); - this.additionalFields = additionalFields; // null OK + @Override + protected void writeInternal(final LogEvent event, final Serializable serializable) { + if (!this.isRunning() || this.connection == null || this.connection.isClosed()) { + throw new AppenderLoggingException("Cannot write logging event; NoSQL manager not connected to the database."); } - } - /** - * Creates managers. - */ - private static final class NoSQLDatabaseManagerFactory implements ManagerFactory<NoSqlDatabaseManager<?>, FactoryData> { - @Override - @SuppressWarnings("unchecked") - public NoSqlDatabaseManager<?> createManager(final String name, final FactoryData data) { - Objects.requireNonNull(data, "data"); - return new NoSqlDatabaseManager(name, data.getBufferSize(), data.provider, data.additionalFields, data.getConfiguration()); + final NoSqlObject<W> entity = this.connection.createObject(); + if (serializable instanceof MapMessage) { + setFields((MapMessage<?, ?>) serializable, entity); + } else { + setFields(event, entity); } + setAdditionalFields(entity); + this.connection.insertObject(entity); } }
