01: package org.jzonic.jlo.formatter;
02:
03: import org.jzonic.jlo.LogRecord;
04:
05: import java.text.SimpleDateFormat;
06: import java.util.Date;
07: import java.util.Map;
08:
09: /**
10: * The SimpleFormatter is a concrete implementation of a Formatter.
11: * The format is:
12: * <pre>
13: * yyyy/MM/dd HH:mm:ss - [target] text of the message {Exception}
14: * </pre>
15: * Example:
16: * <pre>
17: * 2003/01/28 12:14:30 - [fatal] hello world
18: * </pre>
19: *
20: *@author Andreas Mecky
21: *@author Terry Dye
22: */
23: public class SimpleFormatter extends AbstractFormatter {
24:
25: private String df = "yyyy/MM/dd HH:mm:ss - ";
26:
27: /**
28: * Constructor for the SimpleFormatter object
29: */
30: public SimpleFormatter(String configName) {
31: super (configName);
32: }
33:
34: /**
35: *@param lr The LogRecord
36: *@return The message as string that should be logged
37: */
38: public String formatMessage(LogRecord lr) {
39: SimpleDateFormat formatter = new SimpleDateFormat(df);
40: Date currentTime = new Date();
41: String dateString = formatter.format(currentTime);
42: StringBuffer sb = new StringBuffer();
43: sb.append(dateString);
44: if (lr.getTarget() != null) {
45: sb.append("[");
46: sb.append(lr.getTarget().getName().toUpperCase());
47: sb.append("] ");
48: }
49: if (lr.getMessage() != null) {
50: sb.append(lr.getMessage());
51: }
52: if (lr.getThrown() != null) {
53: sb.append(":");
54: sb.append(lr.getStackTrace());
55: }
56: return sb.toString();
57: }
58:
59: public void setParameter(Map params) {
60: if (params.containsKey("date")) {
61: df = (String) params.get("date") + " - ";
62: }
63: }
64: }
|