KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > caucho > log > ELFormatter


1 /*
2  * Copyright (c) 1998-2006 Caucho Technology -- all rights reserved
3  *
4  * This file is part of Resin(R) Open Source
5  *
6  * Each copy or derived work must preserve the copyright notice and this
7  * notice unmodified.
8  *
9  * Resin Open Source is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; either version 2 of the License, or
12  * (at your option) any later version.
13  *
14  * Resin Open Source is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE, or any warranty
17  * of NON-INFRINGEMENT. See the GNU General Public License for more
18  * details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with Resin Open Source; if not, write to the
22  * Free SoftwareFoundation, Inc.
23  * 59 Temple Place, Suite 330
24  * Boston, MA 02111-1307 USA
25  *
26  * @author Scott Ferguson
27  */

28
29 package com.caucho.log;
30
31 import com.caucho.config.ConfigELContext;
32 import com.caucho.config.ConfigException;
33 import com.caucho.config.types.RawString;
34 import com.caucho.el.AbstractVariableResolver;
35 import com.caucho.el.ELParser;
36 import com.caucho.el.Expr;
37 import com.caucho.util.L10N;
38
39 import javax.annotation.PostConstruct;
40 import javax.el.ELContext;
41 import javax.el.ELException;
42 import java.util.ResourceBundle JavaDoc;
43 import java.util.logging.Level JavaDoc;
44 import java.util.logging.LogRecord JavaDoc;
45
46 /**
47  * A Formatter that accepts an EL format string, and.
48  */

