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