KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  * Enhydra Java Application Server Project
3  *
4  * The contents of this file are subject to the Enhydra Public License
5  * Version 1.1 (the "License"); you may not use this file except in
6  * compliance with the License. You may obtain a copy of the License on
7  * the Enhydra web site ( http://www.enhydra.org/ ).
8  *
9  * Software distributed under the License is distributed on an "AS IS"
10  * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See
11  * the License for the specific terms governing rights and limitations
12  * under the License.
13  *
14  * The Initial Developer of the Enhydra Application Server is Lutris
15  * Technologies, Inc. The Enhydra Application Server and portions created
16  * by Lutris Technologies, Inc. are Copyright Lutris Technologies, Inc.
17  * All Rights Reserved.
18  *
19  * Contributor(s):
20  *
21  * $Id: Log4jLogChannel.java,v 1.4 2005/03/24 10:51:20 slobodan Exp $
22  */

23 package com.lutris.logging;
24
25 import java.text.SimpleDateFormat JavaDoc;
26
27 import org.apache.log4j.Level;
28 import org.apache.log4j.Logger;
29
30 /**
31  * Standard implementation of a channel associated with a logging
32  * facility. All messages for the facility are written using a channel.
33  * Care is take to avoid synchronization when possible for performance
34  * reasons.
35  *
36  * @author Vladimir Puskas
37  * @author Predrag Djoic
38  * @author Sinisa Milosevic
39  * @see com.lutris.logging.LogChannel
40  * @see com.lutris.logging.Log4jLogger
41  */

42 public class Log4jLogChannel implements LogChannel {
43
44     /**
45      * Our symbolic name.
46      */

47      String JavaDoc separatorLine ="";
48     
49     private String JavaDoc facility;
50     
51     private static final int INT_ERROR = -1;
52
53     /**
54      * Format for the date.
55      */

56     private static final SimpleDateFormat JavaDoc dateFormatter = new SimpleDateFormat JavaDoc("yyyy.MM.dd HH:mm:ss");
57
58     /**
59      * Logger object with which we are associated.
60      */

61     private org.apache.log4j.Logger logger;
62
63     /**
64      * Construct a new log channel.
65      *
66      * @param chanFacility The facility that the channel is associate with.
67      * @param loggerObj The logging object that this channel will be associated
68      * with.
69      */

70     protected Log4jLogChannel(String JavaDoc chanFacility,
71             org.apache.log4j.Logger loggerObj,String JavaDoc separator) {
72         facility = chanFacility;
73         logger = loggerObj;
74         separatorLine=separator;
75  
76     }
77
78     public int getLevel(String JavaDoc level) {
79         if (logger != null) {
80             if (logger.getLevel() != null) {
81                 return logger.getLevel().toInt();
82             }
83         }
84         return INT_ERROR;
85     }
86
87     /**
88      * Determine if logging is enabled for the specified level.
89      */

90     public boolean isEnabled(int level) {
91         return true;
92 // return logger.isEnabledFor(intToLevel(level));
93
}
94
95     /**
96      * Determine if logging is enabled for the specified level.
97      */

98     public boolean isEnabled(String JavaDoc level) {
99         return true;
100 // return logger.isEnabledFor(stringToLevel(level));
101
}
102
103     /**
104      * Write a string to the log file.
105      */

106     public void write(int level, String JavaDoc msg) {
107         Level lev;
108
109         if (logger == null) {
110             logger = Logger.getLogger(LogAdapter.class.getName());
111         }
112         lev = intToLevel(level);
113         if (lev == null) {
114             lev = Level.INFO;
115         }
116        
117         logger.log(lev, msg + separatorLine);
118     }
119
120     /**
121      * Write a string and exception to the log file.
122      */

123     public void write(int level, String JavaDoc msg, Throwable JavaDoc throwable) {
124         Level lev;
125
126         if (logger == null) {
127             logger = Logger.getLogger(LogAdapter.class.getName());
128         }
129         lev = intToLevel(level);
130         if (lev == null) {
131             lev = Level.INFO;
132         }
133         logger.log(lev, msg + separatorLine, throwable);
134     }
135
136     /**
137      * Write a string to the log file.
138      */

139     public void write(String JavaDoc level, String JavaDoc msg) {
140         Level lev;
141
142         if (logger == null) {
143             logger = Logger.getLogger(LogAdapter.class.getName());
144         }
145         lev = stringToLevel(level);
146         if (lev == null) {
147             lev = Level.INFO;
148         }
149         logger.log(lev, msg + separatorLine);
150     }
151
152     /**
153      * Write a string and exception to the log file.
154      */

155     public void write(String JavaDoc level, String JavaDoc msg, Throwable JavaDoc throwable) {
156         Level lev;
157
158         if (logger == null) {
159             logger = Logger.getLogger(LogAdapter.class.getName());
160         }
161         lev = stringToLevel(level);
162         if (lev == null) {
163             lev = Level.INFO;
164         }
165         logger.log(lev, msg + separatorLine, throwable);
166     }
167
168     private Level intToLevel(int level) {
169         Level lev;
170
171         switch (level) {
172         case com.lutris.logging.Logger.EMERGENCY:
173         case com.lutris.logging.Logger.ALERT:
174             lev = Level.FATAL;
175             break;
176
177         case com.lutris.logging.Logger.CRITICAL:
178         case com.lutris.logging.Logger.ERROR:
179             lev = Level.ERROR;
180             break;
181
182         case com.lutris.logging.Logger.WARNING:
183             lev = Level.WARN;
184             break;
185
186         case com.lutris.logging.Logger.NOTICE:
187         case com.lutris.logging.Logger.INFO:
188             lev = Level.INFO;
189             break;
190
191         case com.lutris.logging.Logger.DEBUG:
192         case com.lutris.logging.Logger.DEBUG1:
193         case com.lutris.logging.Logger.DEBUG2:
194         case com.lutris.logging.Logger.DEBUG3:
195         case com.lutris.logging.Logger.DEBUG4:
196         case com.lutris.logging.Logger.DEBUG5:
197         case com.lutris.logging.Logger.DEBUG6:
198         case com.lutris.logging.Logger.DEBUG7:
199         case com.lutris.logging.Logger.DEBUG8:
200         case com.lutris.logging.Logger.DEBUG9:
201         case com.lutris.logging.Logger.DEBUGTMP:
202             lev = Level.DEBUG;
203             break;
204
205         default:
206             lev = Level.DEBUG;
207         } // end switch (level)
208
return lev;
209     }
210
211     private Level stringToLevel(String JavaDoc level) {
212         Level lev;
213
214         if (level.equalsIgnoreCase("EMERGENCY")
215                 || level.equalsIgnoreCase("ALERT")) {
216             lev = Level.FATAL;
217         } else if (level.equalsIgnoreCase("CRITICAL")
218                 || level.equalsIgnoreCase("ERROR")) {
219             lev = Level.ERROR;
220         } else if (level.equalsIgnoreCase("WARNING")) {
221             lev = Level.WARN;
222         } else if (level.equalsIgnoreCase("NOTICE")
223                 || level.equalsIgnoreCase("INFO")) {
224             lev = Level.INFO;
225         } else if (level.equalsIgnoreCase("DEBUG")
226                 || level.equalsIgnoreCase("DEBUG1")
227                 || level.equalsIgnoreCase("DEBUG2")
228                 || level.equalsIgnoreCase("DEBUG3")
229                 || level.equalsIgnoreCase("DEBUG4")
230                 || level.equalsIgnoreCase("DEBUG5")
231                 || level.equalsIgnoreCase("DEBUG6")
232                 || level.equalsIgnoreCase("DEBUG7")
233                 || level.equalsIgnoreCase("DEBUG8")
234                 || level.equalsIgnoreCase("DEBUG9")
235                 || level.equalsIgnoreCase("DEBUGTMP")) {
236             lev = Level.DEBUG;
237         } else {// default
238
lev = Level.DEBUG;
239         }
240         return lev;
241     }
242     
243     /**
244      * <b>NOT SUPPORTED</b>
245      *
246      * @param level Symbolic level that is to be checked.
247      * @return A log writer object.
248      */

249     public LogWriter getLogWriter(String JavaDoc level) {
250 // throw new UnsupportedOperationException("Log4jLogChannel doesn't support this operation.");
251
return new Log4jLogWriter(this, level);
252
253     }
254
255     /**
256      * <b>NOT SUPPORTED</b>
257      *
258      * @param level Numeric level that is to be checked.
259      * @return A log writer object.
260      */

261     public LogWriter getLogWriter(int level) {
262 // throw new UnsupportedOperationException("Log4jLogChannel doesn't support this operation.");
263
return new Log4jLogWriter(this, level);
264     }
265 }
266
Popular Tags