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.IOException;
030: import java.io.OutputStream;
031:
032: import org.apache.log4j.Appender;
033: import org.apache.log4j.ConsoleAppender;
034: import org.apache.log4j.FileAppender;
035: import org.apache.log4j.Level;
036: import org.apache.log4j.Priority;
037: import org.apache.log4j.SimpleLayout;
038: import org.apache.log4j.WriterAppender;
039: import org.cougaar.util.log.LogTarget;
040: import org.cougaar.util.log.Logger;
041:
042: /**
043: * Package-private utility class for log4j utils.
044: */
045: final class Util {
046:
047: private Util() {
048: // just utility methods
049: }
050:
051: // FIXME optimize to use "static final" arrays!!!
052: // maybe alter the Logger constants to match log4j...
053:
054: /**
055: * Private utility to change between int defined by Logger
056: * and Priority class of log4j.
057: * @param level An integer from Logger.
058: * @deprecated Use Level methods instead
059: */
060: public static final Priority convertIntToPriority(int level) {
061: switch (level) {
062: case Logger.DETAIL:
063: return DetailPriority.DETAIL;
064: case Logger.DEBUG:
065: return Priority.DEBUG;
066: case Logger.INFO:
067: return Priority.INFO;
068: case Logger.WARN:
069: return Priority.WARN;
070: case Logger.ERROR:
071: return Priority.ERROR;
072: case Logger.SHOUT:
073: return ShoutPriority.SHOUT;
074: case Logger.FATAL:
075: return Priority.FATAL;
076: default:
077: return null;
078: }
079: }
080:
081: /**
082: * Private utility to change between int defined by Logger
083: * and Priority class of log4j.
084: * @param level A log4j Priority
085: * @deprecated Use Level methods instead
086: */
087: public static final int convertPriorityToInt(Priority level) {
088: switch (level.toInt()) {
089: case DetailPriority.DETAIL_INT:
090: return Logger.DETAIL;
091: case Priority.DEBUG_INT:
092: return Logger.DEBUG;
093: case Priority.INFO_INT:
094: return Logger.INFO;
095: case Priority.WARN_INT:
096: return Logger.WARN;
097: case Priority.ERROR_INT:
098: return Logger.ERROR;
099: case ShoutPriority.SHOUT_INT:
100: return Logger.SHOUT;
101: case Priority.FATAL_INT:
102: return Logger.FATAL;
103: default:
104: return 0;
105: }
106: }
107:
108: /**
109: * Private utility to change between int defined by Logger
110: * and Level class of log4j.
111: * @param level An integer from Logger.
112: */
113: public static final Level convertIntToLevel(int level) {
114: switch (level) {
115: case Logger.DETAIL:
116: return DetailPriority.DETAIL;
117: case Logger.DEBUG:
118: return Level.DEBUG;
119: case Logger.INFO:
120: return Level.INFO;
121: case Logger.WARN:
122: return Level.WARN;
123: case Logger.ERROR:
124: return Level.ERROR;
125: case Logger.SHOUT:
126: return ShoutPriority.SHOUT;
127: case Logger.FATAL:
128: return Level.FATAL;
129: default:
130: return null;
131: }
132: }
133:
134: /**
135: * Private utility to change between int defined by Logger
136: * and Level class of log4j.
137: * @param level A log4j Level
138: */
139: public static final int convertLevelToInt(Level level) {
140: switch (level.toInt()) {
141: case DetailPriority.DETAIL_INT:
142: return Logger.DETAIL;
143: case Level.DEBUG_INT:
144: return Logger.DEBUG;
145: case Level.INFO_INT:
146: return Logger.INFO;
147: case Level.WARN_INT:
148: return Logger.WARN;
149: case Level.ERROR_INT:
150: return Logger.ERROR;
151: case ShoutPriority.SHOUT_INT:
152: return Logger.SHOUT;
153: case Level.FATAL_INT:
154: return Logger.FATAL;
155: default:
156: return 0;
157: }
158: }
159:
160: public static final int convertStringToInt(String s) {
161: if (s == null) {
162: return -1;
163: } else if (s.equalsIgnoreCase("DETAIL")) {
164: return Logger.DETAIL;
165: } else if (s.equalsIgnoreCase("DEBUG")) {
166: return Logger.DEBUG;
167: } else if (s.equalsIgnoreCase("INFO")) {
168: return Logger.INFO;
169: } else if (s.equalsIgnoreCase("WARN")) {
170: return Logger.WARN;
171: } else if (s.equalsIgnoreCase("ERROR")) {
172: return Logger.ERROR;
173: } else if (s.equalsIgnoreCase("SHOUT")) {
174: return Logger.SHOUT;
175: } else if (s.equalsIgnoreCase("FATAL")) {
176: return Logger.FATAL;
177: } else {
178: return -1;
179: }
180: }
181:
182: public static final String convertIntToString(int level) {
183: switch (level) {
184: case Logger.DETAIL:
185: return "DETAIL";
186: case Logger.DEBUG:
187: return "DEBUG";
188: case Logger.INFO:
189: return "INFO";
190: case Logger.WARN:
191: return "WARN";
192: case Logger.ERROR:
193: return "ERROR";
194: case Logger.SHOUT:
195: return "SHOUT";
196: case Logger.FATAL:
197: return "FATAL";
198: default:
199: return null;
200: }
201: }
202:
203: /**
204: * Helper function to convert the interfaces outputType (See
205: * {@link LogTarget}) into a log4j equivilent
206: * Appender class.
207: *
208: * @param outputType - The input type CONSOLE, FILE, or STREAM
209: * from {@link LogTarget.CONSOLE LogTarget}
210: * @return The log 4j Appender class corresponding to the CONSOLE
211: */
212: public static Class convertIntToAppenderType(int outputType) {
213: switch (outputType) {
214: case LogTarget.CONSOLE:
215: return ConsoleAppender.class;
216: case LogTarget.STREAM:
217: return WriterAppender.class;
218: case LogTarget.FILE:
219: return FileAppender.class;
220: default:
221: return null;
222: }
223: }
224:
225: /**
226: * Helper function to convert outputType and device into the
227: * actual appropriate Appender, used for adding output types.
228: *
229: * @param outputType - The output type either CONSOLE, FILE, or
230: * STREAM of logging output to be removed.
231: * See {@link LogTarget}.
232: * @param outputDevice - The device associated with the
233: * particular APPENDER.
234: */
235: public static Appender convertIntToAppender(int outputType,
236: Object outputDevice) {
237: switch (outputType) {
238: case LogTarget.CONSOLE:
239: return new ConsoleAppender(new SimpleLayout());
240: case LogTarget.STREAM:
241: if ((outputDevice != null)
242: && (outputDevice instanceof OutputStream)) {
243: WriterAppender appender = new WriterAppender(
244: new SimpleLayout(), (OutputStream) outputDevice);
245: appender.setName(Integer.toString(outputDevice
246: .hashCode()));
247: } else {
248: //Report Error
249: }
250: break;
251: case LogTarget.FILE:
252: if ((outputDevice != null)
253: && (outputDevice instanceof String)) {
254: try {
255: return new FileAppender(new SimpleLayout(),
256: (String) outputDevice, true);
257: } catch (IOException e) {
258: //Report Error
259: }
260: } else {
261: //Report Error
262: }
263: break;
264: default:
265: //Report Error
266: return null;
267: }
268: return null;
269: }
270: }
|