2 * ================================================================================
3 * Copyright (c) 2018 AT&T Intellectual Property. All rights reserved.
4 * ================================================================================
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
9 * http://www.apache.org/licenses/LICENSE-2.0
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 * ============LICENSE_END=========================================================
20 package org.onap.dcae.utils.eelf.logger.api.log;
23 import org.onap.dcae.utils.eelf.logger.api.noop.NoOpEELFLogger;
25 import java.lang.reflect.InvocationTargetException;
26 import java.util.LinkedList;
27 import java.util.List;
29 import java.util.ServiceLoader;
30 import java.util.WeakHashMap;
33 * EELF Log Factory uses {@link ServiceLoader} to load implementations of {@link EELFLogger} in application
34 * classpath. If no EELF Logging implementations are found - eelf logging is disabled and {@link NoOpEELFLogger}
37 * @author Rajiv Singla
39 public class EELFLogFactory {
41 private static final ServiceLoader<EELFLogger> SERVICE_LOADER = ServiceLoader.load(EELFLogger.class);
42 private static final Map<Class<?>, EELFLogger> LOGGER_CACHE = new WeakHashMap<>(64);
44 private static List<EELFLogger> loggerImplementations = new LinkedList<>();
48 for (EELFLogger loggerImplementation : SERVICE_LOADER) {
49 loggerImplementations.add(loggerImplementation);
52 if (loggerImplementations.isEmpty()) {
54 "EELF LOGGING ERROR: Unable to find any EELF Logger Implementations in the classpath.");
55 System.err.println("=============EELF LOGGING IS DISABLED================");
56 loggerImplementations.add(NoOpEELFLogger.getInstance());
61 private EELFLogFactory() {
62 // private constructor
65 public static <T> EELFLogger getLogger(Class<T> clazz) {
67 if (LOGGER_CACHE.get(clazz) != null) {
68 return LOGGER_CACHE.get(clazz);
71 EELFLogger EELFLogger = null;
73 EELFLogger = loggerImplementations.get(0).getClass().getConstructor(Class.class)
78 InstantiationException | IllegalAccessException | InvocationTargetException | NoSuchMethodException e) {
79 throw new IllegalStateException("Error while Creating EELF Logger", e);