001: /*
002: * <copyright>
003: *
004: * Copyright 2001-2004 BBNT Solutions, LLC
005: * under sponsorship of the Defense Advanced Research Projects
006: * Agency (DARPA).
007: *
008: * You can redistribute this software and/or modify it under the
009: * terms of the Cougaar Open Source License as published on the
010: * Cougaar Open Source Website (www.cougaar.org).
011: *
012: * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
013: * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
014: * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
015: * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
016: * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
017: * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
018: * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
019: * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
020: * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
021: * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
022: * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
023: *
024: * </copyright>
025: */
026:
027: package org.cougaar.util.log.log4j;
028:
029: import java.io.OutputStream;
030: import java.util.Enumeration;
031: import java.util.HashSet;
032: import java.util.Vector;
033:
034: import org.apache.log4j.Appender;
035: import org.apache.log4j.ConsoleAppender;
036: import org.apache.log4j.FileAppender;
037: import org.apache.log4j.Level;
038: import org.apache.log4j.LogManager;
039: import org.apache.log4j.Logger;
040: import org.apache.log4j.WriterAppender;
041: import org.cougaar.util.log.LogTarget;
042: import org.cougaar.util.log.LoggerController;
043:
044: /**
045: * Package-private log4j implementation of a logger-controller.
046: * <p>
047: * Note that log4j uses static methods to control its logging,
048: * such as "Logger.*" methods.
049: */
050: class LoggerControllerImpl implements LoggerController {
051:
052: private String name;
053: private Logger cat;
054:
055: public LoggerControllerImpl() {
056: // must get a logger-controller!
057: }
058:
059: public LoggerControllerImpl(String name) {
060: this .name = name;
061: if ("root".equals(name)) {
062: this .cat = Logger.getRootLogger();
063: } else {
064: this .cat = Logger.getLogger(name);
065: }
066: }
067:
068: /**
069: * Special "static" method to get a named logger.
070: * <p>
071: * This should be moved to a different interface...
072: */
073: public LoggerController getLoggerController(String o) {
074: return new LoggerControllerImpl(o);
075: }
076:
077: /**
078: * Special "static" method to get the names of all loggers.
079: * <p>
080: * This should be moved to a different interface...
081: */
082: public Enumeration getAllLoggerNames() {
083: HashSet s = new HashSet();
084: Enumeration cats = LogManager.getCurrentLoggers();
085: while (cats.hasMoreElements()) {
086: Logger c = (Logger) cats.nextElement();
087: while (c != null) {
088: Enumeration appenders = c.getAllAppenders();
089: if (appenders.hasMoreElements()) {
090: s.add(c.getName());
091: }
092: Logger rootC = Logger.getRootLogger();
093: if (c == rootC) {
094: c = null;
095: } else {
096: c = rootC;
097: }
098: }
099: }
100: Vector v = new Vector(s);
101: return v.elements();
102: }
103:
104: /**
105: * Get the logging level.
106: *
107: * @return a Logger level constant (DEBUG, INFO, etc)
108: *
109: * @see Logger
110: */
111: public int getLoggingLevel() {
112: Level p = cat.getEffectiveLevel();
113: return Util.convertLevelToInt(p);
114: }
115:
116: /**
117: * Set the logging level.
118: *
119: * @param a Logger level constant (DEBUG, INFO, etc)
120: *
121: * @see Logger
122: */
123: public void setLoggingLevel(int level) {
124: Level p = Util.convertIntToLevel(level);
125: cat.setLevel(p);
126: }
127:
128: /**
129: * Get an array of all LogTargets for this logger.
130: *
131: * return an array of {@link LogTarget} representing all
132: * the various logging destinations.
133: */
134: public LogTarget[] getLogTargets() {
135: Level p = cat.getEffectiveLevel();
136: int loggingLevel = Util.convertLevelToInt(p);
137: Enumeration appenders = cat.getAllAppenders();
138:
139: Vector outputs = new Vector();
140: while (appenders.hasMoreElements()) {
141: Appender a = (Appender) appenders.nextElement();
142: int outputType;
143: String outputDevice;
144: if (a instanceof FileAppender) {
145: outputType = LogTarget.FILE;
146: outputDevice = ((FileAppender) a).getFile();
147: } else if (a instanceof ConsoleAppender) {
148: outputType = LogTarget.CONSOLE;
149: outputDevice = null;
150: } else if (a.getClass() == WriterAppender.class) {
151: outputType = LogTarget.STREAM;
152: outputDevice = a.getName();
153: } else {
154: throw new RuntimeException("Unknown Appender type");
155: }
156: LogTarget lt = new LogTarget(this .name, outputType,
157: outputDevice, loggingLevel);
158: outputs.addElement(lt);
159: }
160:
161: int n = outputs.size();
162: LogTarget[] lta = new LogTarget[n];
163: for (int i = 0; i < n; i++) {
164: lta[i] = (LogTarget) outputs.elementAt(i);
165: }
166:
167: return lta;
168: }
169:
170: /**
171: * Add a logging destination.
172: *
173: * @param outputType The output type either CONSOLE, FILE, or STREAM. See
174: * constants above.
175: * @param outputDevice The device associated with the particular output
176: * type being added. Null for Console, filename for FILE, the actual
177: * output stream object for STREAM.
178: *
179: */
180: public void addLogTarget(int outputType, Object outputDevice) {
181: Appender a = Util
182: .convertIntToAppender(outputType, outputDevice);
183: cat.addAppender(a);
184: }
185:
186: /**
187: * Add a console output type to this logger.
188: * <p>
189: * This is equivalent to:<pre>
190: * addLogTarget(LogTarget.CONSOLE, null);</pre>
191: */
192: public void addConsole() {
193: addLogTarget(LogTarget.CONSOLE, null);
194: }
195:
196: /**
197: * Remove a logging output type.
198: *
199: * @param outputType The output type either CONSOLE, FILE, or STREAM of
200: * logging output to be removed. See constants above.
201: * @param outputDevice The device associated with the particular output
202: * type being removed. Null for Console, filename for FILE, the actual
203: * output stream object for STREAM.
204: */
205: public boolean removeLogTarget(int outputType, Object outputDevice) {
206: String deviceString;
207: if (outputType == LogTarget.FILE) {
208: deviceString = (String) outputDevice;
209: } else if (outputType == LogTarget.STREAM) {
210: // FIXME this use of hashcode is fishy:
211: int id = ((OutputStream) outputDevice).hashCode();
212: deviceString = Integer.toString(id);
213: } else {
214: deviceString = null;
215: }
216: return removeLogTarget(outputType, deviceString);
217: }
218:
219: /**
220: * Remove a logging output type.
221: *
222: * This method is usually used in conjunction with
223: * {@link LoggerController#getLogTargets()} to
224: * iterate through list to remove items.
225: *
226: * @param outputType - The output type either CONSOLE, FILE, or STREAM of
227: * logging output to be removed. See constants above.
228: * @param deviceString - The device associated with the particular output
229: * type being removed. Null for Console, filename for FILE,
230: * the String identifier name associated with the output stream.
231: */
232: public boolean removeLogTarget(int outputType, String deviceString) {
233: Enumeration appenders = cat.getAllAppenders();
234:
235: Appender matchingAppender = null;
236: Class findClass = Util.convertIntToAppenderType(outputType);
237: while (appenders.hasMoreElements()) {
238: Appender appender = (Appender) appenders.nextElement();
239: if (appender.getClass() == findClass) {
240: if ((appender instanceof ConsoleAppender)
241: || ((appender instanceof FileAppender) && (((FileAppender) appender)
242: .getFile().equals(deviceString)))) {
243: matchingAppender = appender;
244: } else if (appender.getName().equals(deviceString)) {
245: matchingAppender = appender;
246: }
247: }
248: }
249:
250: if (matchingAppender != null) {
251: cat.removeAppender(matchingAppender);
252: return true;
253: } else {
254: return false;
255: }
256: }
257: }
|