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.core.appender.nosql;
018
019import java.io.Serializable;
020
021import javax.jms.JMSException;
022
023import org.apache.logging.log4j.Marker;
024import org.apache.logging.log4j.ThreadContext;
025import org.apache.logging.log4j.core.LogEvent;
026import org.apache.logging.log4j.core.appender.AppenderLoggingException;
027import org.apache.logging.log4j.core.appender.ManagerFactory;
028import org.apache.logging.log4j.core.appender.db.AbstractDatabaseManager;
029import org.apache.logging.log4j.core.util.Closer;
030import org.apache.logging.log4j.message.MapMessage;
031import org.apache.logging.log4j.util.BiConsumer;
032import org.apache.logging.log4j.util.ReadOnlyStringMap;
033
034/**
035 * An {@link AbstractDatabaseManager} implementation for all NoSQL databases.
036 *
037 * @param <W> A type parameter for reassuring the compiler that all operations are using the same {@link NoSqlObject}.
038 */
039public final class NoSqlDatabaseManager<W> extends AbstractDatabaseManager {
040    private static final NoSQLDatabaseManagerFactory FACTORY = new NoSQLDatabaseManagerFactory();
041
042    private final NoSqlProvider<NoSqlConnection<W, ? extends NoSqlObject<W>>> provider;
043
044    private NoSqlConnection<W, ? extends NoSqlObject<W>> connection;
045
046    private NoSqlDatabaseManager(final String name, final int bufferSize,
047            final NoSqlProvider<NoSqlConnection<W, ? extends NoSqlObject<W>>> provider) {
048        super(name, bufferSize);
049        this.provider = provider;
050    }
051
052    @Override
053    protected void startupInternal() {
054        // nothing to see here
055    }
056
057    @Override
058    protected boolean shutdownInternal() {
059        // NoSQL doesn't use transactions, so all we need to do here is simply close the client
060        return Closer.closeSilently(this.connection);
061    }
062
063    @Override
064    protected void connectAndStart() {
065        try {
066            this.connection = this.provider.getConnection();
067        } catch (final Exception e) {
068            throw new AppenderLoggingException("Failed to get connection from NoSQL connection provider.", e);
069        }
070    }
071
072    @Deprecated
073    @Override
074    protected void writeInternal(final LogEvent event) {
075        writeInternal(event, null);
076    }
077    
078    @Override
079    protected void writeInternal(final LogEvent event, final Serializable serializable) {
080        if (!this.isRunning() || this.connection == null || this.connection.isClosed()) {
081            throw new AppenderLoggingException(
082                    "Cannot write logging event; NoSQL manager not connected to the database.");
083        }
084
085        final NoSqlObject<W> entity = this.connection.createObject();
086        if (serializable instanceof MapMessage) {
087            setFields((MapMessage<?, ?>) serializable, entity);
088        } else {
089            setFields(event, entity);
090        }
091
092        this.connection.insertObject(entity);
093    }
094
095    private void setFields(final MapMessage<?, ?> mapMessage, final NoSqlObject<W> noSqlObject) {
096        // Map without calling org.apache.logging.log4j.message.MapMessage#getData() which makes a copy of the map.
097        mapMessage.forEach(new BiConsumer<String, Object>() {
098            @Override
099            public void accept(final String key, final Object value) {
100                noSqlObject.set(key, value);
101            }
102        });
103    }
104
105    private void setFields(final LogEvent event, final NoSqlObject<W> entity) {
106        entity.set("level", event.getLevel());
107        entity.set("loggerName", event.getLoggerName());
108        entity.set("message", event.getMessage() == null ? null : event.getMessage().getFormattedMessage());
109
110        final StackTraceElement source = event.getSource();
111        if (source == null) {
112            entity.set("source", (Object) null);
113        } else {
114            entity.set("source", this.convertStackTraceElement(source));
115        }
116
117        final Marker marker = event.getMarker();
118        if (marker == null) {
119            entity.set("marker", (Object) null);
120        } else {
121            entity.set("marker", buildMarkerEntity(marker));
122        }
123
124        entity.set("threadId", event.getThreadId());
125        entity.set("threadName", event.getThreadName());
126        entity.set("threadPriority", event.getThreadPriority());
127        entity.set("millis", event.getTimeMillis());
128        entity.set("date", new java.util.Date(event.getTimeMillis()));
129
130        @SuppressWarnings("ThrowableResultOfMethodCallIgnored")
131        Throwable thrown = event.getThrown();
132        if (thrown == null) {
133            entity.set("thrown", (Object) null);
134        } else {
135            final NoSqlObject<W> originalExceptionEntity = this.connection.createObject();
136            NoSqlObject<W> exceptionEntity = originalExceptionEntity;
137            exceptionEntity.set("type", thrown.getClass().getName());
138            exceptionEntity.set("message", thrown.getMessage());
139            exceptionEntity.set("stackTrace", this.convertStackTrace(thrown.getStackTrace()));
140            while (thrown.getCause() != null) {
141                thrown = thrown.getCause();
142                final NoSqlObject<W> causingExceptionEntity = this.connection.createObject();
143                causingExceptionEntity.set("type", thrown.getClass().getName());
144                causingExceptionEntity.set("message", thrown.getMessage());
145                causingExceptionEntity.set("stackTrace", this.convertStackTrace(thrown.getStackTrace()));
146                exceptionEntity.set("cause", causingExceptionEntity);
147                exceptionEntity = causingExceptionEntity;
148            }
149
150            entity.set("thrown", originalExceptionEntity);
151        }
152
153        final ReadOnlyStringMap contextMap = event.getContextData();
154        if (contextMap == null) {
155            entity.set("contextMap", (Object) null);
156        } else {
157            final NoSqlObject<W> contextMapEntity = this.connection.createObject();
158            contextMap.forEach(new BiConsumer<String, String>() {
159                @Override
160                public void accept(final String key, final String val) {
161                    contextMapEntity.set(key, val);
162                }
163            });
164            entity.set("contextMap", contextMapEntity);
165        }
166
167        final ThreadContext.ContextStack contextStack = event.getContextStack();
168        if (contextStack == null) {
169            entity.set("contextStack", (Object) null);
170        } else {
171            entity.set("contextStack", contextStack.asList().toArray());
172        }
173    }
174
175    private NoSqlObject<W> buildMarkerEntity(final Marker marker) {
176        final NoSqlObject<W> entity = this.connection.createObject();
177        entity.set("name", marker.getName());
178
179        final Marker[] parents = marker.getParents();
180        if (parents != null) {
181            @SuppressWarnings("unchecked")
182            final NoSqlObject<W>[] parentEntities = new NoSqlObject[parents.length];
183            for (int i = 0; i < parents.length; i++) {
184                parentEntities[i] = buildMarkerEntity(parents[i]);
185            }
186            entity.set("parents", parentEntities);
187        }
188        return entity;
189    }
190
191    @Override
192    protected boolean commitAndClose() {
193        // all NoSQL drivers auto-commit (since NoSQL doesn't generally use the concept of transactions).
194        // also, all our NoSQL drivers use internal connection pooling and provide clients, not connections.
195        // thus, we should not be closing the client until shutdown as NoSQL is very different from SQL.
196        // see LOG4J2-591 and LOG4J2-676
197        return true;
198    }
199
200    private NoSqlObject<W>[] convertStackTrace(final StackTraceElement[] stackTrace) {
201        final NoSqlObject<W>[] stackTraceEntities = this.connection.createList(stackTrace.length);
202        for (int i = 0; i < stackTrace.length; i++) {
203            stackTraceEntities[i] = this.convertStackTraceElement(stackTrace[i]);
204        }
205        return stackTraceEntities;
206    }
207
208    private NoSqlObject<W> convertStackTraceElement(final StackTraceElement element) {
209        final NoSqlObject<W> elementEntity = this.connection.createObject();
210        elementEntity.set("className", element.getClassName());
211        elementEntity.set("methodName", element.getMethodName());
212        elementEntity.set("fileName", element.getFileName());
213        elementEntity.set("lineNumber", element.getLineNumber());
214        return elementEntity;
215    }
216
217    /**
218     * Creates a NoSQL manager for use within the {@link NoSqlAppender}, or returns a suitable one if it already exists.
219     *
220     * @param name The name of the manager, which should include connection details and hashed passwords where possible.
221     * @param bufferSize The size of the log event buffer.
222     * @param provider A provider instance which will be used to obtain connections to the chosen NoSQL database.
223     * @return a new or existing NoSQL manager as applicable.
224     */
225    public static NoSqlDatabaseManager<?> getNoSqlDatabaseManager(final String name, final int bufferSize,
226                                                                  final NoSqlProvider<?> provider) {
227        return AbstractDatabaseManager.getManager(name, new FactoryData(bufferSize, provider), FACTORY);
228    }
229
230    /**
231     * Encapsulates data that {@link NoSQLDatabaseManagerFactory} uses to create managers.
232     */
233    private static final class FactoryData extends AbstractDatabaseManager.AbstractFactoryData {
234        private final NoSqlProvider<?> provider;
235
236        protected FactoryData(final int bufferSize, final NoSqlProvider<?> provider) {
237            super(bufferSize, null);
238            this.provider = provider;
239        }
240    }
241
242    /**
243     * Creates managers.
244     */
245    private static final class NoSQLDatabaseManagerFactory implements
246            ManagerFactory<NoSqlDatabaseManager<?>, FactoryData> {
247        @Override
248        @SuppressWarnings("unchecked")
249        public NoSqlDatabaseManager<?> createManager(final String name, final FactoryData data) {
250            return new NoSqlDatabaseManager(name, data.getBufferSize(), data.provider);
251        }
252    }
253}