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.logback.listener;
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;
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;
43 * Logback Startup Listener is used to inject {@link AppLogInfo}
45 * @author Rajiv Singla
47 public class LogbackStartupListener extends ContextAwareBase implements LoggerContextListener, LifeCycle {
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";
56 private boolean started = false;
59 public boolean isResetResistant() {
64 public void onStart(final LoggerContext context) {
69 public void onReset(final LoggerContext context) {
74 public void onStop(final LoggerContext context) {
79 public void onLevelChange(final Logger logger, final Level level) {
89 Context context = getContext();
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));
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));
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);
125 public boolean isStarted() {