001/*
002 * Licensed to the Apache Software Foundation (ASF) under one or more
003 * contributor license agreements. See the NOTICE file distributed with
004 * this work for additional information regarding copyright ownership.
005 * The ASF licenses this file to You under the Apache license, Version 2.0
006 * (the "License"); you may not use this file except in compliance with
007 * the License. You may obtain a copy of the License at
008 *
009 *      http://www.apache.org/licenses/LICENSE-2.0
010 *
011 * Unless required by applicable law or agreed to in writing, software
012 * distributed under the License is distributed on an "AS IS" BASIS,
013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014 * See the license for the specific language governing permissions and
015 * limitations under the license.
016 */
017package org.apache.logging.log4j.nosql.appender;
018
019import java.util.Map;
020
021import org.apache.logging.log4j.Marker;
022import org.apache.logging.log4j.ThreadContext;
023import org.apache.logging.log4j.core.LogEvent;
024import org.apache.logging.log4j.core.appender.AppenderLoggingException;
025import org.apache.logging.log4j.core.appender.ManagerFactory;
026import org.apache.logging.log4j.core.appender.db.AbstractDatabaseManager;
027import org.apache.logging.log4j.core.util.Closer;
028
029/**
030 * An {@link AbstractDatabaseManager} implementation for all NoSQL databases.
031 *
032 * @param <W> A type parameter for reassuring the compiler that all operations are using the same {@link NoSqlObject}.
033 */
034public final class NoSqlDatabaseManager<W> extends AbstractDatabaseManager {
035    private static final NoSQLDatabaseManagerFactory FACTORY = new NoSQLDatabaseManagerFactory();
036
037    private final NoSqlProvider<NoSqlConnection<W, ? extends NoSqlObject<W>>> provider;
038
039    private NoSqlConnection<W, ? extends NoSqlObject<W>> connection;
040
041    private NoSqlDatabaseManager(final String name, final int bufferSize,
042            final NoSqlProvider<NoSqlConnection<W, ? extends NoSqlObject<W>>> provider) {
043        super(name, bufferSize);
044        this.provider = provider;
045    }
046
047    @Override
048    protected void startupInternal() {
049        // nothing to see here
050    }
051
052    @Override
053    protected void shutdownInternal() {
054        // NoSQL doesn't use transactions, so all we need to do here is simply close the client
055        Closer.closeSilently(this.connection);
056    }
057
058    @Override
059    protected void connectAndStart() {
060        try {
061            this.connection = this.provider.getConnection();
062        } catch (final Exception e) {
063            throw new AppenderLoggingException("Failed to get connection from NoSQL connection provider.", e);
064        }
065    }
066
067    @Override
068    protected void writeInternal(final LogEvent event) {
069        if (!this.isRunning() || this.connection == null || this.connection.isClosed()) {
070            throw new AppenderLoggingException(
071                    "Cannot write logging event; NoSQL manager not connected to the database.");
072        }
073
074        final NoSqlObject<W> entity = this.connection.createObject();
075        entity.set("level", event.getLevel());
076        entity.set("loggerName", event.getLoggerName());
077        entity.set("message", event.getMessage() == null ? null : event.getMessage().getFormattedMessage());
078
079        final StackTraceElement source = event.getSource();
080        if (source == null) {
081            entity.set("source", (Object) null);
082        } else {
083            entity.set("source", this.convertStackTraceElement(source));
084        }
085
086        final Marker marker = event.getMarker();
087        if (marker == null) {
088            entity.set("marker", (Object) null);
089        } else {
090            entity.set("marker", buildMarkerEntity(marker));
091        }
092
093        entity.set("threadId", event.getThreadId());
094        entity.set("threadName", event.getThreadName());
095        entity.set("threadPriority", event.getThreadPriority());
096        entity.set("millis", event.getTimeMillis());
097        entity.set("date", new java.util.Date(event.getTimeMillis()));
098
099        @SuppressWarnings("ThrowableResultOfMethodCallIgnored")
100        Throwable thrown = event.getThrown();
101        if (thrown == null) {
102            entity.set("thrown", (Object) null);
103        } else {
104            final NoSqlObject<W> originalExceptionEntity = this.connection.createObject();
105            NoSqlObject<W> exceptionEntity = originalExceptionEntity;
106            exceptionEntity.set("type", thrown.getClass().getName());
107            exceptionEntity.set("message", thrown.getMessage());
108            exceptionEntity.set("stackTrace", this.convertStackTrace(thrown.getStackTrace()));
109            while (thrown.getCause() != null) {
110                thrown = thrown.getCause();
111                final NoSqlObject<W> causingExceptionEntity = this.connection.createObject();
112                causingExceptionEntity.set("type", thrown.getClass().getName());
113                causingExceptionEntity.set("message", thrown.getMessage());
114                causingExceptionEntity.set("stackTrace", this.convertStackTrace(thrown.getStackTrace()));
115                exceptionEntity.set("cause", causingExceptionEntity);
116                exceptionEntity = causingExceptionEntity;
117            }
118
119            entity.set("thrown", originalExceptionEntity);
120        }
121
122        final Map<String, String> contextMap = event.getContextMap();
123        if (contextMap == null) {
124            entity.set("contextMap", (Object) null);
125        } else {
126            final NoSqlObject<W> contextMapEntity = this.connection.createObject();
127            for (final Map.Entry<String, String> entry : contextMap.entrySet()) {
128                contextMapEntity.set(entry.getKey(), entry.getValue());
129            }
130            entity.set("contextMap", contextMapEntity);
131        }
132
133        final ThreadContext.ContextStack contextStack = event.getContextStack();
134        if (contextStack == null) {
135            entity.set("contextStack", (Object) null);
136        } else {
137            entity.set("contextStack", contextStack.asList().toArray());
138        }
139
140        this.connection.insertObject(entity);
141    }
142
143    private NoSqlObject<W> buildMarkerEntity(final Marker marker) {
144        final NoSqlObject<W> entity = this.connection.createObject();
145        entity.set("name", marker.getName());
146
147        final Marker[] parents = marker.getParents();
148        if (parents != null) {
149            @SuppressWarnings("unchecked")
150            final NoSqlObject<W>[] parentEntities = new NoSqlObject[parents.length];
151            for (int i = 0; i < parents.length; i++) {
152                parentEntities[i] = buildMarkerEntity(parents[i]);
153            }
154            entity.set("parents", parentEntities);
155        }
156        return entity;
157    }
158
159    @Override
160    protected void commitAndClose() {
161        // all NoSQL drivers auto-commit (since NoSQL doesn't generally use the concept of transactions).
162        // also, all our NoSQL drivers use internal connection pooling and provide clients, not connections.
163        // thus, we should not be closing the client until shutdown as NoSQL is very different from SQL.
164        // see LOG4J2-591 and LOG4J2-676
165    }
166
167    private NoSqlObject<W>[] convertStackTrace(final StackTraceElement[] stackTrace) {
168        final NoSqlObject<W>[] stackTraceEntities = this.connection.createList(stackTrace.length);
169        for (int i = 0; i < stackTrace.length; i++) {
170            stackTraceEntities[i] = this.convertStackTraceElement(stackTrace[i]);
171        }
172        return stackTraceEntities;
173    }
174
175    private NoSqlObject<W> convertStackTraceElement(final StackTraceElement element) {
176        final NoSqlObject<W> elementEntity = this.connection.createObject();
177        elementEntity.set("className", element.getClassName());
178        elementEntity.set("methodName", element.getMethodName());
179        elementEntity.set("fileName", element.getFileName());
180        elementEntity.set("lineNumber", element.getLineNumber());
181        return elementEntity;
182    }
183
184    /**
185     * Creates a NoSQL manager for use within the {@link NoSqlAppender}, or returns a suitable one if it already exists.
186     *
187     * @param name The name of the manager, which should include connection details and hashed passwords where possible.
188     * @param bufferSize The size of the log event buffer.
189     * @param provider A provider instance which will be used to obtain connections to the chosen NoSQL database.
190     * @return a new or existing NoSQL manager as applicable.
191     */
192    public static NoSqlDatabaseManager<?> getNoSqlDatabaseManager(final String name, final int bufferSize,
193                                                                  final NoSqlProvider<?> provider) {
194        return AbstractDatabaseManager.getManager(name, new FactoryData(bufferSize, provider), FACTORY);
195    }
196
197    /**
198     * Encapsulates data that {@link NoSQLDatabaseManagerFactory} uses to create managers.
199     */
200    private static final class FactoryData extends AbstractDatabaseManager.AbstractFactoryData {
201        private final NoSqlProvider<?> provider;
202
203        protected FactoryData(final int bufferSize, final NoSqlProvider<?> provider) {
204            super(bufferSize);
205            this.provider = provider;
206        }
207    }
208
209    /**
210     * Creates managers.
211     */
212    private static final class NoSQLDatabaseManagerFactory implements
213            ManagerFactory<NoSqlDatabaseManager<?>, FactoryData> {
214        @Override
215        @SuppressWarnings("unchecked")
216        public NoSqlDatabaseManager<?> createManager(final String name, final FactoryData data) {
217            return new NoSqlDatabaseManager(name, data.getBufferSize(), data.provider);
218        }
219    }
220}