Standalone TCA with EELF Logger
[dcaegen2/analytics/tca-gen2.git] / eelf-logger / eelf-logger-logback-impl / src / main / java / org / onap / dcae / utils / eelf / logger / logback / listener / LogbackStartupListener.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.logback.listener;
21
22 import ch.qos.logback.classic.Level;
23 import ch.qos.logback.classic.Logger;
24 import ch.qos.logback.classic.LoggerContext;
25 import ch.qos.logback.classic.spi.LoggerContextListener;
26 import ch.qos.logback.core.Context;
27 import ch.qos.logback.core.spi.ContextAwareBase;
28 import ch.qos.logback.core.spi.LifeCycle;
29 import ch.qos.logback.core.status.InfoStatus;
30 import ch.qos.logback.core.status.WarnStatus;
31 import org.onap.dcae.utils.eelf.logger.api.info.AppLogInfo;
32 import org.onap.dcae.utils.eelf.logger.model.spec.AppLogSpecImpl;
33
34
35 import java.io.File;
36
37 import static org.onap.dcae.utils.eelf.logger.logback.EELFLoggerDefaults.DEFAULT_APP_LOG_INFO;
38 import static org.onap.dcae.utils.eelf.logger.logback.utils.LogUtils.APP_LOG_SPEC_KEY;
39 import static org.onap.dcae.utils.eelf.logger.logback.utils.LogUtils.CUSTOM_MDC_MAP;
40
41
42 /**
43  * Logback Startup Listener is used to inject {@link AppLogInfo}
44  *
45  * @author Rajiv Singla
46  */
47 public class LogbackStartupListener extends ContextAwareBase implements LoggerContextListener, LifeCycle {
48
49     private static final String APP_LOG_INFO_KEY_IN_CONTEXT = "APP_LOG_INFO";
50     private static final String CONTEXT_COMPONENT_NAME_PROPERTY_KEY = "componentName";
51     private static final String CONTEXT_MODIFY_WINDOWS_LOG_PATH_PROPERTY_KEY = "modifyLogPathInWindows";
52     private static final String CONTEXT_LOG_DIRECTORY_PROPERTY_KEY = "logDirectory";
53     private static final String CONTEXT_DEBUG_LOG_DIRECTORY_PROPERTY_KEY = "debugLogDirectory";
54     private static final String CONTEXT_APPEND_DIRECTORY_PROPERTY_KEY = "appendDirectory";
55
56     private boolean started = false;
57
58     @Override
59     public boolean isResetResistant() {
60         return true;
61     }
62
63     @Override
64     public void onStart(final LoggerContext context) {
65
66     }
67
68     @Override
69     public void onReset(final LoggerContext context) {
70
71     }
72
73     @Override
74     public void onStop(final LoggerContext context) {
75
76     }
77
78     @Override
79     public void onLevelChange(final Logger logger, final Level level) {
80
81     }
82
83     @Override
84     public void start() {
85         if (started) {
86             return;
87         }
88
89         Context context = getContext();
90
91         CUSTOM_MDC_MAP.put(APP_LOG_SPEC_KEY, new AppLogSpecImpl(DEFAULT_APP_LOG_INFO));
92         context.putObject(APP_LOG_INFO_KEY_IN_CONTEXT, DEFAULT_APP_LOG_INFO);
93         context.getStatusManager().add(new InfoStatus("Initialized APP LOG INFO", DEFAULT_APP_LOG_INFO));
94
95         // if Component name is not set derive from jar file name
96         if (context.getProperty(CONTEXT_COMPONENT_NAME_PROPERTY_KEY) == null) {
97             final String appName =
98                     new File(getClass().getProtectionDomain().getCodeSource().getLocation().getPath()).getName();
99             context.putProperty(CONTEXT_COMPONENT_NAME_PROPERTY_KEY, appName);
100             context.getStatusManager().add(new WarnStatus(
101                     "No componentName Property found. Deriving it from jar name", appName));
102         }
103
104         // if modify windows log path is enabled - then append target before log directories
105         if (context.getProperty(CONTEXT_MODIFY_WINDOWS_LOG_PATH_PROPERTY_KEY).equalsIgnoreCase("true")) {
106             final String os = System.getProperty("os.name");
107             if (os != null && os.startsWith("Windows")) {
108                 final String logDirectory = context.getProperty(CONTEXT_LOG_DIRECTORY_PROPERTY_KEY);
109                 final String debugLogDirectory = context.getProperty(CONTEXT_DEBUG_LOG_DIRECTORY_PROPERTY_KEY);
110                 final String appendDir = context.getProperty(CONTEXT_APPEND_DIRECTORY_PROPERTY_KEY);
111                 context.putProperty(CONTEXT_LOG_DIRECTORY_PROPERTY_KEY, appendDir + "/" + logDirectory);
112                 context.putProperty(CONTEXT_DEBUG_LOG_DIRECTORY_PROPERTY_KEY, appendDir + "/" + debugLogDirectory);
113             }
114         }
115
116         started = true;
117     }
118
119     @Override
120     public void stop() {
121
122     }
123
124     @Override
125     public boolean isStarted() {
126         return started;
127     }
128 }