Source Code for Apache Log4j NoSQL Appenders

Apache Log4j NoSQL Appenders allows applications to send events to NoSQL repositories. To use it, you need both the log4j-nosql as well as the appropriate driver for your NoSQL database type.

Bytecode (Java 8) for Apache Log4j NoSQL Appenders is provided in a separate JAR file like log4j-nosql-2.3.jar.

Source Code files for Apache Log4j NoSQL Appenders are provided in both binary packge like apache-log4j-2.3-bin.zip and source package like apache-log4j-2.3-src.zip. You can download them at Apache Log4j Website.

You can also browse Source Code files for Apache Log4j NoSQL Appenders 2.3 below.

✍: FYIcenter.com

org/apache/logging/log4j/nosql/appender/couchdb/CouchDbProvider.java

/*
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements. See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You 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.apache.logging.log4j.nosql.appender.couchdb;

import java.lang.reflect.Method;

import org.apache.logging.log4j.Logger;
import org.apache.logging.log4j.core.appender.AbstractAppender;
import org.apache.logging.log4j.core.config.plugins.Plugin;
import org.apache.logging.log4j.core.config.plugins.PluginAttribute;
import org.apache.logging.log4j.core.config.plugins.PluginFactory;
import org.apache.logging.log4j.core.util.Loader;
import org.apache.logging.log4j.core.util.NameUtil;
import org.apache.logging.log4j.nosql.appender.NoSqlProvider;
import org.apache.logging.log4j.status.StatusLogger;
import org.apache.logging.log4j.util.Strings;
import org.lightcouch.CouchDbClient;
import org.lightcouch.CouchDbProperties;

/**
 * The Apache CouchDB implementation of {@link NoSqlProvider}.
 */
@Plugin(name = "CouchDB", category = "Core", printObject = true)
public final class CouchDbProvider implements NoSqlProvider<CouchDbConnection> {
    private static final int HTTP = 80;
    private static final int HTTPS = 443;
    private static final Logger LOGGER = StatusLogger.getLogger();

    private final CouchDbClient client;
    private final String description;

    private CouchDbProvider(final CouchDbClient client, final String description) {
        this.client = client;
        this.description = "couchDb{ " + description + " }";
    }

    @Override
    public CouchDbConnection getConnection() {
        return new CouchDbConnection(this.client);
    }

    @Override
    public String toString() {
        return this.description;
    }

