KickJava   Java API By Example, From Geeks To Geeks.

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


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

22 public class SimpleFormatter extends AbstractFormatter {
23
24     private String JavaDoc df = "yyyy/MM/dd HH:mm:ss - ";
25     /**
26      * Constructor for the SimpleFormatter object
27      */

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

36     public String JavaDoc formatMessage(LogRecord lr) {
37         SimpleDateFormat JavaDoc formatter = new SimpleDateFormat JavaDoc(df);
38         Date JavaDoc currentTime = new Date JavaDoc();
39         String JavaDoc dateString = formatter.format(currentTime);
40         StringBuffer JavaDoc sb = new StringBuffer JavaDoc();
41         sb.append(dateString);
42         if (lr.getTarget() != null) {
43             sb.append("[");
44             sb.append(lr.getTarget().getName().toUpperCase());
45             sb.append("] ");
46         }
47         if (lr.getMessage() != null) {
48             sb.append(lr.getMessage());
49         }
50         if ( lr.getThrown() != null ) {
51             sb.append(":");
52             sb.append(lr.getStackTrace());
53         }
54         return sb.toString();
55     }
56
57     public void setParameter(Map JavaDoc params) {
58         if ( params.containsKey("date")) {
59             df = (String JavaDoc)params.get("date")+" - ";
60         }
61     }
62 }
63
Popular Tags