KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jzonic > jlo > formatter > DefaultFormatter


1 package org.jzonic.jlo.formatter;
2
3 import org.jzonic.jlo.LogRecord;
4 import java.text.SimpleDateFormat JavaDoc;
5 import java.util.Date JavaDoc;
6 import java.util.Map JavaDoc;
7 /**
8  * The DefaultFormatter is a concrete implementation of a Formatter.
9  * The format is:
10  * <pre>
11  * yyyy/MM/dd HH:mm:ss - [target] [fq-classname] text of the message
12  * </pre>
13  * Example:
14  * <pre>
15  * 2003/04/01 17:00:13 - [init] [org.jzonic.jlo.DefaultFormatter] hello world
16  * </pre>
17  *
18  *@author Andreas Mecky
19  *@author Terry Dye
20  */

21 public class DefaultFormatter extends AbstractFormatter {
22     /**
23      * Constructor for the SimpleFormatter object
24      */

25     public DefaultFormatter(String JavaDoc configName) {
26         super(configName);
27     }
28     
29     
30     /**
31      *@param lr The LogRecord
32      *@return The message as string that should be logged
33      */

34     public String JavaDoc formatMessage(LogRecord lr) {
35         SimpleDateFormat JavaDoc formatter = new SimpleDateFormat JavaDoc("yyyy/MM/dd HH:mm:ss - ");
36         Date JavaDoc currentTime = new Date JavaDoc();
37         String JavaDoc dateString = formatter.format(currentTime);
38         StringBuffer JavaDoc sb = new StringBuffer JavaDoc();
39         sb.append(dateString);
40         if ( lr.getElapsed() != -1 ) {
41             sb.append(" [ellapsed: ");
42             sb.append(lr.getElapsed());
43             sb.append("ms] ");
44         }
45         String JavaDoc ndc = lr.getNDC();
46         if ( ndc != null ) {
47             sb.append(ndc);
48             sb.append(" ");
49         }
50         if (lr.getTarget() != null) {
51             sb.append("[");
52             sb.append(lr.getTarget().getName().toUpperCase());
53             sb.append("] ");
54         }
55         String JavaDoc callerLine = lr.getSourceClassName();
56         if ( callerLine != null ) {
57             sb.append("[");
58             sb.append(callerLine);
59             sb.append("] ");
60         }
61         if (lr.getMessage() != null) {
62             sb.append(lr.getMessage());
63         }
64         if ( lr.getThrown() != null ) {
65             sb.append(":");
66             sb.append(lr.getStackTrace());
67         }
68         return sb.toString();
69     }
70
71     public void setParameter(Map JavaDoc params) {
72         // we do not need any
73
}
74 }
75
Popular Tags