01: package org.jzonic.jlo.formatter;
02:
03: import org.jzonic.jlo.LogRecord;
04: import org.jzonic.jlo.formatter.tokens.TokenParser;
05:
06: import java.util.Map;
07:
08: /**
09: * 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 format = "${date} ${time} : [${target}] [${class}] ${text}";
25:
26: public DefinedFormatter(String 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 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 params) {
49: if (params.containsKey("format")) {
50: format = (String) params.get("format");
51: }
52: }
53: }
|