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     */
017    package org.apache.logging.log4j.core.appender.db.nosql;
018    
019    import java.util.Map;
020    
021    import org.apache.logging.log4j.Marker;
022    import org.apache.logging.log4j.ThreadContext;
023    import org.apache.logging.log4j.core.LogEvent;
024    import org.apache.logging.log4j.core.appender.ManagerFactory;
025    import org.apache.logging.log4j.core.appender.db.AbstractDatabaseManager;
026    
027    /**
028     * An {@link AbstractDatabaseManager} implementation for all NoSQL databases.
029     * 
030     * @param <W> A type parameter for reassuring the compiler that all operations are using the same {@link NoSQLObject}.
031     */
032    public final class NoSQLDatabaseManager<W> extends AbstractDatabaseManager {
033        private static final NoSQLDatabaseManagerFactory FACTORY = new NoSQLDatabaseManagerFactory();
034    
035        private final NoSQLProvider<NoSQLConnection<W, ? extends NoSQLObject<W>>> provider;
036    
037        private NoSQLConnection<W, ? extends NoSQLObject<W>> connection;
038    
039        private NoSQLDatabaseManager(final String name, final int bufferSize,
040                final NoSQLProvider<NoSQLConnection<W, ? extends NoSQLObject<W>>> provider) {
041            super(name, bufferSize);
042            this.provider = provider;
043        }
044    
045        @Override
046        protected void connectInternal() {
047            try {
048                this.connection = this.provider.getConnection();
049            } catch (final Exception e) {
050                LOGGER.error("Failed to obtain a connection to the NoSQL database in manager [{}].", this.getName(), e);
051            }
052        }
053    
054        @Override
055        protected void disconnectInternal() {
056            try {
057                if (this.connection != null && !this.connection.isClosed()) {
058                    this.connection.close();
059                }
060            } catch (final Exception e) {
061                LOGGER.warn("Error while closing NoSQL database connection in manager [{}].", this.getName(), e);
062            }
063        }
064    
065        @Override
066        protected void writeInternal(final LogEvent event) {
067            if (!this.isConnected() || this.connection == null || this.connection.isClosed()) {
068                LOGGER.error("Cannot write logging event; manager [{}] not connected to the database.", this.getName());
069                return;
070            }
071    
072            final NoSQLObject<W> entity = this.connection.createObject();
073            entity.set("level", event.getLevel());
074            entity.set("loggerName", event.getLoggerName());
075            entity.set("message", event.getMessage() == null ? null : event.getMessage().getFormattedMessage());
076    
077            final StackTraceElement source = event.getSource();
078            if (source == null) {
079                entity.set("source", (Object) null);
080            } else {
081                entity.set("source", this.convertStackTraceElement(source));
082            }
083    
084            Marker marker = event.getMarker();
085            if (marker == null) {
086                entity.set("marker", (Object) null);
087            } else {
088                final NoSQLObject<W> originalMarkerEntity = this.connection.createObject();
089                NoSQLObject<W> markerEntity = originalMarkerEntity;
090                markerEntity.set("name", marker.getName());
091                while (marker.getParent() != null) {
092                    marker = marker.getParent();
093                    final NoSQLObject<W> parentMarkerEntity = this.connection.createObject();
094                    parentMarkerEntity.set("name", marker.getName());
095                    markerEntity.set("parent", parentMarkerEntity);
096                    markerEntity = parentMarkerEntity;
097                }
098                entity.set("marker", originalMarkerEntity);
099            }
100    
101            entity.set("threadName", event.getThreadName());
102            entity.set("millis", event.getMillis());
103            entity.set("date", new java.util.Date(event.getMillis()));
104    
105            @SuppressWarnings("ThrowableResultOfMethodCallIgnored")
106            Throwable thrown = event.getThrown();
107            if (thrown == null) {
108                entity.set("thrown", (Object) null);
109            } else {
110                final NoSQLObject<W> originalExceptionEntity = this.connection.createObject();
111                NoSQLObject<W> exceptionEntity = originalExceptionEntity;
112                exceptionEntity.set("type", thrown.getClass().getName());
113                exceptionEntity.set("message", thrown.getMessage());
114                exceptionEntity.set("stackTrace", this.convertStackTrace(thrown.getStackTrace()));
115                while (thrown.getCause() != null) {
116                    thrown = thrown.getCause();
117                    final NoSQLObject<W> causingExceptionEntity = this.connection.createObject();
118                    causingExceptionEntity.set("type", thrown.getClass().getName());
119                    causingExceptionEntity.set("message", thrown.getMessage());
120                    causingExceptionEntity.set("stackTrace", this.convertStackTrace(thrown.getStackTrace()));
121                    exceptionEntity.set("cause", causingExceptionEntity);
122                    exceptionEntity = causingExceptionEntity;
123                }
124    
125                entity.set("thrown", originalExceptionEntity);
126            }
127    
128            final Map<String, String> contextMap = event.getContextMap();
129            if (contextMap == null) {
130                entity.set("contextMap", (Object) null);
131            } else {
132                final NoSQLObject<W> contextMapEntity = this.connection.createObject();
133                for (final Map.Entry<String, String> entry : contextMap.entrySet()) {
134                    contextMapEntity.set(entry.getKey(), entry.getValue());
135                }
136                entity.set("contextMap", contextMapEntity);
137            }
138    
139            final ThreadContext.ContextStack contextStack = event.getContextStack();
140            if (contextStack == null) {
141                entity.set("contextStack", (Object) null);
142            } else {
143                entity.set("contextStack", contextStack.asList().toArray());
144            }
145    
146            this.connection.insertObject(entity);
147        }
148    
149        private NoSQLObject<W>[] convertStackTrace(final StackTraceElement[] stackTrace) {
150            final NoSQLObject<W>[] stackTraceEntities = this.connection.createList(stackTrace.length);
151            for (int i = 0; i < stackTrace.length; i++) {
152                stackTraceEntities[i] = this.convertStackTraceElement(stackTrace[i]);
153            }
154            return stackTraceEntities;
155        }
156    
157        private NoSQLObject<W> convertStackTraceElement(final StackTraceElement element) {
158            final NoSQLObject<W> elementEntity = this.connection.createObject();
159            elementEntity.set("className", element.getClassName());
160            elementEntity.set("methodName", element.getMethodName());
161            elementEntity.set("fileName", element.getFileName());
162            elementEntity.set("lineNumber", element.getLineNumber());
163            return elementEntity;
164        }
165    
166        /**
167         * Creates a NoSQL manager for use within the {@link NoSQLAppender}, or returns a suitable one if it already exists.
168         *
169         * @param name The name of the manager, which should include connection details and hashed passwords where possible.
170         * @param bufferSize The size of the log event buffer.
171         * @param provider A provider instance which will be used to obtain connections to the chosen NoSQL database.
172         * @return a new or existing NoSQL manager as applicable.
173         */
174        public static NoSQLDatabaseManager<?> getNoSQLDatabaseManager(final String name, final int bufferSize,
175                                                                      final NoSQLProvider<?> provider) {
176            return AbstractDatabaseManager.getManager(name, new FactoryData(bufferSize, provider), FACTORY);
177        }
178    
179        /**
180         * Encapsulates data that {@link NoSQLDatabaseManagerFactory} uses to create managers.
181         */
182        private static final class FactoryData extends AbstractDatabaseManager.AbstractFactoryData {
183            private final NoSQLProvider<?> provider;
184    
185            protected FactoryData(final int bufferSize, final NoSQLProvider<?> provider) {
186                super(bufferSize);
187                this.provider = provider;
188            }
189        }
190    
191        /**
192         * Creates managers.
193         */
194        private static final class NoSQLDatabaseManagerFactory implements
195                ManagerFactory<NoSQLDatabaseManager<?>, FactoryData> {
196            @Override
197            @SuppressWarnings("unchecked")
198            public NoSQLDatabaseManager<?> createManager(final String name, final FactoryData data) {
199                return new NoSQLDatabaseManager(name, data.getBufferSize(), data.provider);
200            }
201        }
202    }