KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > lutris > logging > LogAdapter


1 package com.lutris.logging;
2
3 import org.apache.log4j.Level;
4 import org.apache.log4j.Logger;
5
6 /**
7  * Temporary class for maintaing old applications that uses
8  * deprecated com.lutris.logging.*
9  */

10 public class LogAdapter implements LogChannel {
11     private static Logger logger = null;
12     private static final int INT_ERROR = -1;
13     public LogAdapter() {}
14  
15     public LogAdapter(org.apache.log4j.Logger logger) {
16         this.logger = logger;
17     }
18
19     public void setLogger(org.apache.log4j.Logger log) {
20         logger = log;
21     }
22
23     public org.apache.log4j.Logger getLogger() {
24         return logger;
25     }
26
27     public int getLevel(String JavaDoc level) {
28         if (logger != null) {
29             if (logger.getLevel() != null) {
30                 return logger.getLevel().toInt();
31             }
32         }
33         return INT_ERROR;
34     }
35
36     /**
37      * Determine if logging is enabled for the specified level.
38      */

39     public boolean isEnabled(int level) {
40         return logger.isEnabledFor(intToLevel(level));
41     }
42
43     /**
44      * Determine if logging is enabled for the specified level.
45      */

46     public boolean isEnabled(String JavaDoc level) {
47         return false;
48     }
49
50     /**
51      * Write a string to the log file.
52      */

53     public void write(int level, String JavaDoc msg) {
54         Level lev;
55
56         if (logger == null) {
57             logger = Logger.getLogger(LogAdapter.class.getName());
58         }
59         lev = intToLevel(level);
60         if (lev == null) {
61             lev = Level.INFO;
62         }
63         logger.log(lev, msg);
64     }
65
66     /**
67      * Write a string and exception to the log file.
68      */

69     public void write(int level, String JavaDoc msg, Throwable JavaDoc throwable) {
70         Level lev;
71
72         if (logger == null) {
73             logger = Logger.getLogger(LogAdapter.class.getName());
74         }
75         lev = intToLevel(level);
76         if (lev == null) {
77             lev = Level.INFO;
78         }
79         logger.log(lev, msg, throwable);
80     }
81
82     /**
83      * Write a string to the log file.
84      */

85     public void write(String JavaDoc level, String JavaDoc msg) {
86         Level lev;
87
88         if (logger == null) {
89             logger = Logger.getLogger(LogAdapter.class.getName());
90         }
91         lev = stringToLevel(level);
92         if (lev == null) {
93             lev = Level.INFO;
94         }
95         logger.log(lev, msg);
96     }
97
98     /**
99      * Write a string and exception to the log file.
100      */

101     public void write(String JavaDoc level, String JavaDoc msg, Throwable JavaDoc throwable) {
102         Level lev;
103
104         if (logger == null) {
105             logger = Logger.getLogger(LogAdapter.class.getName());
106         }
107         lev = stringToLevel(level);
108         if (lev == null) {
109             lev = Level.INFO;
110         }
111         logger.log(lev, msg);
112     }
113
114     private Level intToLevel(int level) {
115         Level lev;
116
117         switch (level) {
118         case com.lutris.logging.Logger.EMERGENCY:
119         case com.lutris.logging.Logger.ALERT:
120             lev = Level.FATAL;
121             break;
122
123         case com.lutris.logging.Logger.CRITICAL:
124         case com.lutris.logging.Logger.ERROR:
125             lev = Level.ERROR;
126             break;
127
128         case com.lutris.logging.Logger.WARNING:
129             lev = Level.WARN;
130             break;
131
132         case com.lutris.logging.Logger.NOTICE:
133         case com.lutris.logging.Logger.INFO:
134             lev = Level.INFO;
135             break;
136
137         case com.lutris.logging.Logger.DEBUG:
138         case com.lutris.logging.Logger.DEBUG1:
139         case com.lutris.logging.Logger.DEBUG2:
140         case com.lutris.logging.Logger.DEBUG3:
141         case com.lutris.logging.Logger.DEBUG4:
142         case com.lutris.logging.Logger.DEBUG5:
143         case com.lutris.logging.Logger.DEBUG6:
144         case com.lutris.logging.Logger.DEBUG7:
145         case com.lutris.logging.Logger.DEBUG8:
146         case com.lutris.logging.Logger.DEBUG9:
147         case com.lutris.logging.Logger.DEBUGTMP:
148             lev = Level.DEBUG;
149             break;
150
151         default:
152             lev = null;
153         } // end switch (level)
154
return lev;
155     }
156
157     private Level stringToLevel(String JavaDoc level) {
158         Level lev;
159
160         if (level.equalsIgnoreCase("EMERGENCY")
161                 || level.equalsIgnoreCase("ALERT")) {
162             lev = Level.FATAL;
163         } else if (level.equalsIgnoreCase("CRITICAL")
164                 || level.equalsIgnoreCase("ERROR")) {
165             lev = Level.ERROR;
166         } else if (level.equalsIgnoreCase("WARNING")) {
167             lev = Level.WARN;
168         } else if (level.equalsIgnoreCase("NOTICE")
169                 || level.equalsIgnoreCase("INFO")) {
170             lev = Level.INFO;
171         } else if (level.equalsIgnoreCase("DEBUG")
172                 || level.equalsIgnoreCase("DEBUG1")
173                 || level.equalsIgnoreCase("DEBUG2")
174                 || level.equalsIgnoreCase("DEBUG3")
175                 || level.equalsIgnoreCase("DEBUG4")
176                 || level.equalsIgnoreCase("DEBUG5")
177                 || level.equalsIgnoreCase("DEBUG6")
178                 || level.equalsIgnoreCase("DEBUG7")
179                 || level.equalsIgnoreCase("DEBUG8")
180                 || level.equalsIgnoreCase("DEBUG9")
181                 || level.equalsIgnoreCase("DEBUGTMP")) {
182             lev = Level.DEBUG;
183         } else {// default
184
lev = null;
185         }
186         return lev;
187     }
188     
189     /**
190      * Create a LogWrite associated with a particular level. This
191      * is often an easier object to use than a LogChannel if limited
192      * levels are needed.
193      *
194      * @param level Symbolic level that is to be checked.
195      * @return A log writer object.
196      */

197     public LogWriter getLogWriter(String JavaDoc level) {
198         // FIXME:
199
return null;
200     }
201
202     /**
203      * Create a LogWrite associated with a particular level. This
204      * is often an easier object to use than a LogChannel if limited
205      * levels are needed.
206      *
207      * @param level Numeric level that is to be checked.
208      * @return A log writer object.
209      */

210     public LogWriter getLogWriter(int level) {
211         // FIXME:
212
return null;
213     }
214 }
215
216 /*
217  old log levels:
218  "EMERGENCY" = 0
219  "ALERT" = 1
220  "CRITICAL" = 2
221  "ERROR" = 3
222  "WARNING" = 4
223  "NOTICE" = 5
224  "INFO" = 6
225  "DEBUG" = 7
226  "DEBUG1" = 8
227  "DEBUG2" = 9
228  "DEBUG3" = 10
229  "DEBUG4" = 11
230  "DEBUG5" = 12
231  "DEBUG6" = 13
232  "DEBUG7" = 14
233  "DEBUG8" = 15
234  "DEBUG9" = 16
235  "DEBUGTMP" = 17
236  */

237
Popular Tags