KickJava   Java API By Example, From Geeks To Geeks.

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


1 package org.jzonic.jlo.formatter;
2
3 import org.jzonic.jlo.LogRecord;
4 import org.jzonic.jlo.formatter.tokens.TokenParser;
5
6 import java.util.Map JavaDoc;
7
8 /**
9  * This formatter can be configured using the format parameter. Syntax
10  * <pre>
11  * <formatter class="DefinedFormatter">
12  * <parameter name="format" value="${date} ${time} : [${target}] [${class}] ${text}"/>
13  * </formatter>
14  * </pre>
15  * The class uses the TokenParser to extract the tokens from the format string and
16  * the TokenParser will replace every occurence with the specific output of the
17  * FormatterToken
18  *
19  * @author Andreas Mecky
20  * @author Terry Dye
21  */

22 public class DefinedFormatter extends AbstractFormatter {
23
24     private String JavaDoc format = "${date} ${time} : [${target}] [${class}] ${text}";
25
26     public DefinedFormatter(String JavaDoc configName) {
27         super(configName);
28     }
29
30     /**
31      * Formats the LogRecord according to the defined format
32      *
33      * @param lr the LogRecord
34      * @return the formatted String
35      */

36     public String JavaDoc formatMessage(LogRecord lr) {
37         TokenParser tp = new TokenParser();
38         return tp.parseLine(lr,format);
39     }
40
41     /**
42      * Sets the format that should be used to render the LogRecord. If not set
43      * the default format is:<br/>
44      * ${date} ${time} : [${target}] [${class}] ${text}
45      *
46      * @param params a Map containing all parameters defined for the formatter
47      */

48     public void setParameter(Map JavaDoc params) {
49         if ( params.containsKey("format")) {
50             format = (String JavaDoc)params.get("format");
51         }
52     }
53 }
54
Popular Tags