1 package org.kuali.common.jute.logging;
2
3 import static com.google.common.base.StandardSystemProperty.LINE_SEPARATOR;
4
5 import java.text.SimpleDateFormat;
6 import java.util.Date;
7 import java.util.List;
8 import java.util.logging.Formatter;
9 import java.util.logging.LogRecord;
10
11 import org.kuali.common.jute.runtime.ProcessIdProvider;
12
13 import com.google.common.base.Optional;
14 import com.google.common.base.Splitter;
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31 public class LogFormatter extends Formatter {
32
33 private static final String FORMAT = "yyyy-MM-dd HH:mm:ss.SSS";
34 private static final String LS = LINE_SEPARATOR.value();
35 private static final Optional<Integer> PID = ProcessIdProvider.INSTANCE.get();
36 private static final Splitter SPLITTER = Splitter.on('.').omitEmptyStrings().trimResults();
37
38 @Override
39 public String format(LogRecord record) {
40
41 SimpleDateFormat format = new SimpleDateFormat(FORMAT);
42 String date = format.format(new Date(record.getMillis()));
43 String thread = Thread.currentThread().getName();
44 String name = shorten(record.getLoggerName());
45 String method = record.getSourceMethodName();
46
47 StringBuilder sb = new StringBuilder();
48 sb.append(date);
49 sb.append(" ");
50 sb.append(record.getLevel());
51 sb.append(" ");
52 sb.append(PID.orNull());
53 sb.append(" [");
54 sb.append(thread);
55 sb.append("] ");
56 sb.append(name);
57 sb.append(".");
58 sb.append(method);
59 sb.append(" : ");
60 sb.append(record.getMessage());
61 sb.append(" ");
62 sb.append(LS);
63 return sb.toString();
64 }
65
66 protected String shorten(String name) {
67 if (name == null) {
68 return "root";
69 }
70 List<String> tokens = SPLITTER.splitToList(name);
71 StringBuilder sb = new StringBuilder();
72 for (int i = 0; i < tokens.size() - 1; i++) {
73 sb.append(tokens.get(i).substring(0, 1));
74 sb.append('.');
75 }
76 sb.append(tokens.get(tokens.size() - 1));
77 return sb.toString();
78 }
79
80 }