0c96dabbea57be8e88324ad7e3748c4e61e49ce0
[so.git] / common / src / main / java / org / openecomp / mso / logger / MsoLogInitializer.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * OPENECOMP - MSO
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
10  * 
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  * 
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=========================================================
19  */
20
21 package org.openecomp.mso.logger;
22
23
24 import java.io.File;
25
26 import javax.servlet.ServletContextEvent;
27 import javax.servlet.ServletContextListener;
28 import javax.servlet.annotation.WebListener;
29
30 import org.slf4j.LoggerFactory;
31
32 import ch.qos.logback.classic.LoggerContext;
33 import ch.qos.logback.classic.joran.JoranConfigurator;
34
35
36 /**
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 
39  * following order:
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)
45  * 
46  *
47  */
48 @WebListener
49 public class MsoLogInitializer implements ServletContextListener
50 {
51         private static String DEFAULT_LOG4J_PROPERTIES_FILE = "/etc/ecomp/mso/config/logback.xml";
52         private static String prefixMsoPropertiesPath = System.getProperty("mso.config.path");
53         static  {
54                 if (prefixMsoPropertiesPath == null) {
55                         prefixMsoPropertiesPath = "/";
56                 } else if (!(prefixMsoPropertiesPath.charAt(prefixMsoPropertiesPath.length() - 1) == '/')) {
57                         prefixMsoPropertiesPath = prefixMsoPropertiesPath + "/";
58                 }
59         }
60         
61         public MsoLogInitializer () {
62         }
63
64
65         @Override
66         public void contextDestroyed(ServletContextEvent event) {
67                 // Nothing to do...
68         }
69
70         @Override
71         public void contextInitialized(ServletContextEvent event)
72         {
73                 String logPropertiesFile = null;
74                 try {
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;
79                         }
80                         else if (fileIsReadable(DEFAULT_LOG4J_PROPERTIES_FILE)) {
81                                 logPropertiesFile = DEFAULT_LOG4J_PROPERTIES_FILE;
82                         }
83                         
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);                 
90                                 context.reset(); 
91                                 jc.doConfigure(logPropertiesFile);
92                                 // Try it out
93                                 MsoLogger initLogger = MsoLogger.getMsoLogger(MsoLogger.Catalog.GENERAL);
94                                 initLogger.info(MessageEnum.INIT_LOGGER, logPropertiesFile, "", "");
95                         }
96                 } catch (Exception e) {
97                         MsoLogger initLogger = MsoLogger.getMsoLogger(MsoLogger.Catalog.GENERAL);
98                         initLogger.error (MessageEnum.INIT_LOGGER_FAIL, "", "", MsoLogger.ErrorCode.UnknownError, "", e);
99                 }
100         }
101         
102         private boolean fileIsReadable (String filePath) {
103                 File f = new File(filePath);
104                 if (f.exists() && f.canRead())
105                         return true;
106                 return false;
107         }
108 }