1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
|
12 | |
|
13 | |
|
14 | |
|
15 | |
|
16 | |
package org.kuali.student.common.ui.client.logging; |
17 | |
|
18 | |
import java.util.ArrayList; |
19 | |
import java.util.HashMap; |
20 | |
import java.util.List; |
21 | |
import java.util.Map; |
22 | |
|
23 | |
import org.kuali.student.common.ui.client.application.KSAsyncCallback; |
24 | |
import org.kuali.student.common.ui.client.service.LogRpcService; |
25 | |
import org.kuali.student.common.ui.client.service.LogRpcServiceAsync; |
26 | |
|
27 | |
import com.google.gwt.core.client.GWT; |
28 | |
import com.google.gwt.user.client.Command; |
29 | |
import com.google.gwt.user.client.DeferredCommand; |
30 | |
|
31 | 0 | public class Logger { |
32 | |
|
33 | |
|
34 | |
|
35 | 0 | static LogLevel logLevel = LogLevel.WARN; |
36 | 0 | static LogBuffer buffer = new LogBuffer(); |
37 | 0 | static Map<String, String> clientContextInfo = new HashMap<String, String>(); |
38 | 0 | static int maxBufferSize = Integer.MAX_VALUE; |
39 | |
|
40 | |
|
41 | |
|
42 | |
|
43 | |
|
44 | |
public static void setMaxBufferSize(int maxSize) { |
45 | 0 | LogBuffer tmp = new LogBuffer(maxSize); |
46 | 0 | for (LogMessage m : buffer.getLogMessages()) { |
47 | 0 | tmp.add(m); |
48 | |
} |
49 | 0 | buffer = tmp; |
50 | 0 | } |
51 | |
|
52 | |
public static void setLogLevel(LogLevel level) { |
53 | 0 | logLevel = level; |
54 | 0 | } |
55 | |
public static LogLevel getLogLevel() { |
56 | 0 | return logLevel; |
57 | |
} |
58 | |
public static void log(LogMessage message) { |
59 | 0 | if (message.getLogLevel().getLevel() >= logLevel.getLevel()) { |
60 | 0 | buffer.add(message); |
61 | |
} |
62 | 0 | } |
63 | |
|
64 | |
public static void log(LogLevel level, String message) { |
65 | 0 | if (level.getLevel() >= logLevel.getLevel()) { |
66 | 0 | buffer.add(new LogMessage(level, message, (Throwable) null)); |
67 | |
} |
68 | 0 | } |
69 | |
|
70 | |
public static void log(LogLevel level, String message, Throwable error) { |
71 | 0 | if (level.getLevel() >= logLevel.getLevel()) { |
72 | 0 | buffer.add(new LogMessage(level, message, error)); |
73 | |
} |
74 | 0 | } |
75 | |
|
76 | |
public static void debug(String message) { |
77 | 0 | log(LogLevel.DEBUG, message); |
78 | 0 | } |
79 | |
public static void debug(String message, Throwable error) { |
80 | 0 | log(LogLevel.DEBUG, message, error); |
81 | 0 | } |
82 | |
|
83 | |
public static void info(String message) { |
84 | 0 | log(LogLevel.INFO, message); |
85 | 0 | } |
86 | |
public static void info(String message, Throwable error) { |
87 | 0 | log(LogLevel.INFO, message, error); |
88 | 0 | } |
89 | |
|
90 | |
public static void warn(String message) { |
91 | 0 | log(LogLevel.WARN, message); |
92 | 0 | } |
93 | |
public static void warn(String message, Throwable error) { |
94 | 0 | log(LogLevel.WARN, message, error); |
95 | 0 | } |
96 | |
|
97 | |
public static void error(String message) { |
98 | 0 | log(LogLevel.ERROR, message); |
99 | 0 | } |
100 | |
public static void error(String message, Throwable error) { |
101 | 0 | log(LogLevel.ERROR, message, error); |
102 | 0 | } |
103 | |
|
104 | |
public static void fatal(String message) { |
105 | 0 | log(LogLevel.FATAL, message); |
106 | 0 | } |
107 | |
public static void fatal(String message, Throwable error) { |
108 | 0 | log(LogLevel.FATAL, message, error); |
109 | 0 | } |
110 | |
|
111 | |
public static void reset() { |
112 | 0 | buffer = new LogBuffer(maxBufferSize); |
113 | 0 | } |
114 | |
|
115 | |
|
116 | |
|
117 | |
public static Map<String, String> getClientContextInfo() { |
118 | 0 | return clientContextInfo; |
119 | |
} |
120 | |
|
121 | |
public static void sendLogs() { |
122 | 0 | final List<LogMessage> messages = new ArrayList<LogMessage>(buffer.getLogMessages()); |
123 | 0 | final Map<String, String> context = new HashMap<String, String>(clientContextInfo); |
124 | 0 | reset(); |
125 | 0 | DeferredCommand.addCommand(new Command() { |
126 | |
public void execute() { |
127 | 0 | String log = formatLog(messages); |
128 | 0 | LogRpcServiceAsync logService = (LogRpcServiceAsync) GWT .create(LogRpcService.class); |
129 | |
|
130 | 0 | logService.sendLog(context, log, new KSAsyncCallback<Boolean>() { |
131 | |
public void handleFailure(Throwable caught) { |
132 | 0 | GWT.log("sendLog Failed", caught); |
133 | |
|
134 | 0 | } |
135 | |
|
136 | |
public void onSuccess(Boolean result) { |
137 | |
|
138 | 0 | GWT.log("sendLog OK", null); |
139 | 0 | } |
140 | |
}); |
141 | 0 | } |
142 | |
}); |
143 | 0 | } |
144 | |
|
145 | |
private static String formatLog(List<LogMessage> messages) { |
146 | 0 | StringBuilder s = new StringBuilder(); |
147 | 0 | for (LogMessage lm : messages) { |
148 | 0 | s.append(lm.getLogLevel().toString()); |
149 | 0 | s.append(":\t"); |
150 | 0 | s.append(lm.getMessage()); |
151 | 0 | Throwable t = lm.getError(); |
152 | 0 | if (t != null) { |
153 | 0 | appendStackTrace(t, s); |
154 | |
} |
155 | 0 | } |
156 | 0 | return s.toString(); |
157 | |
} |
158 | |
|
159 | |
private static void appendStackTrace(Throwable t, StringBuilder s) { |
160 | 0 | s.append(t.toString()); |
161 | 0 | s.append(": at\n"); |
162 | 0 | StackTraceElement[] stack = t.getStackTrace(); |
163 | 0 | for (StackTraceElement frame : stack) { |
164 | 0 | s.append(frame.toString()); |
165 | 0 | s.append("\n"); |
166 | |
} |
167 | 0 | } |
168 | |
} |