2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
6 * ================================================================================
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
11 * http://www.apache.org/licenses/LICENSE-2.0
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
18 * ============LICENSE_END=========================================================
21 package org.openecomp.mso.logger;
26 import javax.servlet.ServletContextEvent;
27 import javax.servlet.ServletContextListener;
28 import javax.servlet.annotation.WebListener;
30 import org.slf4j.LoggerFactory;
32 import ch.qos.logback.classic.LoggerContext;
33 import ch.qos.logback.classic.joran.JoranConfigurator;
37 * This class will attempt to initialize MSO log4j when part of a web application.
38 * It will look for the logback configuration file logback.xml in the
40 * 1. In an init-param "log.configuration" in web.xml
41 * 2. TODO: In a property "log.configuration" in an "application.properties" file
42 * 3. In the default location "/etc/ecomp/mso/config"
43 * 4. Using the log4j default (check system property log.configuration or
44 * just look on the classpath for logback.xml)
49 public class MsoLogInitializer implements ServletContextListener
51 private static String DEFAULT_LOG4J_PROPERTIES_FILE = "/etc/ecomp/mso/config/logback.xml";
52 private static String prefixMsoPropertiesPath = System.getProperty("mso.config.path");
54 if (prefixMsoPropertiesPath == null) {
55 prefixMsoPropertiesPath = "/";
56 } else if (!(prefixMsoPropertiesPath.charAt(prefixMsoPropertiesPath.length() - 1) == '/')) {
57 prefixMsoPropertiesPath = prefixMsoPropertiesPath + "/";
61 public MsoLogInitializer () {
66 public void contextDestroyed(ServletContextEvent event) {
71 public void contextInitialized(ServletContextEvent event)
73 String logPropertiesFile = null;
75 // Look first in the init-parameters
76 String initParam = event.getServletContext().getInitParameter("log.configuration");
77 if (initParam != null && fileIsReadable(prefixMsoPropertiesPath + initParam)) {
78 logPropertiesFile = prefixMsoPropertiesPath + initParam;
80 else if (fileIsReadable(DEFAULT_LOG4J_PROPERTIES_FILE)) {
81 logPropertiesFile = DEFAULT_LOG4J_PROPERTIES_FILE;
84 if (logPropertiesFile != null) {
85 // Initialize loggers with the specified file. If no such file was
86 // specified, will use the default Log4j resolution.
87 LoggerContext context = (LoggerContext) LoggerFactory.getILoggerFactory();
88 JoranConfigurator jc = new JoranConfigurator();
89 jc.setContext(context);
91 jc.doConfigure(logPropertiesFile);
93 MsoLogger initLogger = MsoLogger.getMsoLogger(MsoLogger.Catalog.GENERAL);
94 initLogger.info(MessageEnum.INIT_LOGGER, logPropertiesFile, "", "");
96 } catch (Exception e) {
97 MsoLogger initLogger = MsoLogger.getMsoLogger(MsoLogger.Catalog.GENERAL);
98 initLogger.error (MessageEnum.INIT_LOGGER_FAIL, "", "", MsoLogger.ErrorCode.UnknownError, "", e);
102 private boolean fileIsReadable (String filePath) {
103 File f = new File(filePath);
104 if (f.exists() && f.canRead())