49 public class ELFormatter extends MessageFormatter {
50   static final L10N L = new L10N(ELFormatter.class);
51
52   private String JavaDoc _format;
53   private Expr _expr;
54
55   public void setFormat(RawString format)
56   {
57     _format = format.getValue();
58   }
59
60   public String JavaDoc getFormat()
61   {
62     return _format;
63   }
64
65   @PostConstruct
66   public void init()
67     throws ConfigException
68   {
69     if (_format != null) {
70       try {
71         _expr = (new ELParser(new ConfigELContext(), _format)).parse();
72       } catch (Exception JavaDoc ex) {
73         throw new ConfigException(ex);
74       }
75     }
76   }
77
78   public String JavaDoc format(LogRecord JavaDoc logRecord)
79   {
80     if (_expr == null) {
81       return super.format(logRecord);
82     }
83
84     String JavaDoc ret;
85     if (_expr == null) {
86       ret = super.format(logRecord);
87     }
88     else {
89       try {
90         ELFormatterVariableResolver vr = new ELFormatterVariableResolver();
91         vr.setLogRecord(logRecord);
92
93         ret = _expr.evalString(new ConfigELContext(vr));
94       }
95       catch (Exception JavaDoc ex) {
96         throw new RuntimeException JavaDoc(ex);
97       }
98     }
99
100     return ret;
101   }
102
103   class ELFormatterVariableResolver extends AbstractVariableResolver
104   {
105     ELFormatterLogRecord _logRecord;
106
107     public void setLogRecord(LogRecord JavaDoc logRecord)
108     {
109       _logRecord = new ELFormatterLogRecord(logRecord);
110     }
111
112     @Override JavaDoc
113     public Object JavaDoc getValue(ELContext env, Object JavaDoc base, Object JavaDoc property)
114       throws ELException
115     {
116       if (base != null || ! (property instanceof String JavaDoc))
117     return null;
118
119       if ("log".equals(property)) {
120     env.setPropertyResolved(true);
121     
122         return _logRecord;
123       }
124
125       return null;
126     }
127   }
128
129   /**
130    * An api similar to java.util.logging.LogRecord that provides more complete
131    * information for logging purposes.
132    */

133   public class ELFormatterLogRecord
134   {
135     LogRecord JavaDoc _logRecord;
136
137     ELFormatterLogRecord(LogRecord JavaDoc logRecord)
138     {
139       _logRecord = logRecord;
140     }
141
142     /**
143      * The "formatted" log message, after localization, substitution of
144      * parameters, and the inclusion of an exception stack trace if applicable.
145      * <p>
146      * During formatting, if the source logger has a localization
147      * ResourceBundle and if that ResourceBundle has an entry for
148      * this message string, then the message string is replaced
149      * with the localized value.
150      * <p>
151      * If the message has parameters, java.text.MessageFormat is used to format
152      * the message with the parameters.
153      * <p>
154      * If the log record has an associated exception, the stack trace is
155      * appended to the log message.
156      *
157      * @see java.text.MessageFormat
158      * @see java.lang.Throwable.printStackTrace()
159      */

160     public String JavaDoc getMessage()
161     {
162       /** use the formatMessage() method of the outer class */
163       return formatMessage(_logRecord);
164     }
165
166     /**
167      * The source Logger's name.
168      *
169      * @return source logger name, which may be null
170      */

171     public String JavaDoc getName()
172     {
173       return _logRecord.getLoggerName();
174     }
175    
176     /**
177      * The source Logger's name.
178      *
179      * @return source logger name, which may be null
180      */

181     public String JavaDoc getLoggerName()
182     {
183       return _logRecord.getLoggerName();
184     }
185    
186     /**
187      * The last component of the source Logger's name. The last component
188      * is everything that occurs after the last `.' character, usually
189      * it is the class name.
190      *
191      *
192      * @return short version of the source logger name, or null
193      */

194     public String JavaDoc getShortName()
195     {
196       String JavaDoc name = _logRecord.getLoggerName();
197
198       if (name != null) {
199         int index = name.lastIndexOf('.') + 1;
200         if (index > 0 && index < name.length()) {
201           name = name.substring(index);
202         }
203       }
204
205       return name;
206     }
207    
208     /**
209      * The logging message level, for example Level.INFO.
210      *
211      * @see java.util.logging.Level
212      */

213     public Level JavaDoc getLevel()
214     {
215       return _logRecord.getLevel();
216     }
217
218     /**
219      * The time of the logging event, in milliseconds since 1970.
220      */

221     public long getMillis()
222     {
223       return _logRecord.getMillis();
224     }
225
226     /**
227      * An identifier for the thread where the message originated.
228      */

229     public int getThreadID()
230     {
231       return _logRecord.getThreadID();
232     }
233
234     /**
235      * The throwable associated with the log record, if one was associated.
236      */

237     public Throwable JavaDoc getThrown()
238     {
239       return _logRecord.getThrown();
240     }
241
242     /**
243      * The sequence number, normally assigned in the constructor of LogRecord.
244      */

245     public long getSequenceNumber()
246     {
247       return _logRecord.getSequenceNumber();
248     }
249
250     /**
251      * The name of the class that issued the logging request.
252      * This name may be unavailable, or not actually the name of the class that
253      * issued the logging message.
254      */

255     public String JavaDoc getSourceClassName()
256     {
257       return _logRecord.getSourceClassName();
258     }
259
260     /**
261      * The last component of the name (everthing after the last `.') of the
262      * class that issued the logging request.
263      * This name may be unavailable, or not actually the name of the class that
264      * issued the logging message.
265      *
266      * @return short version of the sourceClassName
267      */

268     public String JavaDoc getShortSourceClassName()
269     {
270       String JavaDoc name = _logRecord.getSourceClassName();
271
272       if (name != null) {
273         int index = name.lastIndexOf('.') + 1;
274         if (index > 0 && index < name.length()) {
275           name = name.substring(index);
276         }
277       }
278
279       return name;
280     }
281    
282     /**
283      * The name of the method that issued the logging request. This name
284      * may be unavailable, or not actually the name of the class that issued
285      * the logging message.
286      */

287     public String JavaDoc getSourceMethodName()
288     {
289       return _logRecord.getSourceMethodName();
290     }
291
292     /**
293      * The "raw" log message, before localization or substitution
294      * of parameters.
295      * <p>
296      * This returned message will be either the final text, text containing
297      * parameter substitution "format elements" (like `{0}') for use by
298      * java.text.MessageFormat, or a localization key.
299      *
300      * @see java.text.MessageFormat
301      */

302     public String JavaDoc getRawMessage()
303     {
304       return _logRecord.getMessage();
305     }
306
307     /**
308      * The resource bundle for localization.
309      */

310     public ResourceBundle JavaDoc getResourceBundle()
311     {
312       return _logRecord.getResourceBundle();
313     }
314    
315     /**
316      * The name of resource bundle for localization.
317      */

318     public String JavaDoc getResourceBundleName()
319     {
320       return _logRecord.getResourceBundleName();
321     }
322    
323     public Object JavaDoc[] getParameters()
324     {
325       return _logRecord.getParameters();
326     }
327   }
328 }
329
330
Popular Tags