001: /*
002: * Copyright (c) 2006, WSO2 Inc. (http://www.wso2.org) All Rights Reserved.
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License");
005: * you may not use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: */
016: package org.wso2.esb.services;
017:
018: import org.apache.axis2.AxisFault;
019: import org.apache.commons.logging.Log;
020: import org.apache.commons.logging.LogFactory;
021: import org.apache.log4j.Level;
022: import org.apache.log4j.LogManager;
023: import org.apache.log4j.Logger;
024: import org.apache.log4j.PropertyConfigurator;
025: import org.apache.synapse.SynapseConstants;
026: import org.wso2.esb.services.tos.LogConfigData;
027:
028: import java.io.*;
029: import java.util.Enumeration;
030: import java.util.HashMap;
031: import java.util.Map;
032: import java.util.Properties;
033:
034: /**
035: * This is a POJO for the log configuration
036: */
037: public class LogAdmin extends AbstractESBAdmin {
038: private static final Log log = LogFactory.getLog(LogAdmin.class);
039:
040: // these are the loggers we give users to change logging levels
041: // from the log settings interface
042: private static String LOGGER_ROOT_ID = "loggerRoot";
043:
044: private static String LOGGER_SERVICE_ID = "loggerService";
045: private static String LOGGER_SERVICE_NAME = "SERVICE_LOGGER";
046: private static String LOGGER_TRACE_ID = "loggerTrace";
047: private static String LOGGER_TRACE_NAME = "TRACE_LOGGER";
048: private static Map loggerMap = new HashMap();
049:
050: // logger map (key=id value=logger name)
051: static {
052: loggerMap.put(LOGGER_SERVICE_ID, LOGGER_SERVICE_NAME);
053: loggerMap.put(LOGGER_TRACE_ID, LOGGER_TRACE_NAME);
054: }
055:
056: /**
057: * Get log4j.properties, logger levels
058: * read from the file system.
059: *
060: * @return
061: * @throws AxisFault
062: */
063: public LogConfigData getLogConfig() throws AxisFault {
064: try {
065: log.info("get log configuration");
066:
067: // initialized to blank so that going xml will have <x></x>
068: // instead of null elements
069: String loggerLevelRoot = "";
070: String loggerLevelService = "";
071: String loggerLevelTrace = "";
072:
073: String logConfigLocation = getLogConfigLocation();
074:
075: // read the log4j.properties file
076: StringBuffer logConfig = new StringBuffer(1024);
077: BufferedReader reader = new BufferedReader(new FileReader(
078: logConfigLocation));
079: String line = null;
080: while ((line = reader.readLine()) != null) {
081: line = line.trim();
082: logConfig.append(line + "\n");
083: }
084: reader.close();
085:
086: // find current logger levels
087: Logger rootLogger = Logger.getRootLogger();
088: if (rootLogger != null) {
089: if (rootLogger.getLevel() != null)
090: loggerLevelRoot = rootLogger.getLevel().toString();
091: }
092:
093: Enumeration loggers = LogManager.getCurrentLoggers();
094: while (loggers.hasMoreElements()) {
095: Logger logger = (Logger) loggers.nextElement();
096: if (LOGGER_SERVICE_NAME.equals(logger.getName())) {
097: if (logger.getLevel() != null)
098: loggerLevelService = logger.getLevel()
099: .toString();
100: } else if (LOGGER_TRACE_NAME.equals(logger.getName())) {
101: if (logger.getLevel() != null)
102: loggerLevelTrace = logger.getLevel().toString();
103: }
104: }
105:
106: // data are log4j.properties file + selected current logger levels
107: LogConfigData logConfigData = new LogConfigData();
108: logConfigData.setLoggerLevelRoot(loggerLevelRoot);
109: logConfigData.setLoggerLevelService(loggerLevelService);
110: logConfigData.setLoggerLevelTrace(loggerLevelTrace);
111: logConfigData.setLogText(logConfig.toString());
112:
113: return logConfigData;
114:
115: } catch (IOException e) {
116: handleFault(log, "log4j.properties read error:"
117: + e.getMessage(), e);
118: return null;
119: }
120: }
121:
122: /**
123: * Update the running log4j with the given log4j.properties.
124: *
125: * @param logConfig log4j.properties contents
126: * @throws AxisFault
127: */
128: public boolean updateLogConfig(String logConfig) throws AxisFault {
129: InputStream is = new ByteArrayInputStream(logConfig.getBytes());
130: Properties properties = new Properties();
131: try {
132: properties.load(is);
133:
134: } catch (IOException e) {
135: handleFault(log, "log4j.properties update error:"
136: + e.getMessage(), e);
137: return false;
138: }
139:
140: // update runtime log4j
141: PropertyConfigurator.configure(properties);
142: log.info("Log configuration updated");
143: return true;
144: }
145:
146: /**
147: * Save given log4j.properties to default location.
148: *
149: * @param logConfig log4j.properties contents
150: * @throws AxisFault
151: */
152: public boolean saveLogConfig(String logConfig) throws AxisFault {
153: if (logConfig == null) {
154: log.error("saving log configuration called with null");
155: return false;
156: }
157:
158: String logConfigurationLocation = getLogConfigLocation();
159:
160: try {
161: OutputStream out = new java.io.FileOutputStream(new File(
162: logConfigurationLocation));
163: out.write(logConfig.getBytes());
164: out.close();
165:
166: } catch (IOException e) {
167: handleFault(log, "log4j.properties write error:"
168: + e.getMessage(), e);
169: return false;
170: }
171:
172: // update runtime log4j
173: PropertyConfigurator.configure(logConfigurationLocation);
174: log.info("log4j.properties saved");
175: return true;
176: }
177:
178: /**
179: * Get log4j.properties location
180: * @return
181: */
182: private String getLogConfigLocation() {
183: String logConfigurationXMLLocation = "conf/log4j.properties";
184:
185: // from synapse home
186: String synapseHome = System
187: .getProperty(SynapseConstants.SYNAPSE_HOME);
188: if (synapseHome != null) {
189: logConfigurationXMLLocation = synapseHome
190: + "/webapp/WEB-INF/classes/conf/log4j.properties";
191:
192: } else {
193: log
194: .warn("synapse.home not set while reading log configuration");
195: }
196: return logConfigurationXMLLocation;
197: }
198:
199: /**
200: * Change logger level in the log4j.properties file.
201: *
202: * @param loggerId logger ID from the loggerMap object
203: * @param level e.g. INFO, DEBUG
204: * @throws AxisFault
205: */
206: public boolean changeLogConfigLevel(String loggerId, String level)
207: throws AxisFault {
208: // find the logger name from id
209: log.info("change log level loggerId=" + loggerId + " level="
210: + level);
211:
212: // get logger and change level
213: Logger logger = null;
214: if (LOGGER_ROOT_ID.equals(loggerId)) {
215: logger = Logger.getRootLogger();
216:
217: } else {
218: String loggerName = (String) loggerMap.get(loggerId);
219: if (loggerName == null) {
220: log.error("logger id not found:" + level);
221: return false;
222: }
223:
224: logger = Logger.getLogger(loggerName);
225: }
226: if (logger != null)
227: logger.setLevel(Level.toLevel(level));
228:
229: return true;
230: }
231:
232: }
|