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