View Javadoc

1   package cz.cuni.amis.utils.logging;
2   
3   import java.text.MessageFormat;
4   import java.text.SimpleDateFormat;
5   import java.util.Arrays;
6   import java.util.Date;
7   import java.util.logging.Formatter;
8   import java.util.logging.LogRecord;
9   
10  /**
11   * Pogamut custom formatter used as default.
12   *
13   * @author Jimmy
14   */
15  public class DefaultLogFormatter extends Formatter {
16  	
17  	private SimpleDateFormat dateFormat = new SimpleDateFormat("HH:mm:ss.SSS");
18  	
19  	private static String longestUnitName = "";
20  	
21  	private static String longestComponentName = "";
22  	
23  	private StringBuffer buffer = new StringBuffer(512);
24  
25  	/**
26  	 * Whether to append {@link DefaultLogFormatter#lineEnd} after the log message.
27  	 */
28  	protected boolean lineEnds = false;
29  
30  	protected String lineEnd = "\r\n";
31  	
32  	protected String name = null;
33  
34  	public static final String[] whitespaces = new String[] {
35  		"        ",
36  		"       ",
37  		"      ",
38  		"     ",
39  		"    ",
40  		"   ",
41  		"  ",
42  		" ",
43  		""
44  	};
45  	
46  	public DefaultLogFormatter() {
47  		this(null, false);
48  	}
49  	
50  	public DefaultLogFormatter(String unitName) {
51  		this.name = unitName;
52  	}
53  	
54  	public DefaultLogFormatter(String unitName, boolean appendLineEnd) {
55  		this.name = unitName;
56  		this.lineEnds = appendLineEnd;
57  	}
58  	
59  	@Override
60  	public synchronized String format(LogRecord record) {
61  		buffer.delete(0, buffer.length());
62  
63  		if (name != null) {
64  			String n = name;
65  			if (n == null) n = "null";
66  			if (n.length() > longestUnitName.length()) {
67  				StringBuffer longest = new StringBuffer();
68  				for (int i = 0; i < n.length(); ++i) {
69  					longest.append(" ");
70  				}
71  				longestUnitName = longest.toString();
72  			}
73  			buffer.append("(");
74  			buffer.append(n);
75  			buffer.append(") ");
76  			int count = longestUnitName.length() - n.length();
77  			for (int i = 0; i < count; ++i) {
78  				buffer.append(" ");
79  			}
80  		} else {
81  			if (longestUnitName.length() > 0) {
82  				buffer.append("()");
83  				buffer.append(longestUnitName);
84  			} else {
85  				buffer.append("() ");
86  			}
87  		}
88  		
89  		buffer.append("[");
90  		buffer.append(record.getLevel().toString());
91  		buffer.append("]");
92  		buffer.append(whitespaces[record.getLevel().toString().length()]);
93  
94  		buffer.append(dateFormat.format(new Date(record.getMillis())));
95  		
96  		buffer.append(" ");
97  		
98  		String n = record.getLoggerName();
99  		if (n == null) n = "null";
100 		if (n.length() > longestComponentName.length()) {
101 			StringBuffer longest = new StringBuffer();
102 			for (int i = 0; i < n.length(); ++i) {
103 				longest.append(" ");
104 			}
105 			longestComponentName = longest.toString();
106 		}
107 		buffer.append("<");
108 		buffer.append(n);
109 		buffer.append("> ");
110 		int count = longestComponentName.length() - n.length();
111 		for (int i = 0; i < count; ++i) {
112 			buffer.append(" ");
113 		}
114 		
115 		Object[] params = record.getParameters();
116 		if (params == null || params.length <= 1) {
117 		    buffer.append(record.getMessage());
118 		} else {
119 		     Object[] passedParams = Arrays.copyOfRange(params, 1, params.length);
120 		     buffer.append(MessageFormat.format(record.getMessage(), passedParams));
121 		}
122 		
123 		if (lineEnds) {
124 			buffer.append(lineEnd);
125 		}
126 		
127 		return buffer.toString();
128 	}
129 
130 }