Standalone TCA with EELF Logger
[dcaegen2/analytics/tca-gen2.git] / eelf-logger / eelf-logger-api / src / main / java / org / onap / dcae / utils / eelf / logger / api / log / EELFLogFactory.java
1 /*
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
8  *
9  *      http://www.apache.org/licenses/LICENSE-2.0
10  *
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=========================================================
17  *
18  */
19
20 package org.onap.dcae.utils.eelf.logger.api.log;
21
22
23 import org.onap.dcae.utils.eelf.logger.api.noop.NoOpEELFLogger;
24
25 import java.lang.reflect.InvocationTargetException;
26 import java.util.LinkedList;
27 import java.util.List;
28 import java.util.Map;
29 import java.util.ServiceLoader;
30 import java.util.WeakHashMap;
31
32 /**
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}
35  * is used.
36  *
37  * @author Rajiv Singla
38  */
39 public class EELFLogFactory {
40
41     private static final ServiceLoader<EELFLogger> SERVICE_LOADER = ServiceLoader.load(EELFLogger.class);
42     private static final Map<Class<?>, EELFLogger> LOGGER_CACHE = new WeakHashMap<>(64);
43
44     private static List<EELFLogger> loggerImplementations = new LinkedList<>();
45
46     static {
47
48         for (EELFLogger loggerImplementation : SERVICE_LOADER) {
49             loggerImplementations.add(loggerImplementation);
50         }
51
52         if (loggerImplementations.isEmpty()) {
53             System.err.println(
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());
57         }
58
59     }
60
61     private EELFLogFactory() {
62         // private constructor
63     }
64
65     public static <T> EELFLogger getLogger(Class<T> clazz) {
66
67         if (LOGGER_CACHE.get(clazz) != null) {
68             return LOGGER_CACHE.get(clazz);
69         }
70
71         EELFLogger EELFLogger = null;
72         try {
73             EELFLogger = loggerImplementations.get(0).getClass().getConstructor(Class.class)
74                     .newInstance(clazz);
75             return EELFLogger;
76
77         } catch (
78                 InstantiationException | IllegalAccessException | InvocationTargetException | NoSuchMethodException e) {
79             throw new IllegalStateException("Error while Creating EELF Logger", e);
80         }
81
82     }
83
84
85 }