KickJava   Java API By Example, From Geeks To Geeks.

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


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: MonologLogChannel.java,v 1.3 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.objectweb.util.monolog.api.BasicLevel;
28 import org.objectweb.util.monolog.api.Level;
29 //import org.objectweb.util.monolog.file.monolog.PropertiesConfAccess;
30

31
32 /**
33  * Monolog implementation of a channel associated with a logging
34  * facility. All messages for the facility are written using a channel.
35  * Care is take to avoid synchronization when possible for performance
36  * reasons.
37  *
38  * @author Sinisa Milosevic
39  * @see com.lutris.logging.LogChannel
40  * @see com.lutris.logging.Log4jLogger
41  */

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

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

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

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

69     protected MonologLogChannel(String JavaDoc chanFacility,
70             org.objectweb.util.monolog.api.Logger loggerObj,String JavaDoc separator) {
71         facility = chanFacility;
72         logger = loggerObj;
73         separatorLine=separator;
74     }
75
76     public int getLevel(String JavaDoc level) {
77         if (logger != null) {
78             return logger.getCurrentIntLevel();
79         }
80         return INT_ERROR;
81     }
82
83     /**
84      * Determine if logging is enabled for the specified level.
85      */

86     public boolean isEnabled(int level) {
87         return true;
88         // return logger.isEnabledFor(intToLevel(level));
89
}
90
91     /**
92      * Determine if logging is enabled for the specified level.
93      */

94     public boolean isEnabled(String JavaDoc level) {
95         return true;
96     }
97
98     /**
99      * Write a string to the log file.
100      */

101     public void write(int level, String JavaDoc msg) {
102         Level lev;
103
104         lev = intToLevel(level);
105         if (lev == null) {
106             lev = BasicLevel.LEVEL_INFO;
107         }
108         logger.log(lev, msg + separatorLine);
109     }
110
111     /**
112      * Write a string and exception to the log file.
113      */

114     public void write(int level, String JavaDoc msg, Throwable JavaDoc throwable) {
115         Level lev;
116
117         lev = intToLevel(level);
118         if (lev == null) {
119             lev = BasicLevel.LEVEL_INFO;
120         }
121         logger.log(lev, msg + separatorLine, throwable);
122     }
123
124     /**
125      * Write a string to the log file.
126      */

127     public void write(String JavaDoc level, String JavaDoc msg) {
128         Level lev;
129
130         lev = stringToLevel(level);
131         if (lev == null) {
132             lev = BasicLevel.LEVEL_INFO;
133         }
134         logger.log(lev, msg + separatorLine);
135     }
136
137     /**
138      * Write a string and exception to the log file.
139      */

140     public void write(String JavaDoc level, String JavaDoc msg, Throwable JavaDoc throwable) {
141         Level lev;
142         lev = stringToLevel(level);
143         if (lev == null) {
144             lev = BasicLevel.LEVEL_INFO;
145         }
146         logger.log(lev, msg + separatorLine, throwable);
147     }
148
149     private Level intToLevel(int level) {
150         Level lev;
151
152         switch (level) {
153         case com.lutris.logging.Logger.EMERGENCY:
154         case com.lutris.logging.Logger.ALERT:
155             lev = BasicLevel.LEVEL_FATAL;
156             break;
157
158         case com.lutris.logging.Logger.CRITICAL:
159         case com.lutris.logging.Logger.ERROR:
160             lev = BasicLevel.LEVEL_ERROR;
161             break;
162
163         case com.lutris.logging.Logger.WARNING:
164             lev = BasicLevel.LEVEL_WARN;
165             break;
166
167         case com.lutris.logging.Logger.NOTICE:
168         case com.lutris.logging.Logger.INFO:
169             lev = BasicLevel.LEVEL_INFO;
170             break;
171
172         case com.lutris.logging.Logger.DEBUG:
173         case com.lutris.logging.Logger.DEBUG1:
174         case com.lutris.logging.Logger.DEBUG2:
175         case com.lutris.logging.Logger.DEBUG3:
176         case com.lutris.logging.Logger.DEBUG4:
177         case com.lutris.logging.Logger.DEBUG5:
178         case com.lutris.logging.Logger.DEBUG6:
179         case com.lutris.logging.Logger.DEBUG7:
180         case com.lutris.logging.Logger.DEBUG8:
181         case com.lutris.logging.Logger.DEBUG9:
182         case com.lutris.logging.Logger.DEBUGTMP:
183             lev = BasicLevel.LEVEL_DEBUG;
184             break;
185
186         default:
187             lev = BasicLevel.LEVEL_DEBUG;
188         } // end switch (level)
189
return lev;
190     }
191
192     private Level stringToLevel(String JavaDoc level) {
193         Level lev;
194
195         if (level.equalsIgnoreCase("EMERGENCY")
196                 || level.equalsIgnoreCase("ALERT")) {
197             lev = BasicLevel.LEVEL_FATAL;
198         } else if (level.equalsIgnoreCase("CRITICAL")
199                 || level.equalsIgnoreCase("ERROR")) {
200             lev = BasicLevel.LEVEL_ERROR;
201         } else if (level.equalsIgnoreCase("WARNING")) {
202             lev = BasicLevel.LEVEL_WARN;
203         } else if (level.equalsIgnoreCase("NOTICE")
204                 || level.equalsIgnoreCase("INFO")) {
205             lev = BasicLevel.LEVEL_INFO;
206         } else if (level.equalsIgnoreCase("DEBUG")
207                 || level.equalsIgnoreCase("DEBUG1")
208                 || level.equalsIgnoreCase("DEBUG2")
209                 || level.equalsIgnoreCase("DEBUG3")
210                 || level.equalsIgnoreCase("DEBUG4")
211                 || level.equalsIgnoreCase("DEBUG5")
212                 || level.equalsIgnoreCase("DEBUG6")
213                 || level.equalsIgnoreCase("DEBUG7")
214                 || level.equalsIgnoreCase("DEBUG8")
215                 || level.equalsIgnoreCase("DEBUG9")
216                 || level.equalsIgnoreCase("DEBUGTMP")) {
217             lev = BasicLevel.LEVEL_DEBUG;
218         } else {// default
219
lev = BasicLevel.LEVEL_DEBUG;
220         }
221         return lev;
222     }
223     
224     /**
225      * <b>NOT SUPPORTED</b>
226      *
227      * @param level Symbolic level that is to be checked.
228      * @return A log writer object.
229      */

230     public LogWriter getLogWriter(String JavaDoc level) {
231       return new MonologWriter(this, level);
232
233     }
234
235     /**
236      * <b>NOT SUPPORTED</b>
237      *
238      * @param level Numeric level that is to be checked.
239      * @return A log writer object.
240      */

241     public LogWriter getLogWriter(int level) {
242       return new MonologWriter(this, level);
243     }
244 }
245
Popular Tags