    /**
     * Factory method for creating an Apache CouchDB provider within the plugin manager.
     *
     * @param databaseName The name of the database to which log event documents will be written.
     * @param protocol Either "http" or "https," defaults to "http" and mutually exclusive with
     *                 {@code factoryClassName&factoryMethodName!=null}.
     * @param server The host name of the CouchDB server, defaults to localhost and mutually exclusive with
     *               {@code factoryClassName&factoryMethodName!=null}.
     * @param port The port that CouchDB is listening on, defaults to 80 if {@code protocol} is "http" and 443 if
     *             {@code protocol} is "https," and mutually exclusive with
     *             {@code factoryClassName&factoryMethodName!=null}.
     * @param username The username to authenticate against the MongoDB server with, mutually exclusive with
     *                 {@code factoryClassName&factoryMethodName!=null}.
     * @param password The password to authenticate against the MongoDB server with, mutually exclusive with
     *                 {@code factoryClassName&factoryMethodName!=null}.
     * @param factoryClassName A fully qualified class name containing a static factory method capable of returning a
     *                         {@link CouchDbClient} or {@link CouchDbProperties}.
     * @param factoryMethodName The name of the public static factory method belonging to the aforementioned factory
     *                          class.
     * @return a new Apache CouchDB provider.
     */
    @PluginFactory
    public static CouchDbProvider createNoSqlProvider(
            @PluginAttribute("databaseName") final String databaseName,
            @PluginAttribute("protocol") String protocol,
            @PluginAttribute("server") String server,
            @PluginAttribute("port") final String port,
            @PluginAttribute("username") final String username,
            @PluginAttribute(value = "password", sensitive = true) final String password,
            @PluginAttribute("factoryClassName") final String factoryClassName,
            @PluginAttribute("factoryMethodName") final String factoryMethodName) {
        CouchDbClient client;
        String description;
        if (factoryClassName != null && factoryClassName.length() > 0 &&
                factoryMethodName != null && factoryMethodName.length() > 0) {
            try {
                final Class<?> factoryClass = Loader.loadClass(factoryClassName);
                final Method method = factoryClass.getMethod(factoryMethodName);
                final Object object = method.invoke(null);

                if (object instanceof CouchDbClient) {
                    client = (CouchDbClient) object;
                    description = "uri=" + client.getDBUri();
                } else if (object instanceof CouchDbProperties) {
                    final CouchDbProperties properties = (CouchDbProperties) object;
                    client = new CouchDbClient(properties);
                    description = "uri=" + client.getDBUri() + ", username=" + properties.getUsername()
                            + ", passwordHash=" + NameUtil.md5(password + CouchDbProvider.class.getName())
                            + ", maxConnections=" + properties.getMaxConnections() + ", connectionTimeout="
                            + properties.getConnectionTimeout() + ", socketTimeout=" + properties.getSocketTimeout();
                } else if (object == null) {
                    LOGGER.error("The factory method [{}.{}()] returned null.", factoryClassName, factoryMethodName);
                    return null;
                } else {
                    LOGGER.error("The factory method [{}.{}()] returned an unsupported type [{}].", factoryClassName,
                            factoryMethodName, object.getClass().getName());
                    return null;
                }
            } catch (final ClassNotFoundException e) {
                LOGGER.error("The factory class [{}] could not be loaded.", factoryClassName, e);
                return null;
            } catch (final NoSuchMethodException e) {
                LOGGER.error("The factory class [{}] does not have a no-arg method named [{}].", factoryClassName,
                        factoryMethodName, e);
                return null;
            } catch (final Exception e) {
                LOGGER.error("The factory method [{}.{}()] could not be invoked.", factoryClassName, factoryMethodName,
                        e);
                return null;
            }
        } else if (databaseName != null && databaseName.length() > 0) {
            if (protocol != null && protocol.length() > 0) {
                protocol = protocol.toLowerCase();
                if (!protocol.equals("http") && !protocol.equals("https")) {
                    LOGGER.error("Only protocols [http] and [https] are supported, [{}] specified.", protocol);
                    return null;
                }
            } else {
                protocol = "http";
                LOGGER.warn("No protocol specified, using default port [http].");
            }

            final int portInt = AbstractAppender.parseInt(port, protocol.equals("https") ? HTTPS : HTTP);

            if (Strings.isEmpty(server)) {
                server = "localhost";
                LOGGER.warn("No server specified, using default server localhost.");
            }

            if (Strings.isEmpty(username) || Strings.isEmpty(password)) {
                LOGGER.error("You must provide a username and password for the CouchDB provider.");
                return null;
            }

            client = new CouchDbClient(databaseName, false, protocol, server, portInt, username, password);
            description = "uri=" + client.getDBUri() + ", username=" + username + ", passwordHash="
                    + NameUtil.md5(password + CouchDbProvider.class.getName());
        } else {
            LOGGER.error("No factory method was provided so the database name is required.");
            return null;
        }

        return new CouchDbProvider(client, description);
    }
}

org/apache/logging/log4j/nosql/appender/couchdb/CouchDbProvider.java

 

⇒ Downloading Apache Log4j 2.4.1 Package

⇐ Source Code for Apache Log4j JMX GUI

⇑ Downloading and Reviewing Apache Log4j Packages

⇑⇑ FAQ for Apache Log4j

2015-11-04, 7938👍, 0💬