View Javadoc

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