001: package com.lutris.logging;
002:
003: import org.apache.log4j.Level;
004: import org.apache.log4j.Logger;
005:
006: /**
007: * Temporary class for maintaing old applications that uses
008: * deprecated com.lutris.logging.*
009: */
010: public class LogAdapter implements LogChannel {
011: private static Logger logger = null;
012: private static final int INT_ERROR = -1;
013:
014: public LogAdapter() {
015: }
016:
017: public LogAdapter(org.apache.log4j.Logger logger) {
018: this .logger = logger;
019: }
020:
021: public void setLogger(org.apache.log4j.Logger log) {
022: logger = log;
023: }
024:
025: public org.apache.log4j.Logger getLogger() {
026: return logger;
027: }
028:
029: public int getLevel(String level) {
030: if (logger != null) {
031: if (logger.getLevel() != null) {
032: return logger.getLevel().toInt();
033: }
034: }
035: return INT_ERROR;
036: }
037:
038: /**
039: * Determine if logging is enabled for the specified level.
040: */
041: public boolean isEnabled(int level) {
042: return logger.isEnabledFor(intToLevel(level));
043: }
044:
045: /**
046: * Determine if logging is enabled for the specified level.
047: */
048: public boolean isEnabled(String level) {
049: return false;
050: }
051:
052: /**
053: * Write a string to the log file.
054: */
055: public void write(int level, String msg) {
056: Level lev;
057:
058: if (logger == null) {
059: logger = Logger.getLogger(LogAdapter.class.getName());
060: }
061: lev = intToLevel(level);
062: if (lev == null) {
063: lev = Level.INFO;
064: }
065: logger.log(lev, msg);
066: }
067:
068: /**
069: * Write a string and exception to the log file.
070: */
071: public void write(int level, String msg, Throwable throwable) {
072: Level lev;
073:
074: if (logger == null) {
075: logger = Logger.getLogger(LogAdapter.class.getName());
076: }
077: lev = intToLevel(level);
078: if (lev == null) {
079: lev = Level.INFO;
080: }
081: logger.log(lev, msg, throwable);
082: }
083:
084: /**
085: * Write a string to the log file.
086: */
087: public void write(String level, String msg) {
088: Level lev;
089:
090: if (logger == null) {
091: logger = Logger.getLogger(LogAdapter.class.getName());
092: }
093: lev = stringToLevel(level);
094: if (lev == null) {
095: lev = Level.INFO;
096: }
097: logger.log(lev, msg);
098: }
099:
100: /**
101: * Write a string and exception to the log file.
102: */
103: public void write(String level, String msg, Throwable throwable) {
104: Level lev;
105:
106: if (logger == null) {
107: logger = Logger.getLogger(LogAdapter.class.getName());
108: }
109: lev = stringToLevel(level);
110: if (lev == null) {
111: lev = Level.INFO;
112: }
113: logger.log(lev, msg);
114: }
115:
116: private Level intToLevel(int level) {
117: Level lev;
118:
119: switch (level) {
120: case com.lutris.logging.Logger.EMERGENCY:
121: case com.lutris.logging.Logger.ALERT:
122: lev = Level.FATAL;
123: break;
124:
125: case com.lutris.logging.Logger.CRITICAL:
126: case com.lutris.logging.Logger.ERROR:
127: lev = Level.ERROR;
128: break;
129:
130: case com.lutris.logging.Logger.WARNING:
131: lev = Level.WARN;
132: break;
133:
134: case com.lutris.logging.Logger.NOTICE:
135: case com.lutris.logging.Logger.INFO:
136: lev = Level.INFO;
137: break;
138:
139: case com.lutris.logging.Logger.DEBUG:
140: case com.lutris.logging.Logger.DEBUG1:
141: case com.lutris.logging.Logger.DEBUG2:
142: case com.lutris.logging.Logger.DEBUG3:
143: case com.lutris.logging.Logger.DEBUG4:
144: case com.lutris.logging.Logger.DEBUG5:
145: case com.lutris.logging.Logger.DEBUG6:
146: case com.lutris.logging.Logger.DEBUG7:
147: case com.lutris.logging.Logger.DEBUG8:
148: case com.lutris.logging.Logger.DEBUG9:
149: case com.lutris.logging.Logger.DEBUGTMP:
150: lev = Level.DEBUG;
151: break;
152:
153: default:
154: lev = null;
155: } // end switch (level)
156: return lev;
157: }
158:
159: private Level stringToLevel(String level) {
160: Level lev;
161:
162: if (level.equalsIgnoreCase("EMERGENCY")
163: || level.equalsIgnoreCase("ALERT")) {
164: lev = Level.FATAL;
165: } else if (level.equalsIgnoreCase("CRITICAL")
166: || level.equalsIgnoreCase("ERROR")) {
167: lev = Level.ERROR;
168: } else if (level.equalsIgnoreCase("WARNING")) {
169: lev = Level.WARN;
170: } else if (level.equalsIgnoreCase("NOTICE")
171: || level.equalsIgnoreCase("INFO")) {
172: lev = Level.INFO;
173: } else if (level.equalsIgnoreCase("DEBUG")
174: || level.equalsIgnoreCase("DEBUG1")
175: || level.equalsIgnoreCase("DEBUG2")
176: || level.equalsIgnoreCase("DEBUG3")
177: || level.equalsIgnoreCase("DEBUG4")
178: || level.equalsIgnoreCase("DEBUG5")
179: || level.equalsIgnoreCase("DEBUG6")
180: || level.equalsIgnoreCase("DEBUG7")
181: || level.equalsIgnoreCase("DEBUG8")
182: || level.equalsIgnoreCase("DEBUG9")
183: || level.equalsIgnoreCase("DEBUGTMP")) {
184: lev = Level.DEBUG;
185: } else {// default
186: lev = null;
187: }
188: return lev;
189: }
190:
191: /**
192: * Create a LogWrite associated with a particular level. This
193: * is often an easier object to use than a LogChannel if limited
194: * levels are needed.
195: *
196: * @param level Symbolic level that is to be checked.
197: * @return A log writer object.
198: */
199: public LogWriter getLogWriter(String level) {
200: // FIXME:
201: return null;
202: }
203:
204: /**
205: * Create a LogWrite associated with a particular level. This
206: * is often an easier object to use than a LogChannel if limited
207: * levels are needed.
208: *
209: * @param level Numeric level that is to be checked.
210: * @return A log writer object.
211: */
212: public LogWriter getLogWriter(int level) {
213: // FIXME:
214: return null;
215: }
216: }
217:
218: /*
219: old log levels:
220: "EMERGENCY" = 0
221: "ALERT" = 1
222: "CRITICAL" = 2
223: "ERROR" = 3
224: "WARNING" = 4
225: "NOTICE" = 5
226: "INFO" = 6
227: "DEBUG" = 7
228: "DEBUG1" = 8
229: "DEBUG2" = 9
230: "DEBUG3" = 10
231: "DEBUG4" = 11
232: "DEBUG5" = 12
233: "DEBUG6" = 13
234: "DEBUG7" = 14
235: "DEBUG8" = 15
236: "DEBUG9" = 16
237: "DEBUGTMP" = 17
238: */